Configure Envers RevisionListener via DI

To add audit trail to our application, we decided to use NHibernate.Envers. To enable application-specific change tracking, DefaultRevisionEntity been extended with user 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 to use a RevisionListener depending on the RevisionEntity attribute that your class is decorated with:

 [RevisionEntity( typeof( RevisionListener ) )] 

I use the ServiceLocator template to enter my access in the RevisionListener . This is currently the only place I have to use ServiceLocator and really want to get rid of it.

Is there any other flexible way to insert my UserAccessor into RevisionEntity?

+4
source share
1 answer

No, you cannot today.

However, that sounds good. Please add your JIRA ticket here http://nhibernate.jira.com/browse/NHE

Without giving it too many thoughts, I think it will be quite difficult to allow users to do this only with attribute configurations (if so, then the IoC socket must be built in). This can probably be done by allowing you to insert a one-time auditor syntax "somewhere close to the IntegrateWithEnvers method."

Relationship Roger

+3
source

All Articles