Recently I setup a new asp.net 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 asp.net 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 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
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.
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 🙂
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!