Configure an Envers RevisionListener via DI

喜你入骨 提交于 2019-11-28 05:19:57

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/6846996/configure-an-envers-revisionlistener-via-di

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!