0

We have just started using FluentValidation and have followed this post to allow us to use our globalized values: http://fluentvalidation.codeplex.com/discussions/394471.

However we are stuck on how to get a reference to the interface for the translation service which wraps our existing globalization values. This is contained in a separate project as is not directly coupled to ASP.NET MVC so we can't use DependencyResolver.

Does anyone know of a way we can get the Interface or the autofac container into the TranslationServiceStringSource class to use to get the translated value?

user351711
  • 3,171
  • 5
  • 39
  • 74

1 Answers1

-1

I assume that somewhere you have some code similar to:

var container = builder.Build();
var resolver = new AutofacDependencyResolver(container);
DependencyResolver.SetResolver(resolver);

If you store that reference to the container somewhere, I often create a reference in the MvcApplication class (global.asax.cs):

public static IContainer Container { get; set; }

Then you can:

public TranslationServiceStringSource(string text) {
    _text = text;
    _translator = new MvcApplication.Container.Resolve<ITranslationService>(); 
}

Though it would be nice to get TranslationServiceStringSource to be managed by AutoFac.

sanbornc
  • 766
  • 5
  • 12
  • If you resolve directly out of the container, the instance will live as long as the container (which is probably for the lifetime of the application) – default.kramer Mar 20 '13 at 20:01
  • In the end we created an IValidationsService managed by Autofac which allowed us to pass in the translation service in the constructor. We couldn't have a static reference in the MvcApplication as this is contained in a separate project and getting TranslationServiceStringSource managed would be tricky as we are using an extension method on the business rules object to add the translations in. I'll give you this though as trying to get TranslationServiceStringSource managed by autofac led me to what I think is the best way to go, or at least the only way I can see. Many thanks for the help. – user351711 Mar 21 '13 at 18:49