Wednesday, January 20, 2010

Lack of proper validation model in MVC 1.0

Now I have been working with MVC 1.0 for quite some time and figuring out some short comings. One major short coming is that there is no proper model for implementing validations. Validation at server side and client side. There are n number of open source tools but none of them are so mature that you can promote them to production. Following are some tools that I evaluated:

MVC.Net Validation toolkit (http://mvcvalidatortoolkit.codeplex.com/) – This toolkit is good in terms of defining server side and client side validations. We just need to define all validations at server side and it automatically takes those validations to client side. However the advantage of this toolkit is disadvantage as well. Validations are defined outside the domain model (your entity). This approach of defining domain validations is not a very good approach and I would opt for it as a last resort.

Data Annotations – .Net Framework 3.5 there is a new functionality of Data Annotations (System.ComponentModel.DataAnnotations). Data annotations have very rich functionality where we can decorate each property of our domain entity. MVC.Net has good integrations with data annotation. If you make your MVC.Net view a strong typed view then MVC.Net framework starts understanding the data annotations of our domain entity. However this model is giving us only server side validation. xVal (http://xval.codeplex.com/) is one tool that can actually retrieve the domain validations from server and can populate those validations as client side validations. However in my evaluation I found that xVal is not able to retrieve all domain validations and whatever it retrieved were also not functional.

But all this is applicable only for MVC 2.0. MVC 1.0 does not understand data annotations and to get it working you need to download non production ready code from codeplex. Basically you need to get DataAnnotations model binder for MVC 1.0 and configure it. For more detail on this please visit ASP.Net MVC Validation.

Enterprise Library Validation Block – In order to achieve our purpose we don’t need to implement EL Validation Block. We can just use the assembly which gives us the functionality to decorate our domain entity with some validations. Use of this block gives us server side validation and then we need to use MVCValidator (http://www.codeplex.com/aspmvcvalidation) library which works with EL Validation block to implement client side validations. However MVCValidator library is also not so good for production as it does not provide support to validate value types. It works only with string data type.

Given all this I decided that I am not going to take any more pain of evaluating open source libraries. I am going to wait for MVC 2.0 where I get proper validation model inbuilt. Till then I will live only with client side validation by implementing jQuery validation plugin. To save myself from corrupt data I can implement proper constraint at database. This is temporary arrangement till MVC 2.0 comes out.

No comments: