January 4, 2010

Validation Frameworks for .net Compact Framework

Filed under: Uncategorized — Tags: , — yasi8h @ 6:52 am

If you google the title of this post you would not find any satisfying results. I tried it and i didn’t (.net validation framework intends to support the .net compact framework. But this is still in the todo list) came across any validation framework that officially supports .net CF. But most of .net validation frameworks out there or at least certain parts of the frameworks can be used in a .net cf project. I had to do some research on this topic recently so i am writing down my finding here. Please note that this post is by no means perfect. I might have missed some validation frameworks intentionally or accidentally. I was interested in a framework that would provide a basic validation framework and some ‘bindings’/helpers for windows forms and MVC. I needed a framework that can be used in a web/desktop and a mobile application. Any framework that was heavily coupled with any one of the platforms was ignored (as it will be difficult to use them in the other platforms).

Validation Frameworks ‘Assessed’

.net Validation Framework

  • The main ValidationFramework.dll assembly works with .netcf 3.5
  • validation rules can be specified as attributes, runtime code and xml configuration. so for whatever validation you need to do, you can use a exsisting rule or you can write a new one.
    • Attributes
      • As you would expect you can specify attributes that specify one or more rules to be applied on a given field, property or (method) parameter.
    • Runtime Code
      • You could write some code to specify what rules needs to be applied to what members. This code can be invoked at the runtime. This code will use types given in ValidationFramework.Reflection namespace to get the relavant members of a given type through reflection. After which you can add validation rules to those members.
    • XML
      • Validation definitions can be defined using XML configuration. The mapping document is designed to be readable and hand-editable.
        • <?xml version=”1.0″ encoding=”utf-8″ ?> <validationMapping xmlns=”urn:validationFramework-validationDefinition-1.5″> <class typeName=”ValidationSample.Person, ValidationSample”><property name=”Name”> <rule typeName=”RequiredStringRule” initialValue=”aaaa”/> </property> <property name=”Age”> <rule typeName=”RangeRule” minimum=”18″ maximum=”80″errorMessage=”Age is invalid” /> </property> <method name=”SetTheAge”> <overloadType typeName=”System.Int32″/> <parameter name=”age”> <rule typeName=”RangeRule” minimum=”18″ maximum=”80″ errorMessage=”Age is invalid” /> </parameter> </method> </class> </validationMapping>
  • Actual validating of a object is done one of the two ways noted bellow.
    • Inside validation
      • This is when the class is aware of validation and does the validation her self. this can be done by inheriting this class from one of the provided base classes and using the inherited methods (such as NotifyValidateChange method, which validate and then do a NotifyChange as in INotifyPropertyChange interface implementations).
      • Another way of doing this is by using a FieldValidationManager or a PropertyValidationManager internally within the class.
    • Outside validation
      • When outside validation is done the class whose objects are validated doesn’t know about the validation process and does not invoke it. someone can externally use a FieldValidationManager or a PropertyValidationManager to do the validation.
  • Validation Rules can be grouped using RuleSets
  • All the Validate methods in *ValidationManagers and Validation related base classes return a ValidationResult.
  • If you decide to write your biznuess classes inheriting from a provided base class that implements IDataErrorInfoProvider interface you can use windows forms controls like ErrorProvider to display errors in windows forms projects.
  • The framework also provides types to help you implement validation in WPF and Web projects.
  • Although the framework have some documentation including the on site wiki, QuickStart projects, FAQ and a help file with documantaion for all the types and there members in the framework. There is a lack of a getting started guide.

Validation Application Block

  • Has better documentation when compared to .net Validation Framework.
  • Just as the .net Validation Framework, Validation Application Block (from here onwards reffered to as VAB) has three ways to specify validation rules or validators.
    • Attributes
    • Runtime Code
    • XML
  • “can be integrated with ASP.NET, Windows Forms, and Windows Communications Foundation (WCF).”
  • Depends on
    • Core library functionality: Microsoft.Practices.EnterpriseLibrary.Common.dll
    • The ObjectBuilder subsystem: Microsoft.Practices.ObjectBuilder.dll
  • When editing the XML configuration for the validation block you can use the provided enterprice library configuration editor(s). This configuration file can contain validation details for types.
  • Validation Rules can be grouped using RuleSets.
  • Validation is done in a manner similler to .net validation framework (or the other way around). ValidationResults class is used to send out validation results…etc
  • Has “Self Validation”. Which basically lets a class mark it self as self validatable and then when the class is validated a specified (by the class it self) method (inside the class) will be run. This method would receive a ValidatinResults type instance which should be modified with the appopriate results.
  • Self Validation and ‘normal’ validation (other validators provided by the framework) can be used together.
  • Validation of a object is done like:
    • Customer customer = new Customer(); ValidationResults results = Validation.Validate<Customer>(customer, customerRuleSetCombo.Text);
    • Or
    • Customer customer = new Customer(); Validator<Customer> validator = ValidationFactory.CreateValidator<Customer>(customerRuleSetCombo.Text); ValidationResults results = validator.Validate(customer);
  • Provides integration support for Win Forms. The main point of integration is ValidationProvider class. It is an Extender ( control that adds properties (at design time) to controls. So if you have a form for adding a new customer you can have a ValidationProvider component in it. And set each control on the form to use the ValidationProvider (using the extended properties on them). You can also set a property (via the SourcePropertyName extented property) name to which the value in the respective control should set to.
    • ValidationProvider class effectively lets you validate values for a given business class’s properties without actually having to have an instance of it at the time of validation.However ValidationProvider is not supported on netcf, due to the unavailability of IExtenderProvider interface.


  • Works with netcf
  • Validation rules can be specified with attributes
  • Business objects can inherit from a provided base class to get the IsValid method (which is used to validate an instance) or use EvilValidator class to do the validation.
  • Don’t have any integration helpers

Fluent Validation

  • Works with netcf
  • Can write validation rules using the builder pattern
    • RuleFor(customer => customer.Postcode).Must(BeAValidPostcode).WithMessage(“Please specify a valid postcode”);
  • Provide MVC framework integration (although as of the time of writing the provided integration is minimal).

NHibernate Validator

  • NHibernate.Validator.dll Works with .netcf
  • Validation rules can be declared using attributes, the builder pattern (ex:- Define(x => x.Name).NotNullableAndNotEmpty();) and XML.
  • Written validators can support applying constrains to the NHibernate meta model. Hence you can use your validation rules to generate an database schema with relevant constrains.
  • “NHibernate Validator is not limited to only be used with NHibernate”.
  • “NHibernate Validator integrates with NHibernate in a native way”
  • Provides integration with
    • WinForms – through NHibernate.Validator.Binding
    • NHibernate
  • Validation is done in a similar way to validation application block.
  • framework can be configured using xml or programaticaly (fluent/builder pattern).

Spring.NET Validation Framework

  • Validation rules can be specified by XML and programmatically.
  • Have WinForms support with Spring.NET RichClient
  • RichClient provides some custom controls, which are not usable in netcf (ie: the custom controls are based on .net controls).

Personally i think the Validation Application Block is the best bet. It has good documentation, features and ‘support’. But i would never know for sure. Got to use it and experience it for my self.


1 Comment »

  1. […] post:  Validation Frameworks for .net Compact Framework « theKindOfMe By admin | category: net library | tags: block, completely-new, marc, not-see, old-ones, […]

    Pingback by Validation Frameworks for .net Compact Framework « theKindOfMe Virtual Library VLChina — January 5, 2010 @ 12:18 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: