GitHub - ShimizuShiori/EventBus: 基于 C# 的事件总线

[](#%E4%BA%8B%E4%BB%B6%E6%80%BB%E7%BA%BF)事件总线

这是一个参照 Java Spring 中的 ApplicationContext.publish(message) 的方法实现的事件总线

[](#1-%E5%9F%BA%E6%9C%AC%E4%BF%A1%E6%81%AF)1 基本信息

[](#11-%E5%AE%89%E8%A3%85)1.1 安装

通过 Nuget 你就可以安装使用它。

[](#12-%E8%BF%90%E8%A1%8C%E7%8E%AF%E5%A2%83)1.2 运行环境

Reface.EventBus 工作在 .NetFramework 上

Reface.Core.EventBus 工作在 .NetCore 上

[](#13-%E4%BE%9D%E8%B5%96%E9%A1%B9)1.3 依赖项

Reface.EventBus 无依赖项

Reface.Core.EventBus 依赖 Microsoft.Extensions.DependencyInjection

[](#2-%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95)2 使用方法

[](#21-%E6%B6%88%E6%81%AF%E5%8F%91%E5%B8%83)2.1 消息发布

定义事件

/// <summary>
/// 控制台启动后的事件
/// </summary>
public class ConsoleStarted : Reface.EventBus.Event
{

public ConsoleStarted(object source) : base(source)
{
    Console.WriteLine("控件台启动完毕");
}

}

在控制台启动后触发事件

class Program
{

static void Main(string\[\] args)
{
    // 构造事件总线
    IEventBus eventBus = new DefaultEventBus();

    // 发布消息
    eventBus.Publish(new ConsoleStarted());
}

}

[](#22-%E6%B6%88%E6%81%AF%E7%9B%91%E5%90%AC)2.2 消息监听

[](#221-%E5%A6%82%E4%BD%95%E7%9B%91%E5%90%AC)2.2.1 如何监听

实现 IEventListener 即可成为监听者

using ConsoleApp1.Events;
using Reface.EventBus;

namespace ConsoleApp1.Listeners
{

public class OnConsoleStarted : IEventListener<ConsoleStarted>
{
    public void Handle(ConsoleStarted @event)
    {
        Console.WriteLine("Console Started");
    }
}

}

[](#222-%E5%A6%82%E4%BD%95%E6%B7%BB%E5%8A%A0%E7%9B%91%E5%90%AC%E8%80%85)2.2.2 如何添加监听者

为了松耦合,我们不会要求手动的将监听者添加到 EventBus 实例中去。 比较简单的方法是通过 config 文件注册监听者。

在配置文件中定义事件监听者

  1. 添加 section
  2. 添加监听者

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>

<section name="eventBus" type="Reface.EventBus.Configuration.EventBusSection, Reface.EventBus"/>

</configSections>
<eventBus>

<listeners>
  <add type="ConsoleApp1.Listeners.OnConsoleStarted, ConsoleApp1" />
</listeners>

</eventBus>
</configuration>

[](#223-%E5%85%B6%E5%AE%83%E6%96%B9%E6%B3%95%E6%B7%BB%E5%8A%A0%E7%9B%91%E5%90%AC%E8%80%85)2.2.3 其它方法添加监听者

除了通过 config 文件的方法,我们还提供了其它方法来注册监听者。

只要实现了 Reface.EventBus.IEventListenerFinder 并在构造 DefaultEventBus 时作为参数传入,便可以订制的方式注册监听者。 目前自带的注册方式有:

  • Reface.EventBus.EventListenerFinders.ConfigurationEventListenerFinder 通过 config 文件来注册
  • Reface.EventBus.EventListenerFinders.AssembliesEventListenerFinder 通过注册程序集,并返反射其中的类型来得到所有实现了 Reface.EventBus.IEventListenerFinder 的成员
  • Reface.EventBus.EventListenerFinders.DefaultEventListenerFinder 通过编码的方式注册监听者

[](#224-%E5%AE%9A%E4%B9%89%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F)2.2.4 定义执行顺序

IEventListener 的实现类再添加 IPrioritized 接口,并实现 Priority 属性,便可以指定执行的顺序。

  • Priority 的值越小,越先执行
  • 未实现 IPrioritized 的 IEventListener 认为 Priority = 0

[](#3-%E9%9B%86%E6%88%90)3. 集成

[](#31-netcore-%E4%B8%8E-servicecollection-%E9%9B%86%E6%88%90)3.1 .NetCore 与 ServiceCollection 集成

IOC/DI 组件的集成,可以免去对监听者一一注册的过程。 在 .NetCore 中,通过为 ServiceCollection 注册必要组件和按程序集注册监听器,可以实现这些功能:

var provider = new ServiceCollection()
.AddEventBus() // 添加 EventBus 功能
.AddEventListeners(this.GetType().Assembly)
.AddEventListeners(typeof(IService).Assembly)
.BuildServiceProvider();
IEventBus eventBus = provider.GetService<IEventBus>();
eventBus.Publish(new TestEvent());


原网址: 访问
创建于: 2023-04-17 09:35:48
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论