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

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

ABP.IO - Handle multiple events in same microservice

问题

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

  1. namespace AbpDemo
  2. {
  3. public class MyHandler
  4. : IDistributedEventHandler<EmployeeServiceEto>,
  5. ITransientDependency
  6. {
  7. public async Task HandleEventAsync(EmployeeServiceEto eventData)
  8. {
  9. if (eventData.EventType = EventType.Created)
  10. // 调用创建员工的方法
  11. if (eventData.EventType = EventType.Updated)
  12. // 调用更新员工的方法
  13. if (eventData.EventType = EventType.GetAll)
  14. // 调用获取所有员工的方法
  15. }
  16. }
  17. }

有人可以帮我提供使用 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?

  1. namespace AbpDemo
  2. {
  3. public class MyHandler
  4. : IDistributedEventHandler&lt;EmployeeServiceEto&gt;,
  5. ITransientDependency
  6. {
  7. public async Task HandleEventAsync(EmployeeServiceEto eventData)
  8. {
  9. if(eventData.EventType = EventType.Created;
  10. // call Create Employee Method
  11. if(eventData.EventType = EventType.Updated;
  12. // call Update Employee Method
  13. if(eventData.EventType = EventType.GetAll;
  14. // call GetAllEmployees
  15. }
  16. }
  17. }

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>。您可以使用这些类型来订阅创建、更新和删除事件。

您可以查看以下示例:

  1. namespace AbpDemo
  2. {
  3. public class MyHandler :
  4. IDistributedEventHandler<EntityCreatedEto<EmployeeServiceEto>>,
  5. IDistributedEventHandler<EntityUpdatedEto<EmployeeServiceEto>>,
  6. IDistributedEventHandler<EntityDeletedEto<EmployeeServiceEto>>,
  7. ITransientDependency
  8. {
  9. public virtual async Task HandleEventAsync(EntityCreatedEto<EmployeeServiceEto> eventData)
  10. {
  11. //当新实体创建时的逻辑
  12. }
  13. public virtual async Task HandleEventAsync(EntityUpdatedEto<EmployeeServiceEto> eventData)
  14. {
  15. //当实体更新时的逻辑
  16. }
  17. public virtual async Task HandleEventAsync(EntityDeletedEto<EmployeeServiceEto> eventData)
  18. {
  19. //当实体删除时的逻辑
  20. }
  21. }
  22. }

如果您像这样操作,那么您就不需要检查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:

  1. namespace AbpDemo
  2. {
  3. public class MyHandler :
  4. IDistributedEventHandler&lt;EntityCreatedEto&lt;EmployeeServiceEto&gt;&gt;,
  5. IDistributedEventHandler&lt;EntityUpdatedEto&lt;EmployeeServiceEto&gt;&gt;,
  6. IDistributedEventHandler&lt;EntityDeletedEto&lt;EmployeeServiceEto&gt;&gt;,
  7. ITransientDependency
  8. {
  9. public virtual async Task HandleEventAsync(EntityCreatedEto&lt;EmployeeServiceEto&gt; eventData)
  10. {
  11. //your logic for when a new entity created
  12. }
  13. public virtual async Task HandleEventAsync(EntityUpdatedEto&lt;EmployeeServiceEto&gt; eventData)
  14. {
  15. //your logic for when the entity updated
  16. }
  17. public virtual async Task HandleEventAsync(EntityDeletedEto&lt;EmployeeServiceEto&gt; eventData)
  18. {
  19. //your logic for when the entity deleted
  20. }
  21. }
  22. }

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:

确定