MVC Architecture in .Net

This article helps you that how MVC pattern can be used in ASP.NET application & maintain the basic Microsoft supplied architecture.

MVC stands for MODEL VIEW CONTROLLER. ASP.NET MVC is an architecture to develop ASP.NET web applications in a different manner than the traditional ASP.NET web development. Web applications developed with ASP.NET MVC are even more SEO (Search Engine) friendly.

The entire ASP.NET MVC architecture is based on Microsoft .NET Framework 3.5 and in addition uses LINQ to SQL Server.

What is a Model?

  1. MVC model is basically a C# or VB.NET class
  2. A model is accessible by both controller and view
  3. A model can be used to pass data from Controller to view
  4. A view can use model to display data in page.

What is a View?

  1. View is an ASPX page without having a code behind file
  2. All page specific HTML generation and formatting can be done inside view
  3. One can use Inline code (server tags ) to develop dynamic pages
  4. A request to view (ASPX page) can be made only from a controller’s action method

What is a Controller?

  1. Controller is basically a C# or VB.NET class which inherits system.mvc.controller
  2. Controller is a heart of the entire MVC architecture
  3. Inside Controller’s class action methods can be implemented which are responsible for responding to browser OR calling views.
  4. Controller can access and use model class to pass data to views
  5. Controller uses ViewData to pass any data to view

The Model is the representation of the thing the user is trying to interact with. It typically holds the data and business rules. The View pulls information from the Model and renders that information in a way the user will understand. The Controller interprets actions by the user, usually mouse and keyboard events, and sends the information to the model or the view. The model knows nothing about the view or the controller other than that they exist. The view and the controller know as much as possible about the model. The advantage to this type of architecture is that the view and the controller may be changed without affecting the the model. For web applications that need to render themselves on various browsers and devices this is an ideal architecture that allows the application to be written originally for a desktop browser and later written for a PDA by changing only the view layer.

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.

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 today’s 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.

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 re-usability 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.

MVC Architecture

.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 filesTyped 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 doesn’t 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() etc events.

.NET Framework has all Object-oriented features like code reuse, encapsulation etc So, proper design of your model can make your user interface and view completely separated from model which can server multiple views.

Conclusion

With the advent of ASP.NET, many of the hacks we have had in place have been removed. All of the view and controller related code is now in essentially one place instead of two. The defacto standard for writing view based application (MVC) is preserved.

Tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *