1
votes

I have a Translation class that takes in ITranslationService as its argument. How do I inject translation service when registering the Lazy<Translation> type? This is what I've done so far, but no luck.

public class Translation
{
    public Translation(ITranslationService translationService)
    {
        // code here
    }
}

container.RegisterType<ITranslationService, TranslationService>();
container.RegisterType<Lazy<Translation>>(new InjectionConstructor(typeof(ITranslationService)));

Error message when trying to resolve Lazy<Translation> type:

The lazily-initialized type does not have a public, parameterless constructor

3
"no luck" meaning what, exactly? (such as exact error message if you get that)hyde
Also you seem to have mismatched angle brackets...hyde
Could it be that you need to add ImportingConstructor as an attribute?Default

3 Answers

3
votes

First of all, Unity 3 now supports resolving Lazy<T>(See What's New section), so you don't need to do anything special, just register ITranslationService and you will be able to resolve Lazy<Translation>.

So the following only applies to Unity 2.

  • You can install this nuget extension from Piotr Wlodek. You will then need to enable it using:

    container.AddNewExtension<LazySupportExtension>();
    

    You will then be able to resolve Lazy<T> objects:

    var lazy = container.Resolve<Lazy<Translation>>();
    

    And the actual Translation object will not be constructed until you call lazy.Value.

  • If neither getting Unity3 nor that extension is an option, you could still try to manually configure Unity2 and resolve those objects.

    In your case, you need to use one of the constructors in Lazy<T> that receives a Func<T> as parameter. (That is a function with no parameters that returns an instance of T, or Translation in your case).

    You can use an InjectionFactory when registering Lazy<Translation> in Unity, which is a factory method that constructs a Lazy<Translation> object. The lazy object will receive in its constructor an initialization function that uses Unity to resolve the Translation:

    container.RegisterType<Lazy<Translation>>(
     new InjectionFactory(c => new Lazy<Translation>(() => c.Resolve<Translation>()) ));
    
1
votes

Unity supports Lazy<T>, but you have to configure it:

unityContainer.AddNewExtension<LazySupportExtension>();

then don't do

container.RegisterType<Lazy<Translation>(new InjectionConstructor(typeof(ITranslationService)));

but instead do:

container.RegisterType<Translation>();

And use it as follows:

unityContainer.Resolve<Lazy<Translation>>();
0
votes

For any one using MVC or Web API projects, just install the relevant Unity Bootstrapper Nuget package i.e Unity.AspNet.WebApi for Web API, and Unity.Mvc for MVC.

And then register your types as you normally would, either in code or through a config file. The Lazy instances will be injected automatically

private Lazy<IMapService> _mapService;

public HomeController(Lazy<IMapService> mapService)
{
    //Lazy instance is injected automatically.
    _mapService = mapService

}