How to implement Seperation of Concern in a MVC Application

We always try to write easy code so that our works get faster but in long run it doesn't help us rather than it creates Maintenance issue when you get a problem and you don't know where to solve it. Also from a Security point of view we need to see whether our application is Loosely coupled or do we use separation of concern or not.

How The Architecture Should be ?

In order to achieve the separation of concern we need to look at the following components in MVC

Use of Separation of Concerns,
Use of Code First approach,
POCO objects,
Repository Pattern,
Dependency Injection and Inversion of Control.

We need to create following layer in our project
CodeFirstData layer.
CodeFirstEntities layer.
CodeFirstServices layer.
MVC Web layer.

Lets see what these layers Mean to us

1. CodeFirstData layer

The data layer is the class library defining a layer which is responsible for interacting with database, contains context classes and a factory pattern implementation to interact with database. The layer contains the repository for each entity to map with database, thus making a complete ORM (Object Resource Model) solution. The class library references EntityFramework DLL to implement the dbcontext classes.

2. CodeFirstEntities layer

Entity layer acts as a model to MVC application, and is also responsible for the creation of DataBase objects when the dbset is first executed. It contains Entity classes in POCO form, having relations and data annotations(Rules/Constraints to be put on Database table/columns).

The properties of the class results in column name in database and name of the class in Database table. The primary key is either defined by the property named Id or "Order[Id]". In our case it is "ProductId" and this is the default protocol set by the entity framework to keep in mind while creating entities. Since this application is code first, we need to create entities first.

3. CodeFirstServices layer

The layer contains services which uses repositories to fetch data from database. The interaction between Services and Repositories is kept loosely coupled thus implementing Inversion of Control using Dependency Injection. It is constructor based dependency injection and does not allow service to make a direct instance of our repositories. Service layer acts as an interface between controllers and repositories, and passes the requests of a controller to repositories.

4. MVCWeb layer

MVCPortal layer is our UI layer, it contains Model/ViewModels, Views and Controllers. I am not going into details of MVC as it is not our primary target. I assume that you already know how to create and run an MVC application. Let's have do quick revision of MVC.


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: