开发者

Intercepting injection

I am injecting repositories into a class, and once I've injected the repositories, I'm assigning my context to each of the repositories so I have my unit of work.

What I'm trying to figure out is, is there a way for me to automatically assign my Unit of Work to each repository as I inject it so that a developer doesn't have to consider this when setting up their code. I've already got my unit of work configured in my base class, the one the developer will be inheriting from.

Can I do something like;

Bind<I>().To<S>().WhenInjectedInto<IBaseClass>( i,b => { i.UnitOfWork = b.UnitOfWork });

But not have to repeat that pattern every time?

[UPDATE] I'm looking at ways to figure out if Ninject is injecting and what from into, https://github.com/ninject/ninje开发者_如何学编程ct.extensions.interception

I'm trying to look through the tests to see if this is far off base. Any recommendations?


I think the better way would be to inject the context into the repositoies using constructor injection. In a web project you can use InRequestScope for the context binding. For a WPF/WinForms/Console application have a look at Ninject.Extensions.NamedScope. You can define that a single context is used for all dependencies of you IBaseClass.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜