Implementing MVC Design Pattern in .NET
Design patterns are very useful to solve complex design problems if used properly. This article explains the basic concept of the Model View Controller (MVC) Design pattern and also shows how closely .NET Framework can be used to implement the MVC design pattern with the one that originally conceived.
Object Oriented-programming concepts. NET Framework, ADO.NET, C#, XSD.
Definition/Description: Model View Controller (MVC)
View renders the data from the Model in response to the request made to the model by controlled events made by user interaction.
Model View Controller is a design approach to separate the application object model from GUI, originally invented around 80s. Then later on it has become a widely accepted common design pattern. The main objective behind this pattern is to decouple the view of the data (presentation layer) from the actual data processing so that the same model can be used for various views. This is achieved by using three different types of objects that interact with each other in loosely coupled manner with their discreet set of tasks.
These three objects are known as Model, View and Controller. We will learn for each of them here.
View is the graphical data presentation (outputting) irrespective of the real data processing. View is the responsible for look and feel, some custom formatting, sorting etc. View is completely isolated from actual complex data operations. For example, Online product catalog view is completely separated from database connection, query, tables etc. It simply gets final row-data from the model and puts some cosmetics and formatting before displaying it in browser. View provides interface to interact with the system. The beauty of MVC approach is that it supports any kind of view, which is challenging in todays distributed and multi-platform environment.
A MVC model can have multiple views, which are controlled by controller. View interface can be of WEB-FORMS, HTML, XML/XSLT, XTML, and WML or can be Windows forms etc.
Model is responsible for actual data processing, like database connection, querying database, implementing business rules etc. It feeds data to the view without worrying about the actual formatting and look and feel. Data provided by Model is display-neutral so it can be interfaced with as many views without code redundancy; this eases your code maintenance and reduces bugs and allows code -reuse at good extent. Model responds to the request made by controllers and notifies the registered views to update their display with new data.
Controller is responsible for Notice of action. Controller responds to the mouse or keyboard input to command model and view to change. Controllers are associated with views. User interaction triggers the events to change the model, which in turn calls some methods of model to update its state to notify other registered views to refresh their display.
Benefits:Following are the few of the benefits of MVC design pattern.
This makes the application extensible and scalable.
This design approach is not suitable for smaller applications. It Overkills the small applications.
.NET and MVC:
Lets try to find out how closely ASP.NET allows architecting application to meet MVC concept.
Model: Dataset/Business Entities, DataAccess components
View. ASPX pages
Controller: Code-behind .vb/.cs files
Typed dataset is one of the greatest features in .NET, which holds the application data in memory and represents the application business entity model, which bridges the UI components with Service Interface and Data Access layer.
So, service interface and Data Access components populate these typed Datasets. Now, these filled datasets can be bound to any view in UI layer.
ASPX and ASCX pages server as view and mean to interface with application, while the code behind classes for these files server as the controller functions.
ASP.NET doesnt have central controller function, instead the code-behind file can directly make request to the Model and can update the dataset. But lot of controller function can be implemented in code behind class events in .aspx or .ascx pages, like Page_Onload(), OnItem_Created() etcevents.
NET Framework has all Object-oriented features like code reuse, encapsulation etcSo, proper design of your model can make your user interface and view completely separated from model which can server multiple views.
The following sample illustrates the MVC design using .NET Framework with C#. This application retrieves the Orders from the database (we used XML File as data source) in typed dataset. This example has two different kinds of views for displaying orders on webform for the same Model using Web.UI controls.
Typed DataSet (Business Entity):
This is the XML order xml schema, which represents the typed dataset for Orders.