Thursday, December 25, 2014

ASP.NET MVC: What MVC is and why it's useful

Recently, I read this book: "Professional ASP.NET MVC 5" and I decided to write a series of blog posts on what this book taught me. On my journey through this book, I realized the knowledge gaps which we have at places and how we can fill them. This book also exposed me to a whole lot of new features that are available to us as developers and how we can use them. I will try to share the knowledge that I have gained and I hope that you enjoy my blog posts.

MVC

MVC is a well known architectural pattern and is there in the industry for decades. MVC stands for Model-View-Controller and it helps us in separating the data access logic, user interface logic and user interaction logic. Dividing the application into these three components helps in separating concerns and better organize our application for extensibility and testing.



MVC


Model: These are the classes that represent the application's data domain. These objects are often used to retrieve and save information from and to the data storage system (e.g. database), respectively. These contain all the business logic.

View: These are the components that represent the user interface. This is what is shown to the user.

Controller: These are the classes that handle the user interaction. They take care of interaction with the model and decide which view to render to the client.

Advantages:

1. Separating Concerns
Separating these concerns helps in managing a complex project by guiding us how to divide the application into smaller parts. Also, in a large project, it can help divide the effort e.g. one person can work on the View while the other person can work on the Model simultaneously.

2. Unit-Testing and Test Driven Development
Interface based core contracts help us in unit testing. You can easily create mock objects and use them for testing purposes.This can help us in test Driven Development.

3. Customization and Extensibility
At every point of studying through ASP.NET MVC, I realized that MVC gives you some default convention to follow but if you don't like it, you will be provided with a hook to change it according to your liking. For example, all the controller classes have the name ending in "Controller" by default. However, if you don't like it you can change it. There are numerous such examples. The default conventions are good for people who want to quickly start development or understand somebody Else's code whereas the hooks help you extend or customize the behavior as per the user choices.

4. Routing
It also has extensive support for routing. You don't have to use file names and their extensions. You can use naming patterns that are good for SEOs and REST. 

5. ASP.NET Features
It also supports all the ASP.NET features like Authentication, Session Management, caching, membership and roles, etc


MVC is an alternative to Web Forms pattern for creating web applications. It has the powerful features of Web Forms but in a more elegant way. In general, if we are building a new web application, I will recommend using MVC rather than Web Forms.

In the next blog post, I will delve deeper into ASP.NET MVC. Until then, Happy Coding :)