忽略
This commit is contained in:
@@ -1,24 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Events
|
||||
{
|
||||
internal class ActionEventHandler<TEventData> : IEventHandler<TEventData> where TEventData : IEventData
|
||||
{
|
||||
public Action<TEventData> Action { get; private set; }
|
||||
|
||||
public virtual bool Ansyc { get; set; }
|
||||
|
||||
public ActionEventHandler(Action<TEventData> handler)
|
||||
{
|
||||
Action = handler;
|
||||
}
|
||||
public void HandleEvent(TEventData eventData)
|
||||
{
|
||||
Action(eventData);
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Events
|
||||
{
|
||||
internal class ActionEventHandler<TEventData> : IEventHandler<TEventData> where TEventData : IEventData
|
||||
{
|
||||
public Action<TEventData> Action { get; private set; }
|
||||
|
||||
public virtual bool Ansyc { get; set; }
|
||||
|
||||
public ActionEventHandler(Action<TEventData> handler)
|
||||
{
|
||||
Action = handler;
|
||||
}
|
||||
public void HandleEvent(TEventData eventData)
|
||||
{
|
||||
Action(eventData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,150 +1,150 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件总线
|
||||
/// </summary>
|
||||
public class EventBus
|
||||
{
|
||||
private static EventBus _eventBus = null;
|
||||
|
||||
public static EventBus Default
|
||||
{
|
||||
get { return _eventBus ?? (_eventBus = new EventBus()); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 定义线程安全集合
|
||||
/// </summary>
|
||||
private readonly ConcurrentDictionary<Type, List<IEventHandler>> _eventAndHandlerMapping;
|
||||
|
||||
public EventBus()
|
||||
{
|
||||
_eventAndHandlerMapping = new ConcurrentDictionary<Type, List<IEventHandler>>();
|
||||
MapEventToHandler();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///通过反射,将事件源与事件处理绑定
|
||||
/// </summary>
|
||||
private void MapEventToHandler()
|
||||
{
|
||||
// Assembly assembly = Assembly.GetEntryAssembly();
|
||||
|
||||
var allAssembly = AppDomain.CurrentDomain.GetAssemblies().Where(item => item.FullName.Contains("Microkj."));
|
||||
if (!allAssembly.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var assembly in allAssembly)
|
||||
{
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
if (!type.IsGenericType && typeof(IEventHandler).IsAssignableFrom(type)) //判断当前类型是否实现了IEventHandler接口
|
||||
{
|
||||
Type handlerInterface = type.GetInterface("IEventHandler`1"); //获取该类实现的泛型接口
|
||||
if (handlerInterface != null)
|
||||
{
|
||||
Type eventDataType = handlerInterface.GetGenericArguments()[0]; // 获取泛型接口指定的参数类型
|
||||
|
||||
if (_eventAndHandlerMapping.ContainsKey(eventDataType))
|
||||
{
|
||||
List<IEventHandler> handlerTypes = _eventAndHandlerMapping[eventDataType];
|
||||
handlerTypes.Add(Activator.CreateInstance(type) as IEventHandler);
|
||||
_eventAndHandlerMapping[eventDataType] = handlerTypes;
|
||||
}
|
||||
else
|
||||
{
|
||||
var handlerTypes = new List<IEventHandler>
|
||||
{
|
||||
Activator.CreateInstance(type) as IEventHandler
|
||||
};
|
||||
_eventAndHandlerMapping[eventDataType] = handlerTypes;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 手动绑定事件源与事件处理
|
||||
/// </summary>
|
||||
/// <typeparam name="TEventData"></typeparam>
|
||||
/// <param name="eventHandler"></param>
|
||||
public void Register<TEventData>(IEventHandler eventHandler)
|
||||
{
|
||||
if (_eventAndHandlerMapping.Keys.Contains(typeof (TEventData)))
|
||||
{
|
||||
List<IEventHandler> handlerTypes = _eventAndHandlerMapping[typeof (TEventData)];
|
||||
if (!handlerTypes.Contains(eventHandler))
|
||||
{
|
||||
handlerTypes.Add(eventHandler);
|
||||
_eventAndHandlerMapping[typeof (TEventData)] = handlerTypes;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_eventAndHandlerMapping.GetOrAdd(typeof (TEventData), (type) => new List<IEventHandler>())
|
||||
.Add(eventHandler);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Register<TEventData>(Action<TEventData> action) where TEventData : IEventData
|
||||
{
|
||||
var actionHandler = new ActionEventHandler<TEventData>(action);
|
||||
Register<TEventData>(actionHandler);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 手动解除事件源与事件处理的绑定
|
||||
/// </summary>
|
||||
/// <typeparam name="TEventData"></typeparam>
|
||||
/// <param name="eventHandler"></param>
|
||||
public void UnRegister<TEventData>(Type eventHandler)
|
||||
{
|
||||
List<IEventHandler> handlerTypes = _eventAndHandlerMapping[typeof (TEventData)];
|
||||
|
||||
_eventAndHandlerMapping.GetOrAdd(typeof (TEventData), (type) => new List<IEventHandler>())
|
||||
.RemoveAll(t => t.GetType() == eventHandler);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据事件源触发绑定的事件处理
|
||||
/// </summary>
|
||||
/// <typeparam name="TEventData"></typeparam>
|
||||
/// <param name="eventData"></param>
|
||||
public static void Publish<TEventData>(TEventData eventData) where TEventData : IEventData
|
||||
{
|
||||
List<IEventHandler> handlers = Default._eventAndHandlerMapping[typeof (TEventData)];
|
||||
|
||||
if (handlers != null && handlers.Count > 0)
|
||||
{
|
||||
foreach (var handler in handlers)
|
||||
{
|
||||
var eventHandler = handler as IEventHandler<TEventData>;
|
||||
if (eventHandler.Ansyc)
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
eventHandler.HandleEvent(eventData);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
eventHandler.HandleEvent(eventData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件总线
|
||||
/// </summary>
|
||||
public class EventBus
|
||||
{
|
||||
private static EventBus _eventBus = null;
|
||||
|
||||
public static EventBus Default
|
||||
{
|
||||
get { return _eventBus ?? (_eventBus = new EventBus()); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 定义线程安全集合
|
||||
/// </summary>
|
||||
private readonly ConcurrentDictionary<Type, List<IEventHandler>> _eventAndHandlerMapping;
|
||||
|
||||
public EventBus()
|
||||
{
|
||||
_eventAndHandlerMapping = new ConcurrentDictionary<Type, List<IEventHandler>>();
|
||||
MapEventToHandler();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///通过反射,将事件源与事件处理绑定
|
||||
/// </summary>
|
||||
private void MapEventToHandler()
|
||||
{
|
||||
// Assembly assembly = Assembly.GetEntryAssembly();
|
||||
|
||||
var allAssembly = AppDomain.CurrentDomain.GetAssemblies().Where(item => item.FullName.Contains("Microkj."));
|
||||
if (!allAssembly.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var assembly in allAssembly)
|
||||
{
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
if (!type.IsGenericType && typeof(IEventHandler).IsAssignableFrom(type)) //判断当前类型是否实现了IEventHandler接口
|
||||
{
|
||||
Type handlerInterface = type.GetInterface("IEventHandler`1"); //获取该类实现的泛型接口
|
||||
if (handlerInterface != null)
|
||||
{
|
||||
Type eventDataType = handlerInterface.GetGenericArguments()[0]; // 获取泛型接口指定的参数类型
|
||||
|
||||
if (_eventAndHandlerMapping.ContainsKey(eventDataType))
|
||||
{
|
||||
List<IEventHandler> handlerTypes = _eventAndHandlerMapping[eventDataType];
|
||||
handlerTypes.Add(Activator.CreateInstance(type) as IEventHandler);
|
||||
_eventAndHandlerMapping[eventDataType] = handlerTypes;
|
||||
}
|
||||
else
|
||||
{
|
||||
var handlerTypes = new List<IEventHandler>
|
||||
{
|
||||
Activator.CreateInstance(type) as IEventHandler
|
||||
};
|
||||
_eventAndHandlerMapping[eventDataType] = handlerTypes;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 手动绑定事件源与事件处理
|
||||
/// </summary>
|
||||
/// <typeparam name="TEventData"></typeparam>
|
||||
/// <param name="eventHandler"></param>
|
||||
public void Register<TEventData>(IEventHandler eventHandler)
|
||||
{
|
||||
if (_eventAndHandlerMapping.Keys.Contains(typeof (TEventData)))
|
||||
{
|
||||
List<IEventHandler> handlerTypes = _eventAndHandlerMapping[typeof (TEventData)];
|
||||
if (!handlerTypes.Contains(eventHandler))
|
||||
{
|
||||
handlerTypes.Add(eventHandler);
|
||||
_eventAndHandlerMapping[typeof (TEventData)] = handlerTypes;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_eventAndHandlerMapping.GetOrAdd(typeof (TEventData), (type) => new List<IEventHandler>())
|
||||
.Add(eventHandler);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Register<TEventData>(Action<TEventData> action) where TEventData : IEventData
|
||||
{
|
||||
var actionHandler = new ActionEventHandler<TEventData>(action);
|
||||
Register<TEventData>(actionHandler);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 手动解除事件源与事件处理的绑定
|
||||
/// </summary>
|
||||
/// <typeparam name="TEventData"></typeparam>
|
||||
/// <param name="eventHandler"></param>
|
||||
public void UnRegister<TEventData>(Type eventHandler)
|
||||
{
|
||||
List<IEventHandler> handlerTypes = _eventAndHandlerMapping[typeof (TEventData)];
|
||||
|
||||
_eventAndHandlerMapping.GetOrAdd(typeof (TEventData), (type) => new List<IEventHandler>())
|
||||
.RemoveAll(t => t.GetType() == eventHandler);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据事件源触发绑定的事件处理
|
||||
/// </summary>
|
||||
/// <typeparam name="TEventData"></typeparam>
|
||||
/// <param name="eventData"></param>
|
||||
public static void Publish<TEventData>(TEventData eventData) where TEventData : IEventData
|
||||
{
|
||||
List<IEventHandler> handlers = Default._eventAndHandlerMapping[typeof (TEventData)];
|
||||
|
||||
if (handlers != null && handlers.Count > 0)
|
||||
{
|
||||
foreach (var handler in handlers)
|
||||
{
|
||||
var eventHandler = handler as IEventHandler<TEventData>;
|
||||
if (eventHandler.Ansyc)
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
eventHandler.HandleEvent(eventData);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
eventHandler.HandleEvent(eventData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,39 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件源:描述事件信息,用于参数传递
|
||||
/// </summary>
|
||||
public class EventData<TData> : IEventData where TData:class
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件发生的时间
|
||||
/// </summary>
|
||||
public DateTime EventTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 触发事件的对象
|
||||
/// </summary>
|
||||
public TData EventSource { get; set; }
|
||||
|
||||
object IEventData.EventSource
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.EventSource as TData;
|
||||
}
|
||||
|
||||
set { this.EventSource =(TData) value; }
|
||||
}
|
||||
|
||||
public EventData()
|
||||
{
|
||||
EventTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件源:描述事件信息,用于参数传递
|
||||
/// </summary>
|
||||
public class EventData<TData> : IEventData where TData:class
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件发生的时间
|
||||
/// </summary>
|
||||
public DateTime EventTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 触发事件的对象
|
||||
/// </summary>
|
||||
public TData EventSource { get; set; }
|
||||
|
||||
object IEventData.EventSource
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.EventSource as TData;
|
||||
}
|
||||
|
||||
set { this.EventSource =(TData) value; }
|
||||
}
|
||||
|
||||
public EventData()
|
||||
{
|
||||
EventTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Events
|
||||
{
|
||||
public interface IEventBus
|
||||
{
|
||||
void Register<TEventData>(IEventHandler eventHandler);
|
||||
|
||||
void Register<TEventData>(Action<TEventData> action) where TEventData : IEventData;
|
||||
|
||||
void UnRegister<TEventData>(Type eventHandler);
|
||||
|
||||
// void Trigger<TEventData>(TEventData eventData) where TEventData : IEventData;
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Events
|
||||
{
|
||||
public interface IEventBus
|
||||
{
|
||||
void Register<TEventData>(IEventHandler eventHandler);
|
||||
|
||||
void Register<TEventData>(Action<TEventData> action) where TEventData : IEventData;
|
||||
|
||||
void UnRegister<TEventData>(Type eventHandler);
|
||||
|
||||
// void Trigger<TEventData>(TEventData eventData) where TEventData : IEventData;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// 定义事件源接口,所有的事件源都要实现该接口
|
||||
/// </summary>
|
||||
public interface IEventData
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件发生的时间
|
||||
/// </summary>
|
||||
DateTime EventTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 触发事件的对象
|
||||
/// </summary>
|
||||
Object EventSource { get; set; }
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// 定义事件源接口,所有的事件源都要实现该接口
|
||||
/// </summary>
|
||||
public interface IEventData
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件发生的时间
|
||||
/// </summary>
|
||||
DateTime EventTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 触发事件的对象
|
||||
/// </summary>
|
||||
Object EventSource { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// 定义事件处理器公共接口,所有的事件处理都要实现该接口
|
||||
/// </summary>
|
||||
public interface IEventHandler
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 泛型事件处理器接口
|
||||
/// </summary>
|
||||
/// <typeparam name="TEventData"></typeparam>
|
||||
public interface IEventHandler<TEventData> : IEventHandler where TEventData : IEventData
|
||||
{
|
||||
bool Ansyc { get; set; }
|
||||
/// <summary>
|
||||
/// 事件处理器实现该方法来处理事件
|
||||
/// </summary>
|
||||
/// <param name="eventData"></param>
|
||||
void HandleEvent(TEventData eventData);
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// 定义事件处理器公共接口,所有的事件处理都要实现该接口
|
||||
/// </summary>
|
||||
public interface IEventHandler
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 泛型事件处理器接口
|
||||
/// </summary>
|
||||
/// <typeparam name="TEventData"></typeparam>
|
||||
public interface IEventHandler<TEventData> : IEventHandler where TEventData : IEventData
|
||||
{
|
||||
bool Ansyc { get; set; }
|
||||
/// <summary>
|
||||
/// 事件处理器实现该方法来处理事件
|
||||
/// </summary>
|
||||
/// <param name="eventData"></param>
|
||||
void HandleEvent(TEventData eventData);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user