英文:
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<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?
答案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<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论