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?
views:
597answers:
3
A:
This sort of thing is indeed possible. Coding Instinct has a great post introducing NHibernate.Validator.
Gabriel Florit
2009-05-22 02:50:11
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
2009-06-02 07:02:08
+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
2009-05-22 12:10:35
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
2009-06-23 02:47:09
A:
Since I need more rep to comment, could you post your code? My 'OldState' Array is always empty...
Lodewijk
2009-07-02 13:11:42