Friday 2 March 2007

NHibernate:How do I know a field is dirty?

The problem arose that I needed to know if a field had changed just prior to saving:

  1. Presentation layer reads object/record
  2. Presentation layer changes object field value
  3. Presentation layer calls business layer to update the object/record
  4. Business layer needs to know if a certain field has changed to perform extra processing
When the record is read (in step 4) from the database it actually comes from the (session) cache and therefore holds the values of the changed (but not yet persisted) fields.

The solution seems to be either clear the cache or tell the business object that the field has changed. The clear-the-cache solution was not tried, would have been along the lines of Session.Evict(object).