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…

  1. Create a new class library project named Eg.AttributeValidation.
  2. Copy the Eg.Core model and mappings from Chapter 2, Models and Mappings to this new project.
  3. Change the namespace and assembly references in the mappings to Eg.AttributeValidation.
  4. Change the namespaces for the entity classes to Eg.AttributeValidation.
  5. Install the NHibernate Validator package using NuGet Package Manager Console by running the following command:
    Install-Package NHibernate.Validator ...

Get NHibernate 4.x Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.