R – NHibernate – problems with validation and transactions

nhibernatetransactionsvalidation

I'm using NHibernate as my ORM. I have a situation where I have some stuff wrapped in an ITransaction. I am listening to the SaveUpdate event in NHibernate and then doing my entity validation in that SaveUpdate handler.

For one of my entities, I want to validate that the value of a certain property hasn't changed. So I figured that I would load up the value of the existing object from the database and compare it with the new value. The problem is that I called ITransaction.Commit() to save my entity object and the transaction hasn't actually been committed at the time that validation occurs, so I can't load the existing object from the database because the transaction has it locked.

So I guess I have a few different questions here:
– Is the SaveUpdate event the correct place to do validation?
– Is there another way I can do this so that I can do the validation that I need to do (getting the existing value from the database and comparing)?

I'm sure someone else out there has run into a similar situation…… hopefully!

Best Answer

Validation has nothing to do with persistence, so saveupdate is not the right place. The correct place of validation depends on: what you want to validate, your programming style, the UI framework you use to show the validation messages, etc. Personally, I prefer to put the validation on the place where things are changed, so I would put it in the change method that sets the property. I don't understand why you want to load the entity in the previous state, because that state is already loaded when you load it for the first time.

Related Topic