网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using Dpz.Core.Infrastructure.Entity;

namespace Dpz.Core.Observer;

public class Subject<T>:ISubject<T> where T : IBaseEntity, new()
{
    //private static List<IEvent> _observers = new List<IEvent>();


    private static Dictionary<Type, List<IEvent<T>>> _observers = new Dictionary<Type, List<IEvent<T>>>();

    public void Attach(IEvent<T> observer)
    {
        if (_observers.ContainsKey(typeof(T)))
        {
            _observers.Add(typeof(T),new List<IEvent<T>>{observer});
        }
        else
        {
            _observers[typeof(T)].Add(observer);
        }
    }

    public void Detach(IEvent<T> observer)
    {
        if (_observers.ContainsKey(typeof(T)))
        {
            _observers[typeof(T)].Remove(observer);
        }
    }

    public async Task NotifyAsync()
    {
        foreach (var observer in _observers)
        {
            foreach (var item in observer.Value)
            {
                await item.HandleAsync(this);
            }
        }
    }
}
loading