Creating validator classes
In the previous recipe, we saw how to decorate our entity classes with NHibernate Validator. A better practice is to extract your validation rules to separate classes and avoid this dependency. In this recipe, we'll show you how to create validator classes, as well as an alternative method for configuring NHibernate Validator.
Getting ready
Complete the Eg.Core model and mappings from Chapter 1, The Configuration and Schema.
How to do it…
- Create a new class library project named
Eg.ClassValidation. - Add a reference to the
Eg.Coremodel. - Install the NHibernate Validator package using NuGet Package Manager Console by executing the following command:
Install-Package NHibernate.Validator - Add the following
ProductValidationclass:public class ProductValidator : ValidationDef<Product> { public ProductValidator() { Define(p => p.Name) .NotNullableAndNotEmpty() .And.MaxLength(255); Define(p => p.Description) .NotNullableAndNotEmpty...