本次做了破坏性升级,支持CQRS模式、支持订阅事件处理、异步发布事件。
事件总线,即EventBus,是一种解耦模块间通讯的强大工具。在CodeWF.EventBus库中,我们得以轻松实现CQRS模式,并通过清晰、简洁的接口进行事件订阅与发布。接下来,我们将详细探讨如何使用这个库来处理事件。
CQRS,全称Command Query Responsibility Segregation,是一种软件架构模式,旨在通过将系统中的命令(写操作)和查询(读操作)职责进行分离,来提高系统的性能、可伸缩性和响应性。
CodeWF.EventBus适用于进程内事件传递(无其他外部依赖),与MediatR功能类似。MediatR库侧重于ASP.NET Core设计,且其功能更加强大,CodeWF.EventBus库优势:
首先请搜索 NuGet 包CodeWF.EventBus
并安装最新版,安装完成后,你就可以在你的代码中引用并使用CodeWF.EventBus
了。
如果是 ASP.NET Core 程序,比如 MVC、Razor Pages、Blazor Server 等模板程序,在Program
中添加如下代码:
// ....
// 1、注册事件总线,将标注`EventHandler`特性方法的类采用单例方式注入IOC容器
EventBusExtensions.AddEventBus(
(t1, t2) => builder.Services.AddSingleton(t1, t2),
t => builder.Services.AddSingleton(t),
Assembly.GetExecutingAssembly());
var app = builder.Build();
// ...
// 2、将上面已经注入IOC容器的类取出、关联处理方法到事件总线管理
EventBusExtensions.UseEventBus(t => app.Services.GetRequiredService(t), Assembly.GetExecutingAssembly());
// ...
AddEventBus
方法会扫描传入的程序集列表,将标注Event
特性的类下又标注EventHandler
特性方法的类采用单例方式注入 IOC 容器。UseEventBus
方法会将上一步注入的类通过 IOC 容器获取到实例,将实例的事件处理方法注册到事件管理队列中去,待收到事件发布时,会从事件管理队列中查找事件处理方法并调用,达到事件通知的功能。如果使用的其他 IOC容器,比如 WPF 中使用了 Prism 框架的DryIoc容器,写法如下:
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
IContainer? container = containerRegistry.GetContainer();
// ...
// Register EventBus
EventBusExtensions.AddEventBus(
(t1,t2)=> containerRegistry.RegisterSingleton(t1,t2),
t=> containerRegistry.RegisterSingleton(t),
typeof(App).Assembly);
// ...
// Use EventBus
EventBusExtensions.UseEventBus(t => container.Resolve(t), typeof(App).Assembly);
}
根据 IOC 容器注册单例、获取服务的 API 不同,做相应修改即可。
默认的 WPF、Winform、AvaloniaUI、控制台程序默认未引入任何 IOC 容器,这里不用做事件服务注册操作,功能使用上和使用IOC只差自动订阅功能,其他功能一样。
在这里我们使用 CQRS 来完成我们程序业务逻辑,在 CQRS 模式中我们的查询和其它业务操作是分开的。不了解 CQRS 的可以看看这篇文章:https://learn.microsoft.com/zh-cn/azure/architecture/patterns/cqrs
在CQRS模式中,命令代表写操作。定义命令类,这些类继承自Command
类
public class CreateProductCommand : Command
{
public string Name { get; set; }
public decimal Price { get; set; }
}
public class CreateProductSuccessCommand : Command
{
public string Name { get; set; }
public decimal Price { get; set; }
}
public class DeleteProductCommand : Command
{
public Guid ProductId { get; set; }
}
在CQRS模式中,查询代表读操作。查询需要等待得到回应,适用于请求/响应。使用查询,调用方只需要关心我需要使用XXQuery,而不必操心我需要XXXService、AAService。定义查询类,继承自Query<T>
:
public class ProductQuery : Query<ProductItemDto>
{
public Guid ProductId { get; set; }
public override ProductItemDto Result { get; set; }
}
public class ProductsQuery : Query<List<ProductItemDto>>
{
public string Name { get; set; }
public override List<ProductItemDto> Result { get; set; }
}
Query<T>
中T表示查询响应结果类型,在查房中使用Result
属性表示。
在B/S
程序中,一般将事件处理程序单独封装到一个类中,文章开头贴的代码中CommandAndQueryHandler
即是自动订阅类格式,这里我们再贴上讲解:
[Event]
public class CommandAndQueryHandler(IEventBus eventBus, IProductService productService)
{
[EventHandler]
public async Task ReceiveCreateProductCommandAsync(CreateProductCommand command)
{
var isAddSuccess = await productService.AddProductAsync(new CreateProductRequest()
{ Name = command.Name, Price = command.Price });
if (isAddSuccess)
{
await eventBus.PublishAsync(this,
new CreateProductSuccessCommand() { Name = command.Name, Price = command.Price });
}
else
{
Console.WriteLine("Create product fail");
}
}
[EventHandler(Order = 2)]
public async Task ReceiveCreateProductSuccessCommandSendEmailAsync(CreateProductSuccessCommand command)
{
Console.WriteLine($"Now send email notify create product success, name is = {command.Name}");
await Task.CompletedTask;
}
[EventHandler(Order = 1)]
public async Task ReceiveCreateProductSuccessCommandSendSmsAsync(CreateProductSuccessCommand command)
{
Console.WriteLine($"Now send sms notify create product success, name is = {command.Name}");
await Task.CompletedTask;
}
[EventHandler(Order = 3)]
public void ReceiveCreateProductSuccessCommandCallPhone(CreateProductSuccessCommand command)
{
Console.WriteLine($"Now call phone notify create product success, name is = {command.Name}");
}
[EventHandler]
public async Task ReceiveDeleteProductCommandAsync(DeleteProductCommand command)
{
var isRemoveSuccess = await productService.RemoveProductAsync(command.ProductId);
Console.WriteLine(isRemoveSuccess ? "Remote product success" : "Remote product fail");
}
[EventHandler]
public async Task ReceiveProductQueryAsync(ProductQuery query)
{
var product = await productService.QueryProductAsync(query.ProductId);
query.Result = product;
}
[EventHandler]
public async Task ReceiveAutoProductsQueryAsync(ProductsQuery query)
{
var products = await productService.QueryProductsAsync(query.Name);
query.Result = products;
}
}
CommandAndQueryHandler
添加了Event
特性,在 IOC 容器注入时标识为可以做为单例注入。EventHandler
特性的方法拥有处理事件的能力,该方法只能有一个事件类型参数;如果方法支持异步,也只支持Task
返回值,不能加泛型声明(加了无效)。使用 IOC 容器的程序会自动将标注Event
特性的类做为单例注入容器,事件总线收到事件通知时自动查找标注EventHandle
特性的方法进行调用,达到事件通知的功能。
对于未标注Event
特性的类,可手动注册事件处理程序,如下是未使用 IOC容器时手动注册示例(核心是EventBus.Default
使用):
internal class CommandAndQueryHandler
{
internal void ManuSubscribe()
{
EventBus.Default.Subscribe<DeleteProductCommand>(this, ReceiveDeleteProductCommandAsync);
EventBus.Default.Subscribe<ProductQuery>(this, ReceiveProductQueryAsync);
}
public async Task ReceiveDeleteProductCommandAsync(DeleteProductCommand command)
{
}
public async Task ReceiveProductQueryAsync(ProductQuery query)
{
}
}
上面挨个注册处理方法有时会过于啰嗦,可以简化:
internal class CommandAndQueryHandler
{
internal CommandAndQueryHandler()
{
EventBus.Default.Subscribe(this);
}
[EventHandler(Order = 2)]
public async Task ReceiveCreateProductSuccessCommandSendEmailAsync(CreateProductSuccessCommand command)
{
Console.WriteLine($"Now send email notify create product success, name is = {command.Name}");
await Task.CompletedTask;
}
[EventHandler(Order = 1)]
public async Task ReceiveCreateProductSuccessCommandSendSmsAsync(CreateProductSuccessCommand command)
{
Console.WriteLine($"Now send sms notify create product success, name is = {command.Name}");
await Task.CompletedTask;
}
[EventHandler(Order = 3)]
public void ReceiveCreateProductSuccessCommandCallPhone(CreateProductSuccessCommand command)
{
Console.WriteLine($"Now call phone notify create product success, name is = {command.Name}");
}
[EventHandler]
public async Task ReceiveDeleteProductCommandAsync(DeleteProductCommand command)
{
var isRemoveSuccess = await productService.RemoveProductAsync(command.ProductId);
Console.WriteLine(isRemoveSuccess ? "Remote product success" : "Remote product fail");
}
[EventHandler]
public async Task ReceiveProductQueryAsync(ProductQuery query)
{
var product = await productService.QueryProductAsync(query.ProductId);
query.Result = product;
}
[EventHandler]
public async Task ReceiveAutoProductsQueryAsync(ProductsQuery query)
{
var products = await productService.QueryProductsAsync(query.Name);
query.Result = products;
}
}
使用了 IOC容器,可以注入IEventBus
服务替换EventBus.Default
使用,EventBus
是IEventBus
接口的默认实现,EventBus.Default
是单例引用。
public class EventBusTestViewModel : ViewModelBase
{
private readonly IEventBus _eventBus;
public MessageTestViewModel(IEventBus eventBus)
{
_eventBus = eventBus;
_eventBus.Subscribe(this);
}
[EventHandler]
public async Task ReceiveDeleteProductCommandAsync(DeleteProductCommand command)
{
var isRemoveSuccess = await productService.RemoveProductAsync(command.ProductId);
Console.WriteLine(isRemoveSuccess ? "Remote product success" : "Remote product fail");
}
}
手动订阅可以在 WPF 的 ViewModel 中使用(代码如上),也可以在 IOC 其他生命周期的服务中使用:
public class TimeService : ITimeService
{
private readonly IEventBus _eventBus;
public TimeService(IEventBus eventBus)
{
_eventBus = eventBus;
_eventBus.Subscribe(this);
}
[EventHandler]
public async Task ReceiveDeleteProductCommandAsync(DeleteProductCommand command)
{
var isRemoveSuccess = await productService.RemoveProductAsync(command.ProductId);
Console.WriteLine(isRemoveSuccess ? "Remote product success" : "Remote product fail");
}
}
手动注册可运用在无法或不需要单例注入的情况使用。
发布命令与查询使用相同的接口,通过IEventBus
或EventBus.Default
的Publish
和PublishAsync
方法发布命令和查询:
_messenger.Publish(this, new DeleteProductCommand { ProductId = id });
var query = new ProductQuery { ProductId = id };
await _messenger.PublishAsync(this, query);
在B/S
控制器的Action
使用发布:
[ApiController]
[Route("[controller]")]
public class EventController : ControllerBase
{
private readonly ILogger<EventController> _logger;
private readonly IEventBus _eventBus;
public EventController(ILogger<EventController> logger, IEventBus eventBus)
{
_logger = logger;
_eventBus = eventBus;
}
[HttpPost("/add")]
public Task AddAsync([FromBody] CreateProductRequest request)
{
_eventBus.Publish(this, new CreateProductCommand { Name = request.Name, Price = request.Price });
return Task.CompletedTask;
}
[HttpDelete("/delete")]
public Task DeleteAsync([FromQuery] Guid id)
{
_eventBus.Publish(this, new DeleteProductCommand { ProductId = id });
return Task.CompletedTask;
}
[HttpGet("/get")]
public async Task<ProductItemDto> GetAsync([FromQuery] Guid id)
{
var query = new ProductQuery { ProductId = id };
await _eventBus.PublishAsync(this, query);
return query.Result;
}
[HttpGet("/list")]
public async Task<List<ProductItemDto>> ListAsync([FromQuery] string? name)
{
var query = new ProductsQuery { Name = name };
await _eventBus.PublishAsync(this, query);
return query.Result;
}
}
在WPF/Avalonia UI
的ViewModel
中使用:
public class EventBusTestViewModel : ViewModelBase
{
private readonly IEventBus _eventBus;
public MessageTestViewModel(IEventBus eventBus)
{
_eventBus = eventBus;
}
public Task ExecuteEventBusAsync()
{
_eventBus.PublishAsync(this, new TestMessage(nameof(MessageTestViewModel), TestClass.CurrentTime()));
return Task.CompletedTask;
}
}
在实际应用中,你可能需要确保在适当的时机(如服务销毁时)取消订阅,以避免内存泄漏:
Messenger.Default.Unsubscribe(this, ReceiveManuCreateProductMessage)
Messenger.Default.Unsubscribe(this)
CodeWF.EventBus提供了一个小巧灵活的事件总线实现,支持CQRS模式,并适用于各种项目模板,如 Avalonia UI、WPF、WinForms、ASP.NET Core 等。通过简单的订阅和发布操作,你可以轻松实现模块间的解耦和通讯。通过有序的事件处理,确保事件得到妥善处理。
仓库地址 https://github.com/dotnet9/CodeWF.EventBus,具体使用可参考:
开发过程中参考不少开源项目:
希望本文的指南能帮助你更好地使用CodeWF.EventBus来处理你的应用程序中的事件。