菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
206
0

Abp框架 事件总线_05

原创
05/13 14:22
阅读数 7181

参考  https://aspnetboilerplate.com/Pages/Documents/EventBus-Domain-Events

EventBus

The EventBus is a singleton object that is shared by other classes to trigger and handle events. To use the event bus, you need to get a reference to it. You can do that in two ways.

1. 事件总线是单例对象

2. 如何使用

  a).Injecting IEventBus 使用属性注入模式:

    参考代码: 

public class TaskAppService : ApplicationService
{
    public IEventBus EventBus { get; set; }

    public TaskAppService()
    {
        EventBus = NullEventBus.Instance;
    }
}

  Property injection is more proper than a constructor injection when injecting the event bus. This way, your class can work without the event bus. NullEventBus implements the null object pattern. When you call its methods, it does nothing at all.

  b).Getting The Default Instance 使用默认实例

    这种是使用全局的 “event bus” 。

EventBus.Default.Trigger(...); //trigger an event

    We do not recommend that you directly use EventBus.Default, since it makes unit testing harder.

    官方说明是: 不推荐使用这种方式    “since it makes unit testing harder”  这会使得项目进行单元测试变得跟困难。。。  (我没去研究过为啥困难。。。暂且相信别人的经验吧。)

 

3. 定义事件

  在触发事件之前,您需要先对其进行定义。事件由派生自EventData的类表示

  参考代码:

public class TaskCompletedEventData : EventData
{
    public int TaskId { get; set; }
}

      此类包含处理事件的类所需的属性。 EventData类定义EventSource(触发事件的对象)和EventTime(触发时)属性。

 4.Predefined Events 预定义事件
  官方举例:
  a).Handled Exceptions 异常处理事件
    ABP框架定义的叫 “AbpHandledExceptionData” 当异常触发时,事件会自动执行。
    This is especially useful if you want to get more information about exceptions (ASP.NET Boilerplate automatically logs all exceptions). You can register to this event to be informed when an exception occurs.
  
  b).实体变更事件
    简单说,就是ef保存数据之前,之后。。。。 实体变更事件触发。
 
5.触发事件
  这个简单
 
6.处理事件官方举例3种
  通常使用的方式
  继承并实现 IEventHandler<T> 
public class ActivityWriter : IEventHandler<TaskCompletedEventData>, ITransientDependency
{
    public void HandleEvent(TaskCompletedEventData eventData)
    {
        WriteActivity("A task is completed by id = " + eventData.TaskId);
    }
}

  使用这种方式好处:abp框会将您实现的事件注册进依赖注入系统( ITransientDependency  “Transient” 指的是生命周期),abp框依赖注入系统帮你管理事件生命周期。

 

  a).处理基本事件

    意思就是 A实现 IEventHandler<T>  后, B、C继承A ,那么B、C 两个事件会被注册。

  b).异常处理程序
    当执行的处理程序只有一个异常时,就直接由被触发的事件绑定的方法抛出异常。
    当多个处理程序抛出异常时,EventBus 为所有异常抛出一个“AggregateException ”
    我应该没理解错。。。
  c).处理多个事件
  在一个处理器程序中处理多个事件,直接看例子:
public class ActivityWriter :
    IEventHandler<TaskCompletedEventData>,
    IEventHandler<TaskCreatedEventData>,
    ITransientDependency
{
    public void HandleEvent(TaskCompletedEventData eventData)
    {
        //TODO: handle the event...
    }

    public void HandleEvent(TaskCreatedEventData eventData)
    {
        //TODO: handle the event...
    }
}

  

7.注册事件处理程序

  a).自动注册

  实现了“IEventHandler” ,都会被abp框架自动注册。

  b).手动注册

    注意:为什么不推荐手动注册!

    直接看官网:https://aspnetboilerplate.com/Pages/Documents/EventBus-Domain-Events#manually

    注意:手动注册之后,如何释放被注册的事件!  

 

8.后台事件

  If you don't want to execute event handlers right away, you can queue events in a background job like below;

public class MyService
{
	private readonly IBackgroundJobManager _backgroundJobManager;

	public MyService(IBackgroundJobManager backgroundJobManager)
	{
		_backgroundJobManager = backgroundJobManager;
	}

	public void Test()
	{
		_backgroundJobManager.EnqueueEventAsync(new MySimpleEventData());
	}
}

  使用这种方法,将在执行后台作业时触发相关事件。

  
  

发表评论

0/200
206 点赞
0 评论
收藏