Wednesday, April 4, 2012

Model-View-Controller (MVC)

The Model-View-Controller (MVC) pattern separates the modeling of the domain, the presentation, and the actions based on user input into three separate classes.
MVC is not just a design pattern while its an architecture because its responsible for whole application not a part of application.

How is it beneficial?
1. User interface may change frequently, and if user interface and business logic combined in single object then every time you need to modify an object that contains business logic, this can introduce errors and require retesting of all business logic every time user interface changes.
2. In some cases application displays same data in different ways at same time, like a analyst may like spreadsheet view while management prefer charts(bar/pie/column) view, and If user updates one view then application must update all other views automatically.
3. UI design requires different skill set than developing business logic, rarely a person have both skill set, so its required to separate these to parts.
4. UI code is more device dependent than business logic. If you need to make a application mobile enabled then it require much UI design changes while business logic has no effects, a clean separation of these two parts accelerates the migration and minimize the risk of introducing errors in business logic.
5. Creating automated testing interfaces for UI is more difficult than business logic. So reducing the code that is tied with UI enhances the testability of UI (and application).  

Model - Manages behavior and data of application domain, respond to the request for it's state(usually from view), and respond to the request to change it's state(usually from controller).
View - Manages display of data.
Controller - Interprets user inputs from keyboard/mouse and passes them to View and/or Model.

The Model is depend on neither the View nor the Controller. Controller has been used to mean different thing in different context. In web applications controller is well defined its server side component that handled http request from client side(View).

The basic fundamental concept of MVC is separation of UI and business logic.

Steve Burbeck describes two models of MVC
- Active model
- Passive model

Passive model - This model is used when Controller changes state (updates) of model and notifies to View(s) that the model has been changed and View(s) should get data and refreshed from Model.

Active model - This model is used when model get changes state (updates) from external sources without involvement of controller, so only model knows about it's updates. In this case model must notify the View(s) to that state changed so get refreshed from model.


One of the motivation of using MVC is that Model is independent from Views and Controller so you may need to reintroduce the dependency, but fortunately by using Observer pattern no need of it.
Any View can implement observer interface and register with model to get notification from model of state change. Controller also need to implement Observer interface and register with model to get notify if Model updated by external sources. Model maintain list of Controller and registered Views and Whenever Model gets updated from other sources Model iterates the list and notify Views and Controller about state change so Views can get data and refreshed from Model.

Benefits
1. Testing - Separation of UI to Business logic and database access reduces test cases. Separated components Model, View and Controller can be tested independently and easily.
2. Supports multiple Views - Model is independent from Views so that multiple Views can use same model objects to display data at same time. Ex. Same data can be displayed as on grid, on spread sheet or on labels on different pages at same time.
3. Accommodates changes - User interface may require rapidly changes like font, color, layouts. Changes, adding new layouts or views to system does not effect the model because of in-dependency of Model.

Summaries Advantages
1. Extensive support for TDD(test driven development).
2. Complex applications can be managed easily.
3. Views are light weight because they do not use View State.
4. Separation of concerns, different aspects of the application can be divided into Model, View and Controller.
5. Easy and full control over rendered HTML.

Liabilities
1. Complexity - MVC introduces new heights of indirection so increases complexity slightly.
2. Cost of frequent changes - If Model getting frequent changes then developers of model must have View's nature in mind, so that model can respond to frequent requests of Views.

Terminology
1. You should not use server controls and should not write code in Views because views is only for getting and displaying data according this pattern. Putting server controls and code in Views disables unit testing, re-usability of code.
2. All code that you put in aspx.cs file will be shift to the controller.
3. We uses HTML controls in Views so that we can use MVC's HTML helper classes to write HTML controls easily.
4. MVC does not use server based forms and view state.
5. It processes web application requests through a single Controller.
6. It works well for Web applications that are supported by large teams of developers and Web designers who need a high degree of control over the application behavior.
7. In windows applications we can consider MVC as
- The OOP infrastructure is the Model
- The Forms are the Views
- The event handlers are the Controller
8. Visual studio 2008 does not include MVC, you need to download and install it. All editions of Visual studio 2010 include MVC. 

Learn working with MVC step by step

Step - 1 http://www.youtube.com/watch?feature=player_embedded&v=KAKxm4eQP24
Step - 2 http://www.youtube.com/watch?v=Fu9v2MIDlTA&feature=player_embedded
Step - 3 http://www.youtube.com/watch?v=0-UdqWy9lVc&feature=player_embedded
Step - 4 http://www.youtube.com/watch?feature=player_detailpage&v=1dlxtHuRw34
Step - 5 http://www.dotnetfunda.com/articles/article1415-how-to-create-mvc-model-view-controller-views-faster-by-using-html-helper-.aspx
Step - 6 http://www.dotnetfunda.com/articles/article1429-how-can-we-do-unit-test-in-mvc-model-view-controller-application-tutoria-.aspx
Step - 7 http://www.dotnetfunda.com/articles/article1485-what-is-mvc-model-view-controller-routing-tutorial-number-7-.aspx
Step - 8 http://www.dotnetfunda.com/articles/article1527-how-to-validate-data-provided-in-mvc-urls-mvc-tutorial-number-8-.aspx
Step - 9 http://www.dotnetfunda.com/articles/article1596-how-to-create-mvc-outbound-urls-mvc-tutorial-number-9-.aspx

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...