问题
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