How can I implement Ninject .InSingletonScope() when using Unity IoC

浪子不回头ぞ 提交于 2019-12-06 11:20:05

问题


I was using ninject IoC in my application and in particular the following:

kernel.Bind<RepositoryFactories>().To<RepositoryFactories>()
   .InSingletonScope();

I would like to implement this using the Unity IoC but can someone tell me how I can make it the same and also what does it mean "InSingletonScope()" ? The following works but I am worried that it's not being done correctly because of the Singleton that maybe needs to be specified.

container.RegisterType<RepositoryFactories, RepositoryFactories>();

回答1:


Unity uses the concept of LifeTimeManager's.. it comes with what is essentially a Singleton LifeTimeManager called ContainerControlledLifetimeManager. You would use it as below:

container.RegisterType<RepositoryFactories>(new ContainerControlledLifetimeManager(), /* other params */);

I'm unsure if you are asking what a Singleton is or not in your question:

also what does it mean "InSingletonScope()" ?

In the context of an IoC container such as Ninject and Unity, a Singleton is an object that is the same each time you request it. In your example, every time you ask your container to resolve a RepositoryFactories object.. it will always be the same object; not a new instance.




回答2:


According to this you should use ContainerControlledLifetimeManager. Your registration will be:

container.RegisterType<RepositoryFactories>(new ContainerControlledLifetimeManager())


来源:https://stackoverflow.com/questions/15049583/how-can-i-implement-ninject-insingletonscope-when-using-unity-ioc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!