views:

597

answers:

3

Prior to persisting updates to my business entities, I need to perform validation checks to determine which properties have been changed. For example, certain fields can only be updated when the "Status" property has a particular value. E.g. when an Order entity has a Status of finalized, only the notes (string) field can be updated. Is this sort of thing possible using NHibernate, or should I be tracking the changes myself in the Business entities?

A: 

This sort of thing is indeed possible. Coding Instinct has a great post introducing NHibernate.Validator.

Gabriel Florit
Thanks for your answer. I'm more concerned with tracking which fields have been modified and applying validation rules against those fields. At face value NHibernate.Validator does not seem to provide any support for this (correct me if I'm wrong)
teevus
+2  A: 

If I understand what you're trying to do, Gabriel's solution is not quite what you need. If it is not, you can try an event listener. Those allow you to hook into a common event (like on save) and do some processing before NHibernate finishes the save/insert/update/delete. Alternatively, you could look into using interceptors by implementing the IInterceptor interface.

Stuart Childs
I managed to use my own custom implementation of IPreUpdateEventListener. The OnPreUpdate() method contains a parameter of type PreUpdateEvent which contains the old State and the new State. These can be compared to work out which properties have changed. Source code available on request
teevus
A: 

Since I need more rep to comment, could you post your code? My 'OldState' Array is always empty...

Lodewijk