Extented separation of concerns and architecture of ASP.NET MVC 5 projects (Divide and conquer)

Most of the projects start as small projects and with time they turn into medium or even large projects. By the time that we realize this is happening, it might be too late and can cost us a lot to change the project architecture.

The separation of concerns is often neglected topic when dealing with web applications. Everyone who has created even once a basic web app knows that by default .NET has a decent template. Of course these templates follow the classic MVC architecture, which divides the application into three parts:
Model > View > Controller
That's okay for starter and for people that are new to these concepts, but if you want to create easily maintainable and extendable application you should absolutely put some effort to extend the basic MVC template. The aim of this blog post is to share a possible example of template extention.

Sample archicture and separation of concerns in ASP.NET Web Application

This extented template and more are available for download here .

What does this architecture give you more than the usual boilerplate templates?

(Those advantages are applicable for medium to large projects)
  • Easier development - one look at the project structure tells you where all different components are situated
  • Lower barrier for understanding, when new people are working on the project - when introducing new people to some project that is already in progress it's always better to do that step by step. And when these components are well structured and neat it is easier to go through them
  • Improved maintenance - whether you have created the project from scratch or not, when a problem occurs it's a lot easier to define where this problem comes from
  • Easier extension of the project - having all the components separated makes it a lot easier to extend them

Detailed explanation of the architecture

  • Project for the database tables - contains the Db Context and the configurations of the migrations
  • Project for abstraction levels over the database - contains implemention of different abstraction levels over the database like Unit of work and Repository pattern
  • Project for the database models - contains all the db models including these from the ASP.NET identity which are moved from the web project
  • Project for the services related to the database access - these services are not Web Api services, or any other ASP.NET services. They are just called like that. They might be called 'Providers',or whatever descriptive name you like. They are just regular classes plus interfaces that are responsible for the containment of the business logic of the application as well as the tasks related to CRUD operations over the database.
  • Project for the services related to the web operations - the same thing is valid here, but here are the services related to more complex logic that is not related to the database like Caching services, Email services and etc.
  • Project for the services tests - just to keep the different types of tests separately
  • Project for the controllers tests
  • Project for the routes tests
  • Here different console apps like Crawlers, Importers and etc. might be created
Web Tools
  • Project for the web application - this is the classic MVC project which is stripped of the Db context dependency as well as the db models dependencies . Contains configurations for the Dependency Injection, Automapper and other external libraries. The architecture is supposed to keep all your contoller actions very tiny and do all of your business logic in the services, which are injected by your IoC (inversion of control) container. In our case this is Autofac
  • Project for the web application infrastructure -this project contains source code which is related to the Web application, but might be reusable, and this is why it is separated by the Web App. For example here we can put our View Models, custom filters and annotations, html extention methods and helpers, and etc.
Lastly there is a separate project intendted to combine all the common classes between all the projects like Global Constants, Global Helper and etc.

So if you like this example architecture, and prefer not to start from scratch and save a couple of hours in boring configurations, don't hesitate and download it here .