Dependency Injection and multiple projects in solution

拈花ヽ惹草 提交于 2020-01-24 23:27:19

问题


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.

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.

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.

Also, Would I need to create third project in the solution to house the DI things. 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?? My assumption was that DI would elimnate this need.


回答1:


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.



来源:https://stackoverflow.com/questions/7353866/dependency-injection-and-multiple-projects-in-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!