I think, that it is a common practice to store information about creation and modification date of entities. However keeping appropriate columns up to date manually is error prone. We must remember that every time we modify entity, we also have to change its modification date. Fortunately thanks to event listeners, NHibernate can do all of this boring stuff for us. Let’s start from creating class which implements two interfaces:
- IPreInsertEventListener
- IPreUpdateEventListener
1 2 3 4 5 6 7 8 9 10 11 12 |
public class NHListener : IPreInsertEventListener, IPreUpdateEventListener { public bool OnPreInsert(PreInsertEvent @event) { throw new NotImplementedException(); } public bool OnPreUpdate(PreUpdateEvent @event) { throw new NotImplementedException(); } } |
As You probably guessed OnPreInsert method is invoked before insert, and OnPreUpdate is called before update of entity. Having prepared the basic skeleton of class, it is time to configure NHibernate to use our event listeners. The basic configuration is taken from my previous post about NHibernate automappings, so I will only extend it by appending event listeners
1 2 3 4 5 6 7 8 9 10 |
Fluently.Configure().Database( MsSqlConfiguration.MsSql2008.ConnectionString( @"YourConnectionString")) .Mappings(val => val.AutoMappings.Add(CreateAutomappings)) .ExposeConfiguration(val => { val.AppendListeners(ListenerType.PreUpdate, new IPreUpdateEventListener[] { new NHListener() }); val.AppendListeners(ListenerType.PreInsert, new IPreInsertEventListener[] { new NHListener() }); }) .BuildSessionFactory(); |
Now we can put some logic into OnPreInsert and OnPreUpdate functions. First of all we have to somehow identify entities which hold information about creation and modification time. In order to do that I created simple interface IDateInfo
1 2 3 4 5 |
public interface IDateInfo { DateTime ModificationDate { get; set; } DateTime CreationDate { get; set; } } |
In the next step we have to modify OnPreInsert and OnPreUpdate methods, so that they can set creation and modification date of entities which implement IDateInfo interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class NHListener : IPreInsertEventListener, IPreUpdateEventListener { public bool OnPreInsert(PreInsertEvent @event) { IDateInfo dateInfo; if ((dateInfo = @event.Entity as IDateInfo) != null) { var currentDate = DateTime.Now; dateInfo.CreationDate = dateInfo.ModificationDate = currentDate; } return false; } public bool OnPreUpdate(PreUpdateEvent @event) { IDateInfo dateInfo; if ((dateInfo = @event.Entity as IDateInfo) != null) { var currentDate = DateTime.Now; dateInfo.ModificationDate = currentDate; } return false; } } |
Unfortunately at this moment saving entity (which implements IDateInfo interface) will throw exception
Despite the fact that we set values of ModificationDate and CreationDate properties , NHiberante seems not to notice that. That is why we have to manually update values of State object from PreInsertEvent and PreUpdateEvent. According to the documentation State object from class PreInsertEvent holds values which should be inserted and State object from class PreUpdateEvent holds values which should be updated. Function which update values of state object might look like that
1 2 3 4 5 6 7 |
private void SetState(IEntityPersister persister, object[] state, string propertyName, object value) { var index = Array.IndexOf(persister.PropertyNames, propertyName); if (index == -1) return; state[index] = value; } |
Updated NHListener class now looks like that
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System; using System.Linq.Expressions; using NHibernate.Event; using NHibernate.Impl; using NHibernate.Persister.Entity; namespace DataAccessLayer { public class NHListener : IPreInsertEventListener, IPreUpdateEventListener { private static readonly string ModificationDatePropertyName = GetPropertyName<IDateInfo>(val => val.ModificationDate), CreationDatePropertyName = GetPropertyName<IDateInfo>(val => val.CreationDate); public bool OnPreInsert(PreInsertEvent @event) { IDateInfo dateInfo; if ((dateInfo = @event.Entity as IDateInfo) != null) { var currentDate = DateTime.Now; dateInfo.CreationDate = dateInfo.ModificationDate = currentDate; SetState(@event.Persister, @event.State, ModificationDatePropertyName, currentDate); SetState(@event.Persister, @event.State, CreationDatePropertyName , currentDate); } return false; } public bool OnPreUpdate(PreUpdateEvent @event) { IDateInfo dateInfo; if ((dateInfo = @event.Entity as IDateInfo) != null) { var currentDate = DateTime.Now; dateInfo.ModificationDate = currentDate; SetState(@event.Persister, @event.State, ModificationDatePropertyName, currentDate); } return false; } private void SetState(IEntityPersister persister, object[] state, string propertyName, object value) { var index = Array.IndexOf(persister.PropertyNames, propertyName); if (index == -1) return; state[index] = value; } private static string GetPropertyName<TType>(Expression<Func<TType, object>> expression) { return ExpressionProcessor.FindPropertyExpression(expression.Body); } } } |
As You can see, after setting values of IDateInfo I also call function SetState and update state of appropriate properties. From now everything works fine and our entities can be saved without problems. Source code for this post can be found here