skip to the main content area of this page
Patterns and Practices

 

NHibernate Validator Tutorial - Validation Attributes and ValidatorEngine - ASP.NET MVC


I have been using the Enterprise Library Validation Application Block ever since it came out in Enterprise Library 3.0. It is a decent validation framework for validating your business objects in your applications. A long time ago I wrote a number of tutorials on the validation application block:

 

 

NHibernate Validator

The Validation Application Block isn't the only game in town, however, there are quite a few validation frameworks. Although the NHibernate Validator offers special integrations with NHibernate, you can use it as a simple validation framework without NHibernate.

You can use XML config files, but here is a simple Customer Class decorated with attributes to validate particular properties on the Customer Class:

 

public class Customer

{

    [NotNullNotEmpty]

    public string Firstname { get; set; }

 

    [NotNullNotEmpty, Length(Max = 10)]

    public string Lastname { get; set; }

 

    [Email]

    public string Email { get; set; }

 

    [Past]

    public DateTime JoinDate { get; set; }

}

 

Most validation frameworks offer such attributes. You can easily validate an instance of Customer using the ValidatorEngine:

 

Customer customer = new Customer();

 

customer.Lastname = "This is longer than expected.";

customer.Email = "bademail.com";

customer.JoinDate = new DateTime(2020,12,25);

 

var validator = new ValidatorEngine();

InvalidValue[] values = validator.Validate(customer);

 

The example is pretty trivial and will produce all sorts of errors:

  • The Firstname is null which violates the NotNullNotEmpty Validator.
  • The Lastname is longer than 10 characters and will cause an error.
  • The Email will not match the built-in regular expressions.
  • The JoinDate is definitely not in the past and will cause an error.

 

When you create an instance of the ValidatorEngine and call Validate against an instance of Customer it returns an array of InvalidValue:

 

NHibernate Validator Tutorial

 

 

Displaying NHibernateValidation Errors Using ASP.NET MVC

If you attended the ASP.NET MVC Firestarter Event in Tampa Florida you will remember me talking about a simple Extension Method to populate the Controller's ModelState to display errors from the Enterprise Library Validation Application Block.

A couple of tutorials on ASP.NET MVC and Enterprise Library Validation Application Block:

You can do the same thing with NHibernate Validator:

 

public static void AddModelErrorsFrom(this ModelStateDictionary dictionary,

                                            InvalidValue[] errors)

{

    foreach (var error in errors)

            dictionary.AddModelError(error.PropertyName, error.Message);

}

 

 

Conclusion

If you are new to the NHibernate Validator, hopefully this tutorial gives you a pretty good idea of how to use it as a stand-alone validation framework similar to the Enterprise Library Validation Application Block.

 

Patterns & Practices Guidance


Tags: NHibernate


Topics



 

Popular Tags



Recent Links