问题描述
我开始使用Ninject,在这个相对较小的项目,我遇到了一个问题:我有这个类
I started to use Ninject , on this relatively small project and i have run into a problem: i have this class
class SomeService : ISomeService
这取决于
class BizLogicModule : IBizLogicModule
这又依赖于
class DataRepository : IDataRepository
的
DataRepository
有一个构造函数,看起来像:
the DataRepository
has a ctor that looks like:
DataRepository(BizEntityModel context)
现在,我需要能够使用<$的单个实例C $ C> BizEntityModel 在多个 IDataRepository
实例。
我还需要创建 IDataRepository
沿的 IBizLogicModule
的人生。该 IBizLogicModule
不知道Ninject,我想保持这种方式。
Now, i need to be able to use a single instance of BizEntityModel
across more than one IDataRepository
instance.
I also need to create IDataRepository
's along the life of a IBizLogicModule
. The IBizLogicModule
does not know about Ninject and i want to keep it that way.
所以我的问题是:
如何连接所有,最多,使用Ninject内核,而
so my problem is:how to wire all that up, using the Ninject kernel, while:
-
不必通过周围的内核实例层。
not having to pass the kernel instance around the layers.
离开代码可读性接近它是什么之前,Ninject(使用工厂方法我只是new'ing)。
leaving the code readable close to what it was prior Ninject (i was just new'ing using a factory method).
接线我走到这一步的简单的部分是:
The simple part of the wiring i got so far is:
Bind<SomeService>().To<ISomeService>();
Bind<BizLogicModule>().To<IBizLogicModule>();
Bind<DataRepository>().To<IDataRepository>();
Bind<BizEntityModel>().To<BizEntityModel>(); //ToSelf()
// .WithConstructorArgument(context => Kernel.Get<BizEntityModel>)
您的指导是非常赞赏
编辑:作为你的答案谢谢!
这里的一些请求更多的数据:
BizEntityModel
登记与Ninject(代码更新)
Thanks for your answers!
here's some more data that was requested:BizEntityModel
is registered with Ninject (code updated).
如果我理解正确的:我可以创建 IBizLogicModule
采用工厂法 IDataRepository
的实例。但给我留下了:
1)我需要通过一个 BizEntityModel
来工厂方法,有时它麸皮新的,有时它的现有实例。使用工厂方法,它会重新创建每次之一。
2)这是一个问题, SomeService
在另一个装配,只有它有一个裁判Ninject。 dll的?
if i understand correctly: i can create instances of IDataRepository
in IBizLogicModule
using a 'factory method'. but that leaves me with:
1) i need to pass a BizEntityModel
to the factory method, some times its bran new and sometimes its an existing instance. using the factory method, it will create anew one every time.
2) is this a problem that SomeService
is in another assembly, and only it has a ref to Ninject.dll ?
推荐答案
你是否Ninject注册BizEntityModel?如果是这样,你应该能够告诉Ninject提供一个BizEntityModel之一,只有一个实例为容器,甚至程序的生命周期的每个请求,而无需定义和注册BizEntityModel的传统的单一实例。即使你有一个工厂方法来工作,Ninject不会让你单身,作用域registraion,如果你有你渴望加载的对象,然后注册该实例的依赖作为一个单身。
Do you register BizEntityModel with Ninject? If so, you should be able to tell Ninject to supply one and only one instance of a BizEntityModel for every request for the lifetime of the container, or even the program, without having to define and register a traditional singleton instance of BizEntityModel. Even if you have to work with a factory method and Ninject won't let you singleton-scope that registraion, if you have to you can eager-load the object and then register the instance for the dependency as a singleton.
IBizLogicModule永远都不应该知道Ninject; Ninject应该知道BizLogicModule。尝试创建一个IDataRepository注册,将提供一个工厂方法(工厂范围的这样一个新的实例每次调用创建的),然后将该工厂方法作为依赖于IBizLogicModule,当它需要创建IDataRepositories将使用它。你基本上是通过国际奥委会的分辨能力传递给在IBizLogicModule提供的工厂类。如果你这样做了很多的不同IBizLogicModule类类型的,你基本上建立一个服务定位器,我会亲自避免,但一个或两个是完全有效的工厂/造物主模式。
IBizLogicModule should never have to know about Ninject; Ninject should know about BizLogicModule. Try creating an IDataRepository registration that will provide a factory method (factory-scoped so a new instance is created per call), then pass that factory method as a dependency to IBizLogicModule, which will use it when it needs to create IDataRepositories. You're basically passing through the IoC's resolution capabilities to provide a factory class in IBizLogicModule. If you do that for a lot of different class types on IBizLogicModule, you're basically creating a service locator which I would personally avoid, but one or two is a perfectly valid Factory/Creator pattern.
这篇关于如何保持IoC容器在一个地方,而内部类需要建立依赖关系beeing构造之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!