Doug.Instance

Improving the world one post at a time

  Home  |   Contact  |   Syndication    |   Login
  27 Posts | 0 Stories | 47 Comments | 0 Trackbacks

News

Tag Cloud


Archives

Post Categories

My Sites

I am totally in love with ASP.Net MVC 2 so I figured what better topic for my first post.

When I first saw the presentations on MVC (or if you prefer MVVM), I wasn't impresesed.  My first reaction is "I already do that."  I looked at it as a design pattern, seperation of concerns, etc.  Then I watched Scott Hanselman's video and realized two very important things:

  1. MVC is not web forms
  2. MVC 2 ships with VS 2010 (even Express)

The second is possibly the most important.  I had looked at other Microsoft patterns (Enterprise Library, etc.) and in general they were overly bloated and forced you to a VERY specific design pattern which was not necessarily compatible with other things you were doing.  In some cases, MVC is the same - you won't be able to just start making MVC "stuff" and sticking it in your existing web forms app. 

Another problem with previous patterns was you had to make sure that everyone on your team had the same release.  While there are updates to MVC, only one version ships with VS so unless you do something out of the ordinary you will be on vanilla MVC 2.

So why do I love MVC (today)?  Well, after watching Scott's video and playing around on my own, I was enjoying not having to cast query string parameters to their native types.  I wrote methods that looked something like this:

public ActionResult SomeMethod(int? id, string someParam, string someOtherParam)
{
    MyModel model = new MyModel()
    {
        Id = id,
        SomeProperty = someParam,
        SomeOtherProperty = someOtherParam
    };
   
return View(model);
}

All was right with the world.  I had a model that contained everything I needed and in many cases was a class already created based on a prior encarnation of the application.

Then I started getting 20-30 parameters in my methods and things started getting ugly.  Then I had my MVC epiphany of the day: MVC model updates can happen via both POST and GET!  So the method above (which was really ugly) turned into this:

public ActionResult SomeMethod()
{
    MyModel model = new MyModel();
    TryUpdateModel(model);
   
return
View(model);
}

Ahh!  So elegant it hurts!

 

posted on Thursday, September 30, 2010 1:04 PM

Feedback

# re: Why I Love ASP.Net MVC (Part I) 9/30/2010 2:58 PM Ryan
I would much rather see you write, granted this doesn't do anything interesting.

public ActionResult SomeMethod(MyModel model)
{
return View(model);
}

Although there are a few concerns to be aware of when binding directly to your entities. Just google for it.

# re: Why I Love ASP.Net MVC (Part I) 9/30/2010 9:54 PM Doug
I am specifically addressing GET transactions. I generally overload the get version of the action with a version accepting either the FormCollection or model and tag it for the appropriate method (HttpPost etc.).

Post A Comment
Title:
Name:
Email:
Comment:
Verification: