1. 程式人生 > >C#事件の事件聚合器

C#事件の事件聚合器

typeof test ring 發布 col add pro ati nbsp

事件聚合器用於集中管理事件的訂閱(Subscribe)和處理(Handle),要使用事件聚合器,首先要理解:事件(event)本質上是一個類。

傳統的+=和-=不足:

1、管理很麻煩;2、不方便擴展。

所以嘗試使用事件聚合器來解決這個問題。

首先,使用一個統一的接口來實現事件的統一標記:

public interface IEvent{  }

事件,需要有對應的事件處理器(EventHandler),這裏使用一個統一的接口 IEventHandler 來進行統一,IEventHandler 只包含一個處理事件的方法 Handle

public interface IEventHandler<in TEvent> where TEvent: class, IEvent
{
    void Handler(TEvent @event);
}

事件聚合器接口 IEventAggregator 需要包含至少兩個方法:訂閱(Subscribe)和發布(Publish)

public interface IEventAggregator
{
    void Subscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class, IEvent;
    void Publish<TEvent>(TEvent @event) where TEvent : class, IEvent;
}

接下來是實現這個接口:EventAggregator

舉例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EventAggregatorTest
{
    public interface IEvent { }

    public interface IEventHandler<in TEvent> where TEvent : class, IEvent
    {
        void Handler(TEvent @event);
    }

    public interface
IEventAggregator { void Subscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class, IEvent; void Publish<TEvent>(TEvent @event) where TEvent : class, IEvent; } public class EventAggregator : IEventAggregator { // 所有的 event 和 對應的 handler 都註冊在這個字典裏 private readonly Dictionary<Type, List<object>> _eventHandlers = new Dictionary<Type, List<object>>(); public void Subscribe<TEvent>(IEventHandler<TEvent> handler) where TEvent : class, IEvent { var eventType = typeof(TEvent); if (_eventHandlers.ContainsKey(eventType)) { var handlers = _eventHandlers[eventType]; if (handlers != null) { handlers.Add(handler); } else { handlers = new List<object> { handler }; } } else { _eventHandlers.Add(eventType, new List<object> { handler }); } } public void Publish<TEvent>(TEvent @event) where TEvent : class, IEvent { var eventType = @event.GetType(); if (_eventHandlers.ContainsKey(eventType) && _eventHandlers[eventType] != null && _eventHandlers[eventType].Count > 0) { var handlers = _eventHandlers[eventType]; foreach (var handler in handlers) { var eventHandler = handler as IEventHandler<TEvent>; eventHandler?.Handler(@event); } } } } public class MealDoneEvent : IEvent { public string DishName { get; private set; } public MealDoneEvent(string dishName) { DishName = dishName; } } public class MealDoneEventHandler : IEventHandler<MealDoneEvent> { private readonly string _name; public MealDoneEventHandler(string name) { _name = name; } #region Implementation of IEventHandler<in MealDoneEvent> public void Handler(MealDoneEvent @event) { Console.WriteLine($"{_name} 正在 {@event.DishName}"); } #endregion } }

調用:

using EventAggregatorTest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EventAggregatorDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var eventAggregator = new EventAggregator();

            eventAggregator.Subscribe(new MealDoneEventHandler("張三"));
            eventAggregator.Subscribe(new MealDoneEventHandler("李四"));
            eventAggregator.Subscribe(new MealDoneEventHandler("王五"));

            eventAggregator.Publish(new MealDoneEvent("吃大餐"));

            Console.ReadKey();
        }
    }
}

C#事件の事件聚合器