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.
Complete the Eg.Core
model and mappings from Chapter 1, The Configuration and Schema.
Create a new class library project named
Eg.ClassValidation
.Add a reference to the
Eg.Core
model.Install the NHibernate Validator package using NuGet Package Manager Console by executing the following command:
Install-Package NHibernate.Validator
Add the following
ProductValidation
class:public class ProductValidator : ValidationDef<Product> { public ProductValidator() { Define(p => p.Name) .NotNullableAndNotEmpty() .And.MaxLength(255); Define(p => p.Description) .NotNullableAndNotEmpty...