1

I have C# solution with 5 projects in it

  1. Data Layer (class library with EF)
  2. Business Layer (class library)
  3. Presentation Layer (ASP.NET MVC)
  4. Common library
  5. MSTest project

I have my references setup like so;

  1. MVC references business layer
  2. Business layer references Data Layer
  3. All project references the common library

I've always have this obsession (OCD) of not having the presentation to have to reference EF and/or the Data Layer project because it should not know what my data layer is doing or how it's doing it's job. In my opinion it should just know which functions to call from the service layer and expect a return value. Maybe it is right and maybe it is wrong.

On my service layer I have a class called Services that has constructor injection like the code below so it can easily be tested using a mocking framework;

public class Services : IServices
{

    public Services(IDbContext dbContext, IBuilders builders)
    {
        _dbContext = dbContext;
        _builders = builders;
    }
}

So now - If I need to use this on my MVC project I'll have to have use DI to handle this situation so I don't have to tightly couple my controllers with my DbContext class. But this requires me having to reference my Data Layer from my Presentation Layer, is that an acceptable practice? What's a good way to handle this type of situation?

secretAgentB
  • 1,279
  • 4
  • 19
  • 36
  • 2
    This should answer your question: http://programmers.stackexchange.com/questions/300376/does-having-di-happen-in-the-composition-root-go-against-the-whole-point-of-depe – Yacoub Massad May 22 '16 at 22:19
  • I ve never thought about creating a class library and moving my controllers 1 layer behing the application entry point. Good thought indeed... but isn't that a little overkill though? Why do we have to jump through hoops just to follow the concept? – secretAgentB May 22 '16 at 22:33
  • 2
    Possible duplicate of [Ioc/DI - Why do I have to reference all layers/assemblies in entry application?](http://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-entry-application) – NightOwl888 May 22 '16 at 23:14
  • @PlatypusMaximus, you could simply accept the fact that you need to reference the data access project from the MVC project. But at least now you understand that you are doing this because the ASP.NET template kinda forced you to do it this way. I would love to a see a template for ASP.NET that has the composition root in a separate project and the other MVC stuff in their own project. – Yacoub Massad May 23 '16 at 09:31

0 Answers0