This question is largely based on the article NHibernate – Automatic change tracking for aggregate roots in DDD scenarios
Although the logic in the article seems sound I have yet to find an implementation solution that will cover all use cases. The problem seems to be related to the following paragraph from the article
There is a slight problem here that we may generate several updates per transaction here, but I am not worried about that overly much, it is fairly simple to resolve (by keeping track of the entity and not updating if we already updated in the current transaction), so I’ll leave it up to you.
Following the article we simply force a version update whenever we update a related entity within the aggregate root. However in cases where both aggregate root and related entity are 'dirty' this will cause a double update on the aggregate root. This causes nhibernate to fall over as the second version update triggered by default from the dirty aggregate root expects the version to be the same as what was loaded from the db.
I've attempted to put a check into the 'PreInsertEventListener' and 'PreUpdateEventListener' checking if the aggregate root is dirty when updating a related entity. If this is the case then ignore the forced update of version.
public bool OnPreUpdate(PreUpdateEvent updateEvent)
{
var rootFinder = updateEvent.Entity as ICanFindMyAggregateRoot;
if (rootFinder == null)
return false;
if (!updateEvent.Session.IsAggregateRootDirty(rootFinder.MyRoot))
{
updateEvent.Session.Lock(rootFinder.MyRoot, LockMode.Force);
}
return false;
}
public static class SessionExtensions
{
public static bool IsAggregateRootDirty(this ISession session, IAggregateRoot entity)
{
ISessionImplementor sessionImplementation = session.GetSessionImplementation();
IPersistenceContext persistenceContext = sessionImplementation.PersistenceContext;
IEntityPersister entityPersister = sessionImplementation.GetEntityPersister(null, entity);
EntityEntry entityEntry = persistenceContext.GetEntry(entity);
if ((entityEntry == null) && (entity is INHibernateProxy))
{
INHibernateProxy proxy = entity as INHibernateProxy;
object obj = sessionImplementation.PersistenceContext.Unproxy(proxy);
entityEntry = sessionImplementation.PersistenceContext.GetEntry(obj);
}
object[] oldState = entityEntry.LoadedState;
object[] currentState = entityPersister.GetPropertyValues(entity, sessionImplementation.EntityMode);
int[] findDirty = entityEntry.Persister.FindDirty(currentState, oldState, entity, sessionImplementation);
var hasDirtyCollection = currentState.OfType<IPersistentCollection>().Any(x => x.IsDirty);
return (findDirty != null) || hasDirtyCollection;
}
}
This solution does seem to work albeit I still need to test it with few more use cases. However I feel as if this solution is a bit heavy handed and was hoping for a solution more along what was outlined in the article. Is there a way to detect weather the version has already been updated in the same transaction or will be, or a simple way to keep track of entities within the transaction set have its version updated.
Thanks.