Model View Controller (MVC ) Architecture


This article gives an overview of the Model View Controller (MVC) architecture and shows how you can implement it with the help of a specific scenario.

Download sample project

Model View Controller

Model view controller is nothing but a design pattern used to achieve customizability in our application. Change is the only thing in the world, which will never change. All the products that we develop to our clients will undergo many changes. To accommodate these changes we should concentrate more on our design. Directly jumping in to the code may give quick solutions to our problem but they will not solve our future problems of customizability and re-usability. So friends in this article we will discuss about MVC a most popular design pattern, which helps us to overcome most of our problems. Initially we may feel that this design pattern will need more time before starting the development. Yes it is true, but the time, which we spend on design, will give some fruitful benefits.

Good Layering Approach

MVC follows the most common approach of Layering. Layering is nothing but a logical split up of our code in to functions in different classes. This approach is well known and most accepted approach. The main advantage in this approach is re-usability of code. Good example of layering approach is UI, Business Logic, and Data Access layer. Now we need to think how we can extend this approach to give us another great advantage customizability. The answer to this is using Inheritance. Inheritance is one of the powerful concepts in oops. .Net supports this in a nice way.

Visual Inheritance in .Net

Before getting in to Visual Inheritance, we will discuss the basic difference between Inheritance and Interfaces.

Inheritance -- what it is?
Interfaces -- How it should be? (It’s a contract)

Both these answers target the classes, which we are designing in our application. Many of us know this definition but we will not use this extensively in our application including me. Unless we work in a design, which uses these concepts to a great extent, we will not agree this. Many of the Microsoft products use this to a great extent. For example how Commerce Server pipeline components achieves the customizability. Commerce Server allows us to create our own component in com and allow us to add it as one of a stage in the pipeline. How they achieve this, only through interfaces. We need to create a com component and the com component should implement a interface. All the objects in our .Net Framework class libraries are inherited from System.Object. This tells us the importance of Inheritance.

Jumping into Visual Inheritance

Let's try out one of .Net most interesting feature, Visual Inheritance. What exactly is it? Now we do know what is inheritance right? No I don't mean the money that you get from your parents/grand parents, I'm talking about class and interface inheritance, which saves you the headache of re-typing code and provides the luxury of code reusability. Now wouldn't it be nice if the same feature that applies to classes and their methods could also be applied to the GUI forms that we create, I mean create a base form with our corporate logo so that it appears on all of the firm's screens? Well such a feature now exists! Yes, you've guessed it, its Visual Inheritance! Visual Inheritance alone will not solve our problem of extensibility. This will help us to start digging our mind for solution. Cool guys now we will directly jump in to our problem with a good example.

Problem Statement

I have an employee master screen, which takes Name and Age as inputs and save the same in our database.



Lets assume this is my basic functionality in my product, which serves most of my customer’s requirement. Now let us assume one of my new client asks for a change in this form. He needs an additional field in this form, which takes the employee’s address and stores the same in the database. How will we achieve this? Normally we will create a new screen which will also has a textbox to get the address input, in the same time we will also add another column in the employee master table and supply the new binaries and modified table and stored procedure scripts to the client. Do you think this is the right approach? If you ask me this is one of the crude way to satisfy an customer. Requirement like this will be very easy to re-write the entire code. But think if a customer asks a change in Payroll Calculation logic. Re-writing code for some specific customers will not helps us in long run. This will end up in maintaining separate – separate Visual source safe for each and every client. These types of solutions are very difficult to handle and after some time we will feel that we have a messy code.

The right way to solve this problem is by having a good design pattern in place and make sure that the entire team clearly understands the design and implements the same in their code. We can solve this by doing a layered approach using MVC and Visual Inheritance.

Solution to our problem

1. Don’t alter the table add another table which stores the additional columns like address and the junction/extended table should have a foreign key relationship with our main employee table.
2. Create a new Inherited form, which inherits from our main employee master screen. To use Visual Inheritance we need to change the Access Modifiers to Protected from Friend. By default VS .Net puts the access modifier as friend.

View Layer

Our View /UI layer should only have UI related validations in it. We should not have any Business Logic in to it. This gives us the flexibility to change the UI at any time and we can also have different UI for different customers. We can even have web based UI for some of the clients.

Controller /Director

Controller is the layer, which responds to the events in the UI. For example Save button click on my employee master screen. This layer should act as an intermediate between our View and Model. Initially we may think that this layer is not necessary. I am also not convinced with this layer. But still we need to think. May be after some days we will get answer for this. This layer will act as just event redirector.

Model

This layer has all our business logic. This is the most important layer. This layer will have our core functionality. This layer should be designed in such a way that out core and complex logic should be designed as functions and these function should be marked as overridable so that the inheriting classes can re-use the logic or override the logic. We should not make all the functions in the layer as overridable this may raise some security threats.

Database Operations

All the database operations should be done in the Model base class. All the inheriting classes should call this method to do database updates. The design can be like this. In my EmpModel base class I will have a protected array list which will store all the objects which needs to get updated. All the classes which inherits this class should add there objects to this array list and they should call the base class Update method. This helps us to do all the db operations in a single transaction.

For example in our example, we should create an EmployeeBase class, which will have properties for name and age. Our EmpModelBase should have a new instantiated EmployeeBase object and our view should fill the object properties. Finally the view will call the controller’s Save method and the controller should call the Model save method, there we will add the Employee object to the array list and the function should call the mybase.Update method. This method should loop through the array list and it should fire corresponding db update statements. This is just an example. We need to enhance this depending upon our requirement.

Conclusion

1. Layering Approach helps us a lot and we need to enhance it to get full customizability.
2. We need to enhance this with all our design knowledge.
3. No IDE enforces these patterns, it is up to us to do clean and disciplined way of coding.
4. Once we are used to these approaches / patterns then we are addicted to it.

Download sample project


Comments

Author: Kanchi Raghuraman Kesavan16 May 2004 Member Level: Bronze   Points : 0

Hi the Article was good. But I believe , it can be made better by including some Original Example.

Regards and Good Luck

Raghuraman

Author: critic20 Jun 2004 Member Level: Bronze   Points : 0

No sathish have posted sample project demonstrating the MVC pattern.

Author: sujit kumar singh03 Dec 2004 Member Level: Bronze   Points : 0

satish... i think in case of winapplication controller will be nothing but a single class which will have delegates to handle events passed from different other forms (view's).. depending on the view which is generating event... controller will call other classes required for business logic (which will be model)..

Author: sujit kumar singh03 Dec 2004 Member Level: Bronze   Points : 0

sorry i didn't contiued since i had to go...
In case of web application MVC can be used as
Controller: HttpModule library which will take the request and then forward to appropriate .aspx page.. (all authentication or validation can be done here)
Model: seperate classes for each objects (User, Departments etc.. for form processing... similar to java beans
View: .aspx pages to take input from user and then display...


process flow would be
input.aspx (view)
HttpModule(controller)--> initialize the object classes for selected page--> class (Model) will execute business logic--> forward the result along with the object to
view.aspx(result view page)

Thanks
Sujit

Author: Prerana kargeti19 Jan 2005 Member Level: Bronze   Points : 0

the article was simple and easy to be understood,but it would have been more beneficial if topics like "smarty engine" and its functions could have been introduced.

Author: Nrupendra Singh Rathore30 Sep 2005 Member Level: Bronze   Points : 0

Its a good & short example of implementation of mvc.

Author: Ramalakshmi03 Sep 2006 Member Level: Bronze   Points : 0

The example was very good , But could not download the sample project . Could anyone help me in this

Author: Nilesh Joshi09 Oct 2006 Member Level: Bronze   Points : 0

Example here is short, but i think, can be written in more relastic way with good example.

Author: Bunty10 Dec 2009 Member Level: Gold   Points : 2

Hi,

Very nice article.

Keep posting such article which help us a lot.

Author: Madhurendra Kumar Pandey23 Dec 2009 Member Level: Bronze   Points : 0

nice work , nice attempt....

Author: sjk20 Feb 2010 Member Level: Bronze   Points : 1

Good work..Simple and understandable..

I think this will also help you..
http://www.tctcworld.com/dotnet/viewforum.php?f=1

Author: PHANI HARSHITHA MADALA22 Feb 2010 Member Level: Gold   Points : 1

Hi Mr.satish,

Very usefull info..

Thanks for sharing with all of us.

As others suggested could have been more effective with examples.


regards,

harshitha

Author: macxima23 Feb 2010 Member Level: Gold   Points : 0

Hi Satish,

good expaination for MVC.

Author: Avinash03 Aug 2010 Member Level: Bronze   Points : 1

I am using MVC architecture in my project, and article is nice to understand the basics of MVC....!

Thanks,
Avinash

Guest Author: maniammai03 Sep 2013

Excellent. easy to understand

Guest Author: Icke29 Oct 2013

Thank you for the good article. Unfortunately, all the images are lost. The references point to 404 :( is there a way to fix that?



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: