Late version of RegisterInstance in Unity

In our project, we use Unity as the basis for dependency injection. Now I am faced with a situation where I need a later version IUnityContainer.RegisterInstance().

In ninject, the code I'm trying to do will look something like this:

this.Bind<IMyInterface>().ToMethod(context => GetMyObjectLateBound());

In Unity, however, I have not yet found a way to do this.

The reason I need this is because by that time the instance IMyInterfacehas not yet been created, that the binding is in progress, and the creation of the object is also not processed by Unity (and I cannot replace it any time soon).

+3
source share
1 answer

Unity supports Lazy. You must add the following extension to unity:

unityContainer.AddNewExtension<LazySupportExtension>();

then you can do the following:

unityContainer.RegisterType<IComponent, Component1>(); 
var lazyComponent = unityContainer.Resolve<Lazy<IComponent>>();

, .

+5

All Articles