Tag: nhib

NHib Atleast One Check

NHibernate doesnt have a way to check whether a List has atleast one item.
This kind of validation is important and if it fails, atleast the save should catch it.
This can be done with a neat Mapper extension and an on save listener.

//Register with the NH Configuration
configuration.SetListener(ListenerType.SaveUpdate, new HasAtleastOne.Listener());

//Use in the classMap
HasMany(x => x.Links).KeyColumn("Project_id").MustHaveAtleastOne();

//Test the code
var l = new HasAtleastOne.Listener();
var e = new Project();

Read more »