I want to track changes in my domain model. I know that NHibernate ISession is an inmplementation of UnitOfWork pattern, so it tracks this changes. Is there any way to pull out them, for example, before Commit() or Flush()?
2 Answers
Take a look at NHibernate's IInterceptor.
OnFlushDirty - will show you persisted properties on an updated object.
OnSave - will shows you persisted properties on a saved object.
You just need to create an interceptor class that implements this interface, and when you configure your NHibernate session, tell it to use that class.
Comments
I think than Interceptors are a little bit obsolete. Еrying to use NHibernate Events. I've subscribed on OnPreUpdate event. It's parameter has State and OldState properties, but OldState is allways null. Does anyone know this OldState works at all?
1 Comment
session.Merge( yourUnsavedObject ) NHibernate will first load the entity, then apply changes from the unsaved object. That process will then populate OldValues. This is using version 3.1. I believe earlier versions used SaveOrUpdateCopy().