ASP.NET MVC Model and Business Rules Validation

5 Sep

ASP.NET MVC Preview 5 is the release I have been waiting for from Microsoft.  The MVC team finally added easy model validation to the framework.  The new features are detailed at Scott Guthrie’s blog.  Validation was an issue that had previously hung me up when I dabbled with the MVC framework.  Model validation enables another business rules validation in a way that I thought was very elegant.  You can use partial classes to add business rules to a model class.  This allows your business rules to be separate from the model classes and if you regenerate your model classes you won’t overwrite your business rules.  I really like to see this because it means in my test project I can remove a layer of abstraction and keep the business rules close to the model classes.

I do have an issue with one of the new features in Preview 5 though and that is the [ActionName("action")] attribute.  In conjunction with the  [AcceptVerbs("POST")] attribute it allows you have different functionality for a URL depending on how its requested.  This is necessary functionality I just have issue with the way its implemented.  I would prefer to see something along the lines of this.

public object Create(string productName, Decimal unitPrice)
{
    if (Request.Verb == "GET")
    {
        return View("Create");
    }
    else if (Request.Verb == "POST")
    {
        // save the data RedirectToAction("List");
    }
}

I think this would make more sense.  You wouldn’t have the possibility of confusion as to what action to call your code.  You would always call the Create action.  If you started getting actions with a lot of lines of code in them you could just refactor the code in the if else statements into new methods.

Lost Styles

2 Sep

I did something dumb and lost my WordPress theme.  All hail the mighty Google though for helping track down the name of the theme I was using.  I searched for a word in the title of one my recent blog posts and then clicked on the Google cache version of it and found the name of the theme I was using at the bottom of the page.  I still lost all the changes to the theme I had made but at least I have somewhere to start from.

What Hung Me Up On Using ASP.NET MVC Preview 4

13 Aug

I was delving into ASP.NET MVC Preview 4 for a small project and I got hung up on form validation.  Currently ASP.NET MVC provides no way to validate form input against the model objects.  Form validation has to be written client side in JavaScript and server side in the action.  I would really like to be able to decorate the LINQ-to-SQL objects fields with validation information like Rails does.  I also hope they add some validator helpers to the the HTML helpers.

I see that someone else ran into this and went ahead and coded a solution for it the Forms Framework with Validation for ASP.NET MVC.  This is an interesting approach to the problem I am not sure how it would deal with forms that are using more than one model.  I want Microsoft to address this feature rather than cobbling basic functionality together from a lot of little side projects that may not see any further progress after their initial development.

iPhone Post

30 Jul

This is a post from my new iPhone using the WordPress iPhone application. This is pretty neat.

ASP.NET MVC Beginner Mistake

24 Jul

My last post was the result of making a beginners mistake and spending a lot of time being unable to resolve the issue.  Since I am using IIS 6 on XP/2003 I have to add .mvc to my controller paths to activate the ASP.NET MVC framework on a URL.  I was doing this and my controllers were working except for the / path of the web site. 

ASP.NET MVC still needs a default.aspx in the root of the web site.  The default.aspx just performs a redirect to the URL of whatever controller you are using for the site root. I had not changed the URL that default.aspx was redirecting to.  Once I changed the URL the redirect was using my problem was fixed.  I want to give a shout out to the official ASP.NET MVC forum for the help I got there.  Its nice to see that a Microsoft product is building an actual community around this technology as that more than anything will help it grow and evolve.