Since Asp.Net Mvc 1 I’ve seen many developers confused about how to use it. I’m writing this post with the hope that it will be useful for the developers who want to learn about Asp.net Mvc and will clear up some confusion about what it is.
While Asp.Net Mvc is recent, the MVC paradigm is not. If you don’t know what MVC stands for here’s a very quick info. When using MVC you design your application into 3 tiers:
- Model - responsible for processing and storing data.
- View - responsible for displaying the data.
- Controller - responsible for taking user input, sending it to the proper model and selecting which view is shown to the user.
Asp.Net Mvc (or any other MVC framework) is simply providing the plumbing and the tools to develop faster an application according to the MVC paradigm. They don’t impose to do things in a certain way, it’s still very easy to write a code mess using asp.net mvc. It’s up to you, the developer, to maintain the discipline and respect the responsibilities of each tier.
Many developers suffer of the “fat controller” syndrome, that is they handle everything in the controller. They think that the Model is in fact the database or all those objects which hold data and many tutorials just encourage this confusion. They teach you to shove almost everything in the Model directory, where you have some data holder objects (POCOs) used by linq2sql or EF and also by the Views and frankly, this is a confusing MESS! So let’s clear it up now.
The Model is the application layer where you process and store the data. In a complex application, the Model is at least a project itself, without any relation to Asp.net Mvc. The Model is in fact the “engine” of the application, some people call it the “Business Layer”, it’s simply not the database or the ORM, linq2sql, EF etc. It knows how to persist the data, but it does much more than that. And in a properly written application, the Model uses the Repository pattern (so it won’t be tied up to a specific technology) to deal with the persistance.
You need to understand that your Model may be composed of one or many projects and it has little relation with the Model directory. In fact, the Asp.Net Mvc project is just the web interface of your application and it contains Controllers, Views and … View Models. The View Model is the model used by a View and it is not the same thing as the Model. It usually contains some bits of your Model but that’s it. Only in trivial applications and tutorials, they are the same and it’s important to know the difference.
Also, the Controller is not the Model either. Yes, almost everyone knows this, but many developers seem to love to access the database, orm etc from the controller, basically turning the Controller into a Controller-Model hybrid. The Controller job is to send the input to the Model and eventually to tell the Model to store the processed result. That’s it.
In conclusion, Asp.Net Mvc makes it easy for you to write MVC aplication but you have to know what a Model really is and what a Controller is supposed to do. As for tutorials, take them with a grain of salt, they are made to show some neat features but you also learn some not-so-good practices and think that you have to use linq2sql or EF or that a view model is the Model. And don’t start me about the validation, but this post is already long enough.