简体   繁体   English

依赖注入和解决方案中的多个项目

[英]Dependency Injection and multiple projects in solution

I have been following this article concerning the use of the repository pattern and UnitOfWork with entity framework. 我一直在关注这个关于使用存储库模式和用的UnitOfWork实体框架的文章。 I am also planning to use Ninject as my IOC container for an upcoming project. 我还计划将Ninject用作我即将进行的项目的IOC容器。

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. 给定文章中的示例代码,NorthwindData项目中的NorthwindContext类将实现IUnitOfWork接口,该接口位于NorthwindModel项目内部。

How can I utilize dependency injection to eliminate the dependency on NorthwindModel to NorthwindData? 如何利用依赖注入消除对NorthwindData到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. 示例项目中的存储库类看起来好像它们将依赖IOC容器注入我了解的NorthindContext实例。

Also, Would I need to create third project in the solution to house the DI things. 另外,我是否需要在解决方案中创建第三个项目来容纳DI物品。 Any samples on how to set this up? 有任何样本如何设置吗?

EDIT: 编辑:

I suppose my question above stems from a more general question.. 我想我上面的问题来自一个更笼统的问题。

Is it bad to have the two projects dependent on an assembly reference from NWData to NWModel?? 让这两个项目依赖于从NWData到NWModel的程序集引用是否不好? My assumption was that DI would elimnate this need. 我的假设是DI将消除这种需求。

To break the dependency from NorthwindModel to NorthwindData look at what the repositories need from the NorthwindContext. 要打破从NorthwindModel到NorthwindData的依赖关系,请查看NorthwindContext中存储库的需求。 Pull that into an interface, IDataContext, declare that in the NorthwindModel project and let NorthwindContext implement it. 将其拉入IDataContext接口,在NorthwindModel项目中进行声明,然后让NorthwindContext实现它。 Now use DI to inject NorthwindContext into the repositories that now only depend on IDataContext. 现在使用DI将NorthwindContext注入现在仅依赖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. 但这取决于范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM