Skip to main content

Posts

Showing posts from 2010

Data Validation with Non-Inclusive Range Validator

The other day I was doing some validation for a few of my class's numeric properties. The valid range, I was told, was 0 to infinity. That's easy enough, I told myself. I'll just put a [Range] attribute each property and let the framework do its thing, like so...

[Range(0,double.MaxValue,ErrorMessage="Please specify a value greater than or equal to zero.")]
After publishing the web site to the staging server, I got an email from the client saying that 0 was not a valid value for some of the properties. My first thought was simply to change my range attribute this way:

Range(0.00000000001,double.MaxValue,ErrorMessage="Please specify a value greater than or equal to zero.")]
After all, who's going to type that many zeros? I still wasn't happy with that solution, so I started considering my alternatives. IDataErrorInfo was a definite option, and that would look this:

public string this[string propertyName] { get { switch (propertyName) …

Modifying Code Generation Templates for MvcContrib Grid

Visual Studio 2010 in general, and MVC 2 in particular, bring the concept of code generation templates into the realm of first-class objects. Well, maybe it's not that good. However, MVC definitely makes tremendous use of the code generation template system, called Text Transformation Template Toolkit (or T4 for short). A fantastic introduction to T4 editing can be found in the January 2010 issue of MSDN Magazine, titled "Text Template Transformation Toolkit and ASP.NET MVC." That article covers the basics of editing T4 templates, so I won't bother repeating the same information. Instead, I'll describe the template I created for our team's latest project.

Before I get to the template itself, I need to describe the tools we're using, how to install them, and a little background on why we decided to do what we're doing.

The toolsMvcContrib GridEarly on, we did what most teams do. We used the tools provided. For creating a list of strongly-typed objects (…

Why You Should Consider MVC 2 for Your Next ASP.NET Web Site

You have many choices when you set out to design a new web site. Some people might say that you have too many choices. For the purposes of this article, I'm going to assume that you've already decided that you're going to use ASP.NET in one form or another, so won't presume to debate the merits of Windows versus Linux hosting. Instead I'm going to focus on a real project where we chose MVC Release 2 for a redesign of an existing ASP.NET WebForms site.

There are four main reasons that helped us decide on using ASP.NET MVC for our project: Testability Clean, SEO-Friendly URLsFull Control of the HTML generatedWe got to delete a lot of code!Testability By default, MVC projects encourage the practice of test-driven development. Each controller can be paired with a controller test class that exercises the controller's functionality without regard to the view that will ultimately render in the UI. Testing each controller's methods in this way ensures a clean se…