Search code examples
c#microservicesabp-frameworkevent-driven

ABP.IO - Handle multiple events in same microservice


Considering the below documentation and the example to handle different events

https://docs.abp.io/en/abp/latest/Distributed-Event-Bus#pre-defined-events

Do I need to have 3 microservices to handle create, update & Delete for a resource?

or Do I need to write something like this?

namespace AbpDemo
{
    public class MyHandler
        : IDistributedEventHandler<EmployeeServiceEto>,
          ITransientDependency
    {
        public async Task HandleEventAsync(EmployeeServiceEto eventData)
        {
            if(eventData.EventType = EventType.Created;
                // call Create Employee Method
            if(eventData.EventType = EventType.Updated;
                // call Update Employee Method
            if(eventData.EventType = EventType.GetAll;
                // call GetAllEmployees
            
        }
    }
}

Can someone help me with an example of handling multiple events in the same microservice using apb.io framework?


Solution

  • ABP Framework provides three pre-defined event types such as EntityCreatedEto<T>, EntityUpdatedEto<T>, and EntityDeletedEto<T>. You can use these types to subscribe for create, update, and delete events.

    You can see the following example:

    namespace AbpDemo
    {
        public class MyHandler : 
              IDistributedEventHandler<EntityCreatedEto<EmployeeServiceEto>>,
              IDistributedEventHandler<EntityUpdatedEto<EmployeeServiceEto>>,
              IDistributedEventHandler<EntityDeletedEto<EmployeeServiceEto>>,
              ITransientDependency
        {
           public virtual async Task HandleEventAsync(EntityCreatedEto<EmployeeServiceEto> eventData)
           {
              //your logic for when a new entity created
           }
    
           public virtual async Task HandleEventAsync(EntityUpdatedEto<EmployeeServiceEto> eventData)
           {
              //your logic for when the entity updated
           }
    
           public virtual async Task HandleEventAsync(EntityDeletedEto<EmployeeServiceEto> eventData)
           {
              //your logic for when the entity deleted
           }
        }
    }
    

    If you do it like that, then you don't need to check the EventType. An example from the ABP Framework can be seen here.