问题
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