Separation of Concerns with ASP.NET MVC

Beginner

2019-04-12

Separation Of Concerns with ASP.NET MVC

In this tutorial we will learn what is separation of concerns (SoC) and how ASP.NET MVC architecture enrich the SoC principle. If you didn’t read our previous tutorial about ASP.NET MVC architecture, I strongly recommend you to read : ASP.NET MVC Pipeline life-cycle – ASP.NET MVC Tutorials Part 1 and ASP.NET MVC Pipeline life-cycle – ASP.NET MVC Tutorials Part 2

What Is Separation Of Concerns

Separation Of Concerns ( SoC ) is a design principle that separates computer program into separate sections. Each section is responsible for one thing and does it well.

A section doesn’t know about each other sections behavior or inner implementation.

This achieves modularity among program sections through decoupling each section from another.

for example : in a web application, the HTML part is separate from logic parts which makes maintenance and testing simpler and easier.

MVC Architecture

as you saw in the previous tutorials, MVC mainly is split into three separate parts.

  • Model : which contains business data that the program will operate on. This data is concerned on how to store the user’s info, manipulate it and how to retrieve it when needed.
  • View : Each system needs an interface that enables the end user to deal with. MVC View achieves this be rendering the user interface part that will take action from the user and will show him the result of those actions.
  • Controller : This is the linking between the Model and the View. When an action is received from the View it’s parsed to the Controller, the Controller does some logic like checking if the user is allowed to do that action or not. Then it retrieved the data associated with that action from the Model part then send them to the View to show them to the user.

Each piece of the MVC architecture is well-defined and self-contained—this is referred to as the separation of concerns. The logic that manipulates the data in the model is contained only in the model; the logic that displays data is only in the view, and the code that handles user requests and input is contained only in the controller. With a clear division between each of the pieces, your application will be easier to maintain and extend over its lifetime, no matter how large it becomes.

What is Domain Model

The data we need to run the program on is called the model. A model might need some business rules or some technical rules to be applied on the data units.

those rules build together a system of abstractions called the domain model that describes how the data attributes will be stored and retrieved. it also provide an API to deal with the data.

Domain model usually is implemented into a separate assembly and is referenced through the MVC application. that’s how separation of concerns is done in big projects.

How MVC is Different than Other Patterns

MVC isn’t the only pattern that achieves the SoC concept. there are another patterns that may have less or more benefits or usage concerns.

Understanding the alternatives will make us have strong understanding of MVC itself.

How is ASP.NET Implementing the MVC

as we understood in the MVC piple-line tutorials we mentioned above the HTTP Request goes through different stages in ASP.NET MVC implementation

  • HTTP Request : the web server receives the HTTP request and parse it to the routing module
  • Routing : determines what resources to be loaded where are they located
  • Controller : once a route is mapped to controller receives the request and search and invoke the associated action.
  • Model : the action might do some data manipulation or fetch data needed for the presentation.
  • Model-to-Controller : data is sent back to the controller action to do some processing or to parse them directly to be viewed.
  • View : the view engine receives the data and instantiate the view template.
  • Render : the view result will be rendered and the response will be sent back to the user.

asp.net-mvc-soc

Smart UI Pattern

Smart UI Pattern is one of the most common used Patterns. If you used ASP.NET Web Forms or Windows Forms, then you dealt with the Smart UI Pattern before.

It’s used to build smart UI interface through dragging UI components into the page. Each component is associated with some events that are invoked when a change in the input is made e.g a keystroke is pressed or a drop down menu value is selected. the control components add code to intercept all these events and the developer write code handlers to handle the events.

Smart-UI-Pattern

This creates a monolithic application as we discussed in ASP.NET MVC Tutorials : Introduction

Smart UI is well suited or speed development of small projects and prototypes. Once you start adding more control components, the application gets complicated.  If you tries to add a new feature you will not be able to to so without breaking an existing one.

Code duplication also comes in mind when writing handlers for multiple event in Smart UI Pattern.

Model-View Pattern

You can consider the Model-View Pattern a good improvement for the Smart UI Pattern. Instead of having all the system in one layer with each component coupled to the other.

Data, Processes and rules are all in one layer as shown in the following image

model-View-architecture

Classic Three-Tier Architectures

The three-tier or three-layer pattern separates the persistence code from the domain model and places it in a new component called the ddata access layer (DAL).

Three-Tier-Architecture

The main difference between MVC and Three Tier architecture is A three tier architecture is that the client tier never communicates directly with the data tier In a Three-tier model all communication must pass through the middle tier

MVC architecture is triangular: the view sends updates to the controller, the controller updates the model, and the view gets updated directly from the model

Model-View-View-Model

It originated from Microsoft and is used in the Windows Presentation Foundation (WPF).

In MVVM model and view have bi directional way of communication

The only difference between MVC and MVVM is in MVC ViewModel allows data to be parsed from controller to view only while in MVVM model and view can communicate with each other in a bi directional way.

MVVM-Architecture

Summary

In this tutorial we understood what is the Separation of Concerns principle and how it applies to ASP.NET MVC architecture. we seen how MVC combines the most useful features from other patterns that was used before.

Resources

1- Wikipedia