Search code examples
aspnetboilerplate

Entity history tracking and event handlers abp v3.4.0


I've just updated to abp v3.4.0 and i'm currently investigating the History tracking feature as i think this could be quite useful to me.

I'm just not sure how to actually get the information out and how it relates to the rest of the Abp system.

So this is how i'm thinking it should work in my mind.

  1. a property of an Entity is updated i.e Enabled => Disabled.
  2. subscribe to the Entity Updated Event.
  3. check that the entity went from Enabled => Disabled via the Entity History.
  4. perform some other actions.

Now the thing i am not sure about is how to get the Entity History information from the Updated event.

I've tried using the repository private readonly IRepository<EntityChangeSet,long> _entityChangeRepository; in the IEventHandler but this doesn't seem to "Save Changes" before the Updated event is triggered.

So what would be the correct way to access the Entity History information? Is it even possible to access it through an Event?

Thanks!


Solution

  • Entity History is for audit purposes.

    EntityChangeSet is saved after your entity is updated (and after its Updated event is triggered).

    Is it even possible to access it through an Event?

    Bad news: You cannot access it (without extensive hacking) through your entity's Updated event.

    Good news: But you can access it through an event.

    So what would be the correct way to access the Entity History information?

    Access it in the Created event of EntityChangeSet entity.

    Implement an event handler for IEventHandler<EntityCreatedEventData<EntityChangeSet>.

    There, you can:

    • check if your entity is in EntityChangeSet.EntityChanges,
    • check if the EntityChange.ChangeType is EntityChangeType.Updated,
    • check if the property is in EntityChange.PropertyChanges,
    • "check that the entity went from Enabled => Disabled via the" PropertyChange, and finally
    • "perform some other actions."

    Again, Entity History is for audit purposes so this isn't straightforward. But it's now possible! :)