NHibernate Envers: Property Value Based Auditing Object

I have a very specific audit need.

Consider the following class (I just changed the name of my classes and simplified unnecessary code)

[Audited]
public class Client
{
  [NotAudited]
  public virtual IList<Order> Orders {get; set;}
}

      

The client object should only be validated when the Orders property is NOT empty.

Is it even possible? If so, how do I do it?

+3


source to share


1 answer


If you want to disable runtime auditing based on some state, you can create your own subclass AuditEventListener

and pass an instance of that type to the method IntegrateWithEnvers

.

In your subclass, you can override OnPostDelete

, OnPostInsert

, OnPostRecreateCollection

, OnPostUpdate

, OnPreRemoveCollection

and OnPreUpdateCollection

. In your case, you should probably check evt.Entity

and evt.AffectedOwnerOrNull

. If you want to audit, just call the base method, if you don't want to audit, do nothing in your implementation.



Note that you should probably just do this if you are just using Envers for simple logging. If you are using it to recreate historical instances, "deleting" the audit for some historical events can cause problems when loading historical instances. If it is for you, it is safe to do so if the entity either has or does not have Orders

for its entire lifetime.

+3


source







All Articles