Just sharing some of my inconsequential lunch conversations with you... RSS  

Monday, August 06, 2007

Interception NHibernate activity

We intercept NHibernate activity for the following reasons:

  • logging
  • authorization validation
  • generic validation
  • trigger events
The intercepting mechanism is a generic one, where we deliver each of the life cycle triggers to the super class all domain model classes derive from. On this implementation we've just derived this parent of all classes from NHibernate.IInterceptor as virtual methods with empty implementations - other than the generic logging. For example:


public bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
{
Model.EntityObject entityObject = entity as EntityObject;

if (entityObject == null)
{
return false;
}

return entityObject.OnFlushDirty(entity, id, currentState, previousState, propertyNames, types);
}



The way we hooked generic logging is quiet simple: we've intercepted OnFlushDirty and are just logging object[] currentState.

The entity update upon trigger is not immediate. Other than just setting properties on parameter entity, we have to set currentstate:

currentState[(int)propertyNames["MyProperty"]] = ...;

There goes the strongly typed advantage...

No comments:

Development Catharsis :: Copyright 2006 Mário Romano