Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate set properties automatically on save

I'm using DDD and NHibernate to persist my domain object. In my database every table that is related to an entity has four extra columns (CreatedBy, UpdatedBy, CreatedDate, UpdatedDate) used for audit tracking. I am using the repository pattern that only allows aggregate roots to be saved. When NHibernate tries to save the entities on the aggregate root, I get a SQL DateTime error because the entities don't have their audit properties set. Is there a way in NHibernate to set properties on objects just before save?

Here is a little example. I have an Order object which is the aggregate root. I also have OrderNote objects that are children of the order. When I add an OrderNote to the Order and then save the Order an exception is thrown because the CreatedDate/UpdatedDate are set to DateTime.MinValue which will cause a SQL DateTime overflow. Since these audit columns are part of persistence and not related to the problem domain, I don't want the Order aggregate root to set these properties when the note is added. The audit columns/properties should only be known by the persistence framework and not the domain. I would like to be able to tell NHibernate to set these properties when saving or updating. Is there a way to do this?

like image 304
awilinsk Avatar asked Jul 20 '09 13:07

awilinsk


2 Answers

Interceptors can do this:

http://www.nhforge.org/doc/nh/en/index.html#manipulatingdata-interceptors (9.10, link doesn't always work).

Cut/paste from the document:

public class AuditInterceptor : IInterceptor
{
    ...

    public boolean OnSave(object entity,
                          object id,
                          object[] state,
                          string[] propertyNames,
                          IType[] types)
    {
        if ( entity is IAuditable )
        {
            for ( int i=0; i<propertyNames.Length; i++ )
            {
                if ( "CreateTimestamp" == propertyNames[i] )
                {
                    state[i] = DateTime.Now;
                    return true;
                }
            }
        }
        return false;
    }

}

OnFlushDirty() could be used for UpdatedDate.

You are going to want every entity that has a CreatedDate/UpdatedDate to implement an interface so that the interceptor can check whether it should act (in the provided example it is IAuditable).


Edit

Just noticed chapter 11: Interceptors and events. You can inherit from EmptyInterceptor which makes things easier if you only need to override some of the methods.

I've never used Events.

like image 173
anonymous Avatar answered Nov 16 '22 00:11

anonymous


You could also look at NHibernate's versioning system, which can help you in your case ;)

like image 24
Oliver Hanappi Avatar answered Nov 15 '22 23:11

Oliver Hanappi