To add an audit trail to our application we decided to use NHibernate.Envers. To allow app specific tracking of revisions, the DefaultRevisionEntity
was extended with user specific data.
public virtual void NewRevision( object revisionEntity )
{
var revisionData = revisionEntity as Revision;
if( revisionData != null )
{
// Set additional audit data.
var identity = UserAccessor.CurrentIdentity;
revisionData.UserId = identity.UserId;
revisionData.EmployeeId = identity.EmployeeId;
revisionData.UserName = identity.Name;
}
}
Envers decides wich RevisionListener
to use depending on the RevisionEntity
attribute your class is decorated with:
[RevisionEntity( typeof( RevisionListener ) )]
I am using the ServiceLocator pattern to inject my accessor into the RevisionListener
. Currently this is the only place where I have to use a ServiceLocator and really want to get rid of it.
Is there another, flexible way to inject my UserAccessor into the RevisionEntity?