ABP.IO – 处理同一微服务中的多个事件

huangapple go评论61阅读模式
英文:

ABP.IO - Handle multiple events in same microservice

问题

根据以下文档和示例来处理不同事件,我需要拥有3个微服务来处理资源的创建、更新和删除吗?还是我需要编写类似以下的代码?

namespace AbpDemo
{
    public class MyHandler
        : IDistributedEventHandler<EmployeeServiceEto>,
          ITransientDependency
    {
        public async Task HandleEventAsync(EmployeeServiceEto eventData)
        {
            if (eventData.EventType = EventType.Created)
                // 调用创建员工的方法
            if (eventData.EventType = EventType.Updated)
                // 调用更新员工的方法
            if (eventData.EventType = EventType.GetAll)
                // 调用获取所有员工的方法
        }
    }
}

有人可以帮我提供使用 abp.io 框架在同一个微服务中处理多个事件的示例吗?

英文:

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&lt;EmployeeServiceEto&gt;,
		  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?

答案1

得分: 1

ABP Framework提供了三种预定义的事件类型,例如EntityCreatedEto<T>EntityUpdatedEto<T>EntityDeletedEto<T>。您可以使用这些类型来订阅创建、更新和删除事件。

您可以查看以下示例:

namespace AbpDemo
{
    public class MyHandler : 
          IDistributedEventHandler<EntityCreatedEto<EmployeeServiceEto>>,
          IDistributedEventHandler<EntityUpdatedEto<EmployeeServiceEto>>,
          IDistributedEventHandler<EntityDeletedEto<EmployeeServiceEto>>,
          ITransientDependency
    {
       public virtual async Task HandleEventAsync(EntityCreatedEto<EmployeeServiceEto> eventData)
       {
          //当新实体创建时的逻辑
       }

       public virtual async Task HandleEventAsync(EntityUpdatedEto<EmployeeServiceEto> eventData)
       {
          //当实体更新时的逻辑
       }

       public virtual async Task HandleEventAsync(EntityDeletedEto<EmployeeServiceEto> eventData)
       {
          //当实体删除时的逻辑
       }
    }
}

如果您像这样操作,那么您就不需要检查EventType。ABP框架的示例可以在这里查看。

英文:

ABP Framework provides three pre-defined event types such as EntityCreatedEto&lt;T&gt;, EntityUpdatedEto&lt;T&gt;, and EntityDeletedEto&lt;T&gt;. 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&lt;EntityCreatedEto&lt;EmployeeServiceEto&gt;&gt;,
          IDistributedEventHandler&lt;EntityUpdatedEto&lt;EmployeeServiceEto&gt;&gt;,
          IDistributedEventHandler&lt;EntityDeletedEto&lt;EmployeeServiceEto&gt;&gt;,
          ITransientDependency
    {
       public virtual async Task HandleEventAsync(EntityCreatedEto&lt;EmployeeServiceEto&gt; eventData)
       {
          //your logic for when a new entity created
       }

       public virtual async Task HandleEventAsync(EntityUpdatedEto&lt;EmployeeServiceEto&gt; eventData)
       {
          //your logic for when the entity updated
       }

       public virtual async Task HandleEventAsync(EntityDeletedEto&lt;EmployeeServiceEto&gt; 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.

huangapple
  • 本文由 发表于 2023年5月10日 21:19:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定