views:

16

answers:

1

Hi guys and girls.

Im using NHibernate to map my database tables with my entities and NHibernate Validator to validate my entities. It works sweet when validating the properties of the entities, however, is it possible to make database lookup validation with NHibernate Validator?

(Poor) Example: I have an Animal class and an AnimalType class (type= {cat, dog, horse}). An animal of the same type has to have a unique name. So if there exists a horse in the database with the name "Jolly Jumper" I would like to receive an error message if I try to create another horse with that name.

public class Animal { public int Id { get; set; } public string Name { get; set; } public AnimalType Type { get; set; } }

public class AnimalType { public int TypeId { get; set; } public string TypeName { get; set; } }

public class AnimalDef : ValidationDef { public AnimalDef() {
Define(e => e.AnimalName).ShouldBySomeMagicReturnFalseIfThereExistsAnimalOfTheSameTypeHavingTheSameName(); } }

A: 

Yes, it's possible.

Here's an example from Fabio Maulo: http://fabiomaulo.blogspot.com/2009/10/validation-through-persistence-not.html

Diego Mijelshon