Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » Enterprise Development » Implementing MVC Design Pattern in .NET

Implementing MVC Design Pattern in .NET

This article explains the basic concept of the Model View Controller (MVC) design pattern and also shows how how closely .NET Framework can be used to implement the MVC design pattern with the one that originally conceived.

Page Views : 10264
Downloads : 282
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
MVCDesignCSharp.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Introduction

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.

Prerequisites

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:

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:

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:

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.

  • Since MVC handles the multiple views using the same enterprise model it is easier to maintain, test and upgrade the multiple system.

  • It will be easier to add new clients just by adding their views and controllers.

  • Since the Model is completely decoupled from view it allows lot of flexibilities to design and implement the model considering reusability and modularity. This model also can be extended for further distributed application.

  • It is possible to have development process in parallel for model, view and controller.

  • This makes the application extensible and scalable.

Drawbacks:

  • Requires high skilled experienced professionals who can identify the requirements in depth at the front before actual design. 

  • It requires the significant amount of time to analyze and design.

  • 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. 

Sample Code:

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. 

Code walkthrough:

Typed DataSet (Business Entity): 

This is the XML order xml schema, which represents the typed dataset for Orders.

ASPX:

MVCDesign.aspx webform displays two different views for the same dataset.

protected MVCDesignCSharp.XMLData.Orders ordersDataSet;
private
void Button1_Click(object sender, System.EventArgs e)
{
BizOrderManager.GetOrderList(ordersDataSet);
if(DataGrid1.Visible==false)
{
DataList1.Visible =
false;
DataGrid1.Visible =
true;
DataGrid1.DataBind();
Button1.Text="Show View 1";
}
else
{
DataList1.Visible =
true;
DataGrid1.Visible =
false;
DataList1.DataBind();
Button1.Text="Show View 2";
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
BizOrderManager.GetOrderList(ordersDataSet);
DataList1.Visible =
true;
DataGrid1.Visible =
true;
DataList1.DataBind();
DataGrid1.DataBind();
Button1.Text="Show Only View 2";
}

Business Objects:

This is the component method, which can be requested by controller. This business component can interact other data access components.

public static void GetOrderList(DataSet OrderDS)
{
OrderDS.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "/XMLData/Orders.xml");
}

DataSource:

For easy configuration and simplification I have used XML files as data source for this application.

How to Run this Code:

  • Unzip the MVCDesign.zip

  • Build the solution MCDesign.sln

  • Run the application.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
nimesh panchal
Currently working as an independent consultant in New York City. Overall 5+ Years of experience in Software development having expertise in C#/VB.NET/ASP.NET /VB/JAVA/C++ and SQL Server/Oracle database 1.5 Years of experience in Microsoft .NET Technology. Main areas of expertise also includes analysis and designing application architecture using Design patterns, use cases, class model diagrams, database diagram, work flow for business functionalities and other software engineering concepts and models. 4+Years of solid industrial experience working with Microsoft technology for n-Tier E-Commerce and Client-server applications. Good hands on development tools like VisualStudio.NET, Visio, SQL Server, Oracle, Microsoft Project and many other tools.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.