1

I have a Visual Studio solution that has the following:

  • MyProject.Domain - POCOs.
  • MyProject.Data - ORM (Entity Framework DbContext).
  • MyProject.Services - Wrapper around the DbContext. Contains classes that perform business logic.
  • MyProject.Web - MVC app that uses the service layer.

For my MVC app, I am using Ninject to inject services into my MVC controllers. The binding in my MVC project looks like this:

kernel.Bind<ISomeService>().To<SomeService>();

This part is working fine. However, I would further like to inject an IDbContext into my services, and the examples I see online are a little confusing. The examples I see online look make the above code look like this:

kernel.Bind<ISomeService>().To<SomeService>();
kernel.Bind<ISomeDbContext>().To<SomeDbContext>();

Now, since SomeDbContext is located in MyProject.Data, this means that I have to add a reference to the ORM project in my MVC project. This works, but this seems to defeat the purpose of IoC. The MVC project shouldn't "know" about the ORM, right? Why isn't this considered poor design? What is the proper way to manage this dependency?

anon
  • 4,578
  • 3
  • 35
  • 54
  • 1
    @Steven - This is indeed a duplicate. The link you provided answers my question (especially your answer!). I am voting to close. Thank you! – anon Jan 20 '14 at 21:31

1 Answers1

1

If you configure all your dependencies in the MVC project, it is needed to have reference to project that holds implementations.

I am OK with this solution and if it is really needed to avoid this kind of reference you can try the following approach.

I am not familiar with Ninject, but in CastleWindsor it is possible to write installers anywhere (within any assembly) and then invoke them during type registration process from your MVC project. There is a SO question that might help you: Convert this Castle Windsor Installer to Ninject to register all repositories.

There won't be dirrect reference to ORM project from MVC project if you add a project with "installers" that knows about ORM project and its interfeces. In this case your MVC project should know about DataAccess layer interfaces and the project with installers only.

Community
  • 1
  • 1
Ilya Palkin
  • 14,687
  • 2
  • 23
  • 36