1

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.

stephen776
  • 9,134
  • 15
  • 74
  • 123

1 Answers1

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.

Christian Horsdal
  • 4,914
  • 23
  • 24
  • Not sure this really helps me...or maybe I am asking the wrong question. If NorthwindData has a class that implements an Interface from NorthwindModel, I would still need to add an assembly reference from NWData to NWModel to get the project to build...which is what I think I need to avoid...Maybe I am wrong about this?? – stephen776 Sep 08 '11 at 20:31
  • @stephen: It's okay to have NWData depend on NWModel. What you want to avoid is having your presentation layer depend on NWData, since it should be two "layers" away, and you want to only have each project touch layers adjacent to them. – StriplingWarrior Sep 08 '11 at 21:02
  • Would this not limit my ability to test each project individually? Or swap NWData out for another project that implements the interfaces in NWModel – stephen776 Sep 08 '11 at 21:04
  • Ah, I thought your question was the other way round. I would argue though that having NWData depend on NWModel is ok since the model be seen as the core of the app and data access as periphiral. This follows the Hexogonal architecture style – Christian Horsdal Sep 08 '11 at 21:05
  • the NWModel/NWData from the project is just my example. I am looking for advice in more general terms. A solution with multiple projects, and setting up DI to facilitate TDD and flexibility. – stephen776 Sep 08 '11 at 21:08