使用Unity IoC时如何实现Ninject .InSingletonScope() [英] How can I implement Ninject .InSingletonScope() when using Unity IoC

查看:84
本文介绍了使用Unity IoC时如何实现Ninject .InSingletonScope()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用了ninject IoC,尤其是以下内容:

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

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

我想使用Unity IoC来实现这一点,但是有人可以告诉我 我如何使它相同,这又意味着"InSingletonScope()"是什么意思? 以下工作有效,但我担心由于可能需要指定Singleton而无法正确完成工作.

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>();

推荐答案

Unity使用LifeTimeManager的概念.它带有一个称为ContainerControlledLifetimeManager的单例LifeTimeManager.您将按以下方式使用它:

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:

"InSingletonScope()"又是什么意思?

also what does it mean "InSingletonScope()" ?

在IoC容器(例如Ninject和Unity)的上下文中,单例对象是每次请求时都相同的对象.在您的示例中,每次您要求容器解析一个RepositoryFactories对象时,它始终是同一对象.不是新实例.

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.

这篇关于使用Unity IoC时如何实现Ninject .InSingletonScope()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆