Why MVC? That is a reasonable question. Model–View–Controller (MVC) is an architectural pattern used in software engineering.
This is different from Web Forms (Traditional ASP.NET Web Applications).
In ASP.NET applications that do not use the MVC framework, user interaction is organized around pages, and around raising and handling events from the page and from controls on the page.
In contrast, user interaction with ASP.NET MVC applications is organized around controllers and action methods. The controller defines action methods. Controllers can include as many action methods as needed.
The breakthrough of Visual Basic 3 is that developers would just respond to events, such as clicks on buttons. You would spend your day doing “event – driven” development by writing code that would execute based on click of a mouse.
But MVC is different. Sure, if you look close enough, you can see examples in MVC.
But you must put that to the side now. They are not gone. It is more that that you should work in terms of “controllers” and how a url maps to an action method.
The controller defines action methods. Controllers can include as many action methods as needed.
Some key facts about how MVC fundamentally works:
The pattern isolates "domain logic" (the application logic for the user) from input and presentation (GUI), permitting independent development, testing and maintenance of each.
The MVC offers a clear separation of concerns.
Other key facts include:
ModelThe object that represents the data being operated on or requested
ViewTransforms the Model into a format appropriate for sending to the user
ControllerCoordinates the request
The components of the ASP.NET MVC framework are designed so that they can be easily replaced or customized.
MVC is just another form of a web application
Dependency Injection is part of the architecture
In the MVC world
Without the concept of dependency injection, a consumer who needs a particular service in order to accomplish a certain task would be responsible for handling the life-cycle (instantiating, opening and closing streams, disposing, etc.) of that service.
One benefit of using the dependency injection approach is the reduction of repetitive code in the application objects since all work to initialize or setup dependencies is handled by a provider component.
Another benefit is that it offers configuration flexibility because alternative implementations of a given service can be used without recompiling code. This is useful in unit testing because it is easy to inject a fake implementation of a service into the object being tested by changing the configuration file.
Martin Fowler talks about it more deeply here.
You can watch a video too.
Watch The Webcast Here (59 Minutes, 82 Megabytes). Sounds like Rob Conery talking about how painful it would be to change a tight coupling in your code. The example he cites is the database backend. Let’s say you want to change your source of data. This would be painful as you’d have to go everywhere where your code talks to the database and change the places where the connections is created and opened. You would need to remember to clean up and dispose. If you abstracted that away, like with dependency injection or the repository pattern, you would avoid all the headaches associated with changing your data store.
Specifically, Rob talks about implementing StructureMap.
I haven’t used StructureMap, but here is some of what the package promises:
Incoming request directed to Controller for processing
In the next step the controller gets a model object. The fact that the database is involved is transparent to the Controller.
Once the model is obtained by the controller, it is passed to the view for rendering to the end user.
The view renders the chart or user interface. The view knows nothing about where the data came from or what the user input.
There are several choices for creating web based content. Web forms have been around for a long time.
Web Forms (your other option)
ASP.NET offers great features to both Web Forms and MVC
You can watch a presentation about how you would choose between using Web forms versus MVC.
Choosing between ASP.NET Web Forms and MVC - MIX Videos
Blog posts and videos by team members:
I thought I’d start this blog with the official web site for MVC. I believe I’ll start with a download.
I chose the Microsoft Web Platform Installer.
I walked through the setup. I re-ran to make sure it got installed appropriately.
MVC 2 RC - This is not in full production release as of 1/23/2010, but that’s the code base I would like to work from.
Here is the URL:
This is “Pre-Release” Software
However, I ran into an error because I have it already installed. When I checked Control Panel, here is what I found:
Visual Studio 2010 Beta 2 has everything I need.
This first tutorial is about getting familiar with some of the classes.
The goals are:
The next few screens will demonstrate how to create a blank new project of type:
Figure above : File, New Project (starting from scratch)
Figure above: ASP.NET MVC 2 Web Application (a plain old new project)
Figure above: Solution Explorer (default project)
Let’s focus on the HomeController class.
What is the job of the controller?
Taking a deeper look – HomeController.cs
Imagine that you enter the following URL into your web browser’s address bar:
The Default route maps this URL to the following parameters:
controller = Home
action = Index
id = 3
Note – In this case we are ignoring id = 3. That comes later when we example the default template. Remember, we control how the url gets interpreted.
What the default application looks like. Notice I added “From Bruno!”
This diagram speaks well to the architecture. Notice that the url ends in "/Home/About".
One of the things to note is that a URL does not map to a specific web page that you wrote. Most approaches require that the direct page is hit. Otherwise you get the “404 – Page Not Found” error.
Plain Old HTML Below – The hard coded old school way of doing things
The URL above is NOT MVC
More examples of how parsing works
Code Snippet that shows the default routing tables being setup
I like the asp code on www.autopartsandstuff.com.