Andreas
Andreas

Reputation: 5309

Configure an Envers RevisionListener via DI

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?

Upvotes: 4

Views: 1793

Answers (1)

Roger
Roger

Reputation: 1950

No, you can't today.

However - it sounds like a nice feature. Please add a JIRA ticket about it here http://nhibernate.jira.com/browse/NHE

Without giving it too much thoughts, I think it'll be pretty hard to enable users to do this with only attribute configuration though (if so, sort of an IoC needs to be built internally). Probably it can be accomplished by allowing to inject a revision listener singleton "somewhere close to IntegrateWithEnvers method".

Regards Roger

Upvotes: 3

Related Questions