views:

94

answers:

2

Let's say I have a Type II SCD database, that is basically append only. I am using NHibernate to persist objects to my database. I have an object like so:

Pony
|- int Id
|- Guid EntityId
|- string PonyName
|- string PonyColor
|- int RevisionValidFrom
|- int RevisionValidTo

Here's a typical scenario:

Pony myLittlePony = myStable.GetLatestPonyByGuid("0f1ac08a-3328-43db-b278-77c272e4fea3");
myLittlePony.PonyColor = "Fish";
myNHSession.Save(myLittlePony);

I want to be able to call Session.Save(myLittlePony) and have NHibernate UPDATE the old entity's RevisionValidTo to whatever I specify and then INSERT the modified Pony as a new row with a new Id, basically as if it were a brand new object being persisted to the DB.

A: 

This can be done with custom SQL for update.

Lachlan Roche
A: 

I ended up creating my own SaveOrUpdateEventListener and registering it in the configuration for NHibernate. I then issue a raw SQL command.

For the NHibernate config:

var listener = new PonySaveOrUpdateEventListener();
config.SetListener(NHibernate.Event.ListenerType.SaveUpdate, listener);
config.SetListener(NHibernate.Event.ListenerType.Save, listener);

The class:

public class PonySaveOrUpdateEventListener : DefaultSaveOrUpdateEventListener
{
    protected override object EntityIsPersistent(SaveOrUpdateEvent @event)
    {
        Pony ent = @event.Entity as Pony;

        // I don't care if it's not a pony or if the entity doesn't need updating, call base!
        if (ent == null || !IsDirty(@event))
            return base.EntityIsPersistent(@event);

        // Do nasty dialect-specific raw SQL because using NH to update this row throws us into an infinite loop
        string tablename = ((ILockable)@event.Entry.Persister).RootTableName.ToLower();
        System.Data.IDbCommand command = ((ISession)@event.Session).Connection.CreateCommand();
        command.CommandText = String.Format("update {0} set RevisionValidTo = {1} where Id = '{2}'", tablename, CurrentRevision.Id, ent.Id);
        command.ExecuteNonQuery();

        // Make the event look like it was never persistent and force a transient insert of the entity
        ent.Id = Guid.Empty;
        @event.Entry = null;
        return EntityIsTransient(@event);
    }

    protected override object EntityIsTransient(SaveOrUpdateEvent @event)
    {
        Pony ent = @event.Entity as Pony;
        if (ent == null)
            return base.EntityIsTransient(@event);

        ent.RevisionValidFrom = Host.NextRevision;
        ent.RevisionValidTo = null;

        return base.EntityIsTransient(@event);
    }

    private static bool IsDirty(SaveOrUpdateEvent @event)
    {
        IEntityPersister persister = @event.Entry.Persister;
        object[] oldState = @event.Entry.LoadedState;
        object[] currentState = persister.GetPropertyValues(@event.Entity, @event.Session.EntityMode);
        Int32[] dirtyProps = persister.FindDirty(currentState, oldState, @event.Entity, @event.Session);
        return dirtyProps != null;
    }
}
snicker
Didn't the ISession.SaveOrUpdate() method do the job?
Will Marcouiller
@Will: Do what job? Did you read the question?
snicker