依赖注入和解决方案中的多个项目 [英] Dependency Injection and multiple projects in solution

查看:89
本文介绍了依赖注入和解决方案中的多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注

I have been following this article concerning the use of the repository pattern and UnitOfWork with entity framework. I am also planning to use Ninject as my IOC container for an upcoming project.

鉴于本文的示例代码,NorthwindData项目中的NorthwindContext类实现了IUnitOfWork接口,该接口位于NorthwindModel项目内部.

Given the sample code from the article, the NorthwindContext class in the NorthwindData project implements the IUnitOfWork interface which live inside of the NorthwindModel project.

如何利用依赖注入消除NorthwindModel对NorthwindData的依赖?示例项目中的存储库类看起来好像它们将依赖IOC容器注入据我所知的NorthindContext实例.

How can I utilize dependency injection to eliminate the dependency on NorthwindModel to NorthwindData? The repository classes in the sample project look as if they would rely on an IOC container to inject instances of NorthindContext which I understand.

此外,我是否需要在解决方案中创建第三个项目来容纳DI物品.有关如何设置此示例的任何示例?

Also, Would I need to create third project in the solution to house the DI things. Any samples on how to set this up?

我想我上面的问题来自一个更笼统的问题.

I suppose my question above stems from a more general question..

使两个项目依赖于从NWData到NWModel的程序集引用是否不好?我的假设是DI将消除这种需求.

Is it bad to have the two projects dependent on an assembly reference from NWData to NWModel?? My assumption was that DI would elimnate this need.

推荐答案

要打破从NorthwindModel到NorthwindData的依赖关系,请查看NorthwindContext中存储库的需求.将其放入接口IDataContext中,在NorthwindModel项目中进行声明,然后让NorthwindContext实现它.现在使用DI将NorthwindContext注入现在仅依赖IDataContext的存储库中.

To break the dependency from NorthwindModel to NorthwindData look at what the repositories need from the NorthwindContext. Pull that into an interface, IDataContext, declare that in the NorthwindModel project and let NorthwindContext implement it. Now use DI to inject NorthwindContext into the repositories that now only depend on IDataContext.

关于可能的第三个项目:是的,让应用程序根项目只负责启动时的设置 是个好主意.但这取决于范围.

Regarding the possible third project: yes having an application root project responsible only setup at startup can be a good idea. But it depends on scope.

这篇关于依赖注入和解决方案中的多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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