开发者

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;
  }
开发者_JS百科}

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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜