Home Proper way to architect MVC application with EF and Dependency Injection
Reply: 1

Proper way to architect MVC application with EF and Dependency Injection

Matt J
1#
Matt J Published in 2018-01-12 15:44:06Z

I'm starting a new Enterprise application for the first time in a while, so I want to make sure I get the architecture correct right from the get go. I plan to use MVC and EF (not Core for either, at this time), as well as Unity for Dependency Injection. This application will be an extension of sort to an already existing 3rd party app + database. EF will be database-first.

Most of this is fairly new to me, so hopefully I get all the terminology correct.

I've been reading countless articles on how to properly organize everything, but a lot seems to be somewhat contradictory. Some common themes are

  • Do not use the Repository Pattern with EF, since EF is already basically a repo
  • Separate app into layers
    • Data Layer (dbContext, EF POCOs)
    • Business Layer (business logic)
    • Service Layer (mapper between business and presentation)
    • Presentation Layer (thin controllers)

My current plan is to have two separate projects

  1. MyApp.Business which contains

    • all EF generated objects (DbContext and entity partial classes)
    • extra partial classes to add functionality
    • business logic classes
    • injecting connection string information into "MyDBContext"
  2. MyApp.Web which contains

    • all MVC classes (views, controllers, view models, etc)
    • Service classes that call business logic, and transform between BL/EF models and view models
    • injecting "MyDBContext" and business layer classes into the services
    • injecting the services into the controllers

Is this the right way to design this?

My concerns:

  1. The business logic seems too coupled to EF. How do I de-couple this without essentially creating a repository? Do you just assume you won't switch to NHibernate or even a non-ORM DAL?

  2. How do I set up the dbContext in a way that lets me test business logic with mock data? Examples I've seen all over have the system injecting "MyDBContext" type objects, and not interfaces.

  3. I combined the DAL and Business Layer so that I can make use of partial classes with the models. That ok?

Ishara Madawa
2#
Ishara Madawa Reply to 2018-01-12 16:02:43Z

As my experience, i use Simple injector as DI container and use Repository pattern and UnitOfWork pattern with DB layer in Web API 2 and MVC projects. Also you can find lots of examples and community support

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312112 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO