Base solution for your next web application
Open Closed

EventBus Domain Events to update the entity itself? #4104


User avatar
0
soonjoo created

Hello team,

How do I use EventBus Domains Events and to update the entity itself? For example, I have Task Entity, and whenever Task is updated/created/delete, besides adding a record to another table, I also need to update a column on the Task entity itself. However, when I do it, it goes into an infinite loop because the update on the Task Entity will trigger another Update Event.

I am building a revision tracker so I'm using Domain Events to keep revisions of any change to the Task Entity, and I need to update the main Task Entity with the latest revision Id.

Thanks.


2 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    There are a few ways:

    • Check if the CreationTime, LastModificationTime or DeletionTime is not within X period of time.
    • Maintain a boolean dictionary somewhere.
    • Maintain a boolean property on the Task entity itself. [list:9u85y1vp] [*:9u85y1vp] Add a [NotMapped] property to Task entity:
    public class Task : Entity<long>
    {
        [NotMapped]
        public bool IsRevisionHandled { get; set; }
    }
    
    • Check and set that property when handled:
    [UnitOfWork]
    public virtual void HandleEvent(EntityUpdatedEventData<Task> eventData)
    {
        var task = eventData.Entity;
    
        if (task.IsRevisionHandled)
        {
            return;
        }
    
        task.IsRevisionHandled = true;
    
        // ...
    }
    ```[/*:m:9u85y1vp][/list:o:9u85y1vp]
    
  • User Avatar
    0
    soonjoo created

    Thank you for the help, I will try to maintain a boolean dictionary on another table