ASP.NET xVal Library with DataAnnotations

Recently I setup a new mvc application.  After reading Steve Sanderson’s new book – in which he refers to his xVal library, I figured I’d take a look at it.

The main features of the xVal library is it’s ability to apply validation that executes on the server side, as well as the ability to apply client side validation using the same technique.  This is quite powerful.

On the client side, you can use the native MS library, or jQuery. 

On the server side, there are also options available: DataAnnotations, Castle Validators, or even further, you can write your own IRulesProvider and attach rules programmatically.

I won’t go into ‘how’ to use the library, as Steve has given a great walk through here: xVal – a validation framework for ASP.NET MVC

The second part is the DataAnnotations – which I was glad to have investigated some of Silverlight 3 to understand the concept.  As mentioned in this post by Brad Wilson (DataAnnotations and ASP.NET MVC), “in .NET 3.5 SP 1, the ASP.NET team introduced a new assembly named System.ComponentModel.DataAnnotations…”

I think the real value here is this ‘buddy class’ concept that lets you use validation attributes on code generated classes, ie. Linq to SQL/Entity Framework.  Basically you can add attribute metadata to the generated classes in partial classes.  An example of this can be found here, Integrating xVal Validation with Linq-to-Sql

This is very powerful, in particular how the xVal library gives a developer several options to help apply validation to the model classes.

Great stuff  🙂

Maarten Balliauw on ASP.NET MVC Best Practices

Maarten Balliauw shares some best practices on ASP.NET MVC

  • Use model binders where possible
  • Be careful when using model binders
  • Never re-invent the wheel
  • Avoid writing decisions in your view
  • Don’t do lazy loading in your ViewData
  • Put your controllers on a diet
  • Compile your views

Thanks Maarten

Castle Windsor 2.0

Ayende has announced Castle Windsor 2.0 Release:

Castle Windsor is by far my favorite IOC container – it’s facilities, ability to use xml config or fluent interface code registration make it very flexible.

I have used Castle Windsor in several projects of late, including with the MVCContrib support for Windsor.

Great news and long live the Castle Windsor  🙂


A bit OT but… If you are using NHibernate you can use 2.1 and include NHibernate.ByteCode.Castle.  From the NH release notes – using that assembly will tell NH to use the DynamicProxy2.

ASP.NET Webforms vs. ASP.NET MVC

I’ve been reading some blog posts with guys trying to say which is better, and what a developer should know with ASP.NET etc…

1. Any ASP.NET developer who thinks he would need to learn both (ie let’s say a consultant)  – should always expand and learn the technology at hand.  Most often we tend to become experts in what we work with everyday. 

2. I have my own opinion of which one I like better – but the fact is , there are different strokes for different folks.  Many are working on projects where Webforms were and have been the primary ASP.NET technology for awhile.  The staff is educated on it, knows it’s pros and cons and works through them.  They have an investment in it. 

In my persona experience, the control that the ASP.NET MVC has for the developer, being ‘less abstract’ is appealing.  I find I have to fight the abstraction that ASP.NET Webforms brings (ie. the idea of ‘state’ – but it is stateless).

I do know that the next version of Webforms continues to expand and get better – so there are obviously pros and cons to both.

Again, personally I love what MS has done with ASP.NET MVC and I was able to quickly build rich UI and testable interfaces with it.  It is more of what I see out there in the rest of the web development world.  Webforms are very proprietary to MS and unique.  It has an event-driven/viewstate model.

In the end, it’s the difference between a ‘Front controller’ architecture vs. a ‘Page controller’ architecture.  The MVC model is a proven architecture as well, and I find it a bit more intuitive than attempting to use a MVP setup with webforms.

In my experience I’ve seen shops put way too much logic in the code behind – not separate out the layer appropriately.  Obviously the same can be done in MVC with too much logic in the page or controller.  (I prefer a service layer in both setups).

So in the end, regardless of the technology, how you architect it and apply rules to your development team (ie. minimize code in the code behind of a page) is what is really the issue in my opinion  🙂

NHibernate Validator

Ayende has posted about the NHibernate Validator

I’ve been using a custom validator library (that uses attributes like this one).  However, seeing what the NHibernate validator provides, I’d stick with it – since the validation is called on save on the dev behalf.

Ayende also shows how to pull out the errors.

As per his post, you can read the documentation on the library for further information.

As always, good stuff from Ayende!