R – NHibernate Rollback w/ broken rules (validation best practice)

nhibernatevalidation

I'm working on a legacy system that is using the enterprise library validation block to add a broken rule when an object is not valid. Then the user is returned a message based on this error and told the object was not updated.

The only issue is that now I'm using NHibernate to persist these objects -NHProf shows an update to the object when I commit the session. This is because the object has been modified I assume and the ORM is simply doing its job.

My question is this – what would be the best way to check for these broken rules before I commit the session? Or should I not use Enterprise library and switch to something NHibernate friendly?

Update

I came across this event listener class for the NHibernate Validator Event Listener – my final implementation was very similar

Best Answer

I'm not familiar with the enterprise validation block, but can't you write an interceptor or something like that which can be used to determine whether the object can be saved or not, based on the information you have regarding the broken rules ?

You can also choose not to use the automatic dirty checking of NHibernate.
This means that you'll have to call 'Save' yourself on an object you have modified in order to get that object saved. You can get this functionality via NH Addins

Related Topic