Los Techies : Blogs about software and anything tech!

Creating a Timestamp Interceptor in NHibernate


In a previous post, I gave an example of a Timestamp class and how one might create an ICompositeUserType to map it within NHibernate. Here I want to show of an example of an IInterceptor which will automatically populate the values for my Timestamp class. OnSave is for the inserts, and OnFlushDirty is for the updates. There are a bunch of other methods that you can tap into for different things, so check out the NHibernate docs.
 
public class TimestampInterceptor : EmptyInterceptor
{
    private const string TIMESTAMP_PROPERTY = "Timestamp";

    private readonly IDomainContext domainContext;
    private readonly ISystemClock clock;

    public TimestampInterceptor(IDomainContext domainContext, ISystemClock clock)
    {
        this.domainContext = domainContext;
        this.clock = clock;
    }

    public override bool OnSave(object entity, object id, object[] state, 
        string[] propertyNames, IType[] types)
    {
        var timestampable = entity as ITimestampable;

        if(timestampable == null)
            return false;

        var timestamp = GetTimestamp(state, propertyNames);

        timestamp.CreatedDateTime = clock.Now();

        if (domainContext.DomainUser != null)
            timestamp.CreatedByStaff = domainContext.DomainUser.StaffName;

        return true;
    }

    public override bool OnFlushDirty(object entity, object id, object[] currentState, 
        object[] previousState, string[] propertyNames, IType[] types)
    {
        var timestampable = entity as ITimestampable;

        if (timestampable == null)
            return false;

        var timestamp = GetTimestamp(currentState, propertyNames);

        timestamp.UpdatedDateTime = clock.Now();

        if(domainContext.DomainUser != null)
            timestamp.UpdatedByStaff = domainContext.DomainUser.StaffName;

        return true;
    }

    private static Timestamp GetTimestamp(object[] state, string[] propertyNames)
    {
        var timestamp = state[Array.IndexOf(propertyNames, TIMESTAMP_PROPERTY)] as Timestamp;

        if( timestamp == null )
        {
            timestamp = new Timestamp();
            state[Array.IndexOf(propertyNames, TIMESTAMP_PROPERTY)] = timestamp;
        }

        return timestamp;
    }
}
 
I haven't run this through the ringers yet, so let me know if you spot some problems.
Technorati Tags: ,
Kick It on DotNetKicks.com
Posted Mar 27 2008, 10:28 PM by Ray Houston

Comments

Elegant Code » Implementing NHibernate Interceptors wrote Elegant Code » Implementing NHibernate Interceptors
on 05-15-2008 1:30 PM

Pingback from  Elegant Code » Implementing NHibernate Interceptors

Dave the Ninja wrote re: Creating a Timestamp Interceptor in NHibernate
on 06-18-2008 7:23 PM

Ray, I have been battleing tirelessly trying to get this example working with my castle/nhibernate/rhino-tools (binsor) setup.

I can get the interceptor to fire then it spews with a IInterceptor not implemented error.

Correct me if im wrong, but EmptyInterceptor implements IInterceptor - very confusing.

Have you got this running with binsor, registering the Interceptor as a component in the boo file?

Any advice would be great!

David

Dave The Ninja wrote re: Creating a Timestamp Interceptor in NHibernate
on 06-19-2008 3:43 AM

Got it sorted!

Was mixing IInterceptor's between Castle and NHIbernate - whoops!

David

Marcus wrote re: Creating a Timestamp Interceptor in NHibernate
on 10-07-2009 3:47 AM

Hi, very nice example, works great for simple values, but when I try to modify a collection in the currentState it doesn't work because "NHibernate.Collection.Generic.PersistentGenericBag`1cannot be converted to a "System.Collections.Generic.List`1[]"

Any Ideas?

Thanks!

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems