Extension Methods Improving the quality of Life

Extension methods, such a simple idea that can totally make the small things in your application or at least the un-DRY portions of it cleaner.
AddModelError and Multiple ValidationResults
Here is a simple example (taken from a custom model binder):
not too bad, but when working in a block of code where you might have a few instances of it you tend to see the noise a little bit.
How about a little reduction and also centralize some logic so your entire code base can leverage it?
Now that everything you thought was upright in the world is all in question, ok, not really - not even close. In fact weve only saved three lines of code where such things are being used but I would argue if you try to keep small things like this in mind that over time your code base will start to reap the benefits.
Here is how I implemented the extension method. Your mileage may vary.
DataAnnotations and the Validator class
Another area that might not be Asp.net MVC specific is if you are using the DataAnnotaions directly on your models and you use the static Validator class to validation the model. In this case the TryValidateObject is being used for the bool that is returned from the operation.
I think I prefer this a little more
Where ValidateAnnotations is implemented as follows. Also you if you prefer to change the where this can be used you can change the accessibility or change what this method extends to limit its exposure.
Hopefully these two little nuggets will help someone out. Both of these are just a means to end, where I think that end is a transformation to a code base even beyond simple changes like this. After all it is all about the journey not the destination.
File under: Asp.Net MVC Continuous Improvement Extension Methods- Internet Explorer is like
- I Quit My Job And We Are Moving Home To Montana
- LINQPad with Databases
- Stupid Linq Tricks Record Rollup
- Getting started with MVCContrib Filters
- C4MVC Reason 42 to Join
- Checking for special characters using LINQ
- Asp net MVC Session State Extension Method
- Unit Testing Just Do It
- Blog.Start
Posts
- 07 Jun 2010 Internet Explorer is like
- 06 May 2010 I Quit My Job And We Are Moving Home To Montana
- 17 Apr 2010 LINQPad with Databases
- 01 Apr 2010 Stupid Linq Tricks Record Rollup
- 30 Mar 2010 Getting started with MVCContrib Filters
- 23 Mar 2010 Extension Methods Improving the quality of Life
- 22 Mar 2010 C4MVC Reason 42 to Join
- 13 Mar 2010 Checking for special characters using LINQ
- 09 Mar 2010 Asp net MVC Session State Extension Method
- 08 Mar 2010 Unit Testing Just Do It
- 15 Feb 2010 Blog.Start