Thursday, March 11, 2010

MVC Architectural Pattern ( Part 1) : Basic Concepts

I have implemented MVC (or batter to say many varition) of MVC in few of my projects. MVC provide us a very strong archtitecture but its current understanding is very importent. MVC should not be confused with Tree Tier architecture. MVC is different and its has lots more. Ok Let me start my explaination….


MVC is an architectural pattern as opposed to desgin pattern. Architectural pattern has wider scope as compare to desgin pattern. Most common architectural patterns are :



1) MVC


2) MVP


3) Multitier Architecture ( 3 tier )


4) Peer to peer.

MVC concent was indtroduced by SmallTalk’s inventors Trygve M. H. Reenskaug. MVC contain tree major components


1) Modal : Connected to user’s mind


2) View : Connected to user’s eyes


3) Controller: Connected to user’s hands.

In simple terms View is what we see ( using eyes J ). Modal is our data ( which is in our mind ) and controller is our hand ( which controls the input). You can think of MVC as a sepration of your data from display where your data is not aware where it will be rendered. In pratical software desgin




1) Modal is our object represeting data. Modal are SMART.


2) View is visualization of state of model. View are DUMBS

 
3) Controler facilate the change of state of model. It takes user input. Controller are THIN






In a typical ASP .Net application View are our ASPX pages ( purley DUMB who just render the data ). Controller are our code behind ( which handle user input ) and modal are out businnes object.

MVC Pasive Mode



In MVC, each View must have a controller. Noramally contrller facilate the comminucation between view and model. Whenever there is a change in data , controller takes the responsibility of notifiing the view that data has been changed and you need to update yourself. That is normally done using direct call to view. In this Modal is isloated and its not ware about View and controller. But this approach has a problem if I have a multiple disaplay of same data ??? Think you are going to a builder and he show you different view of your dream house. The Blacony view ,Front door view. In a way he is showing same data ( our dream house ) but different angle. If data changes then all view must react and should change themselves.


The Varition

According to MVC communiccation between Model and View can be done using direct call or using observer desgin pattern . As I love desgin pattern so I will take observer as an example. In this View need to register them with model through controller. Whenever any changes happen in data the Modal notifies all its registered view about the change. In the notificaion ( in .Net we can say events ) its usual to passed the view who actually made that data change. Remember all user inputs are handeled by the controller.


This was the basic intro of MVC, in Part 2 I will try to explain MVC using .Net winform and ASP.Net implemtation. In Part 3 we will discuss about architectural benefits of MVC.