In our project, we use Unity as a dependency injection framework. Now I am faced with a situation, where I need a late-bound version of IUnityContainer.RegisterInstance()
.
In ninject, the code I am trying to do, would look something like this:
this.Bind<IMyInterface>().ToMethod(context => GetMyObjectLateBound());
In Unity, however, I have not found a way of doing this, so far.
The reason, why I need this, is that the Instance of IMyInterface
is not created yet by the time, that the binding happens and the object creation is also not handled by Unity (and I cannot replace this anytime soon).
Unity supports Lazy. You need to 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>>();
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.