0

I want to separate my ASP. NET MVC 5 + WebApi2 solution into separate logical projects, so (in my head) I have:

  • Data.csproj
    • references EF6 and handles Code First migrations
  • Models.csproj
    • references Automapper
    • refrences Data (above)
  • Services.csproj
    • references Models (above)
  • Web.csproj
    • references autofac
    • references services above

But I can't get my real project to look like that because

  1. Identity sprinkles the model and EF references all over my Web.csproj
  2. When I configure Autofac in Web.csproj and try to register my DbContexts and whatever other dependencies are in my other projects, I will need access to the concrete types, so Web will need to reference all other projects as the DI is setup in Web?

This is a brand new project auto-generated by the ASP .NET template. Thanks.

Ali
  • 1,462
  • 2
  • 17
  • 32
  • My point 2 is related to: http://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-entry-application, So I guess it is okay to have all assemblies referenced by web, because that is the composition root? – Ali Jan 22 '16 at 16:03

1 Answers1

0
  1. Generally, you avoid getting your Entity-Framework pollution into your web code by not referencing your data-models in your web project. If you put interfaces for the models in a separate infrastructure project, for example, you won't have that problem any more. Your 'services' can return abstract types with no dependency on EF and coupling is reduced.

  2. Personally, I like to get around this problem wither with a separate project that is responsible for factory code or (even better IMO) giving each project responsibility for constructing its own objects. Having the factory code in the same place further reduces coupling and can make refactoring easier.

One more thing...

If this is a new project, why do you even need a DI container. You could always use poor man's dependency injection and refactor later when you have a better idea of your needs. They are often overused or used as a crutch to hide overly complex lasagna code. It is an incredibly useful and powerful technology, but most of the benefit in terns of flexibility can also be realised through well designed factories and builders. These can have the additional benefit of increased readability.

Max
  • 721
  • 5
  • 8
  • I think i will need some more concrete references to how to move ASP NET Identity data and application context to its own project. Also also how to move Autofac startup code to a separate project outside of Web UI-- if that is what you are suggesting. – Ali Jan 22 '16 at 15:52