Property validation with attributes
NHibernate Validator provides data validation for classes. In this recipe, we will show you how to use NHibernate Validator attributes to validate your entities.
Getting ready
Complete the Eg.Core
model and mappings from Chapter 2, Models and Mappings.
How to do it…
- Create a new class library project named
Eg.AttributeValidation
. - Copy the
Eg.Core
model and mappings from Chapter 2, Models and Mappings to this new project. - Change the namespace and assembly references in the mappings to
Eg.AttributeValidation
. - Change the namespaces for the entity classes to
Eg.AttributeValidation
. - Install the NHibernate Validator package using NuGet Package Manager Console by running the following command:
Install-Package NHibernate.Validator -Project Eg.AttributeValidation
- Create a new attribute class named
NotNegativeDecimalAttribute
with the following code:[Serializable] [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] public class NotNegativeDecimalAttribute...