/// <summary> /// 移除事件处理器。 /// </summary> /// <param name="route">事件路由。</param> /// <param name="handler">事件处理器。</param> public virtual void RemoveAsyncEventHandler <T>(DomainServiceEventRoute route, DomainServiceAsyncEventHandler <T> handler) where T : EventArgs { if (route == null) { throw new ArgumentNullException(nameof(route)); } if (handler == null) { throw new ArgumentNullException(nameof(handler)); } if (route.HandlerType != typeof(DomainServiceAsyncEventHandler <T>)) { throw new InvalidCastException("事件路由处理器类型与传入的类型不符。"); } lock (_Events) { Delegate d = GetEventRouteDelegate(route); if (d == null) { SetEventRouteDelegate(route, handler); } else { SetEventRouteDelegate(route, Delegate.Remove(d, handler)); } } }
/// <summary> /// 添加事件处理器。 /// </summary> /// <param name="route">事件路由。</param> /// <param name="handler">事件处理器。</param> public virtual void AddEventHandler(DomainServiceEventRoute route, DomainServiceEventHandler handler) { if (route == null) { throw new ArgumentNullException(nameof(route)); } if (handler == null) { throw new ArgumentNullException(nameof(handler)); } if (route.HandlerType != typeof(DomainServiceEventHandler)) { throw new InvalidCastException("事件路由处理器类型与传入的类型不符。"); } lock (_Events) { Delegate d = GetEventRouteDelegate(route); if (d == null) { SetEventRouteDelegate(route, handler); } else { SetEventRouteDelegate(route, Delegate.Combine(d, handler)); } } }
/// <summary> /// 引发事件。 /// </summary> /// <param name="route">事件路由。</param> /// <param name="context">领域执行上下文。</param> /// <param name="eventArgs">事件参数。</param> public virtual void RaiseEvent <T>(DomainServiceEventRoute route, IDomainExecutionContext context, T eventArgs) where T : EventArgs { if (route == null) { throw new ArgumentNullException(nameof(route)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } if (route.HandlerType != typeof(DomainServiceEventHandler <T>)) { throw new InvalidCastException("事件路由处理器类型不符。"); } Delegate d = GetEventRouteDelegate(route); if (d == null) { return; } foreach (var item in d.GetInvocationList().Cast <DomainServiceEventHandler <T> >()) { item(context, eventArgs); } if (route.ParentRoute != null) { RaiseEvent(route.ParentRoute, context, eventArgs); } }
/// <summary> /// 注册异步事件。 /// </summary> /// <typeparam name="T">事件参数类型。</typeparam> /// <param name="name">事件名称。</param> /// <param name="ownerType">所有者类型。</param> /// <returns></returns> public static DomainServiceEventRoute RegisterAsyncEvent <T>(string name, Type ownerType) where T : EventArgs { DomainServiceEventRoute route = new DomainServiceEventRoute(name, ownerType, typeof(DomainServiceAsyncEventHandler <T>)); return(route); }
public override void RaiseEvent <T>(DomainServiceEventRoute route, IDomainExecutionContext context, T eventArgs) { base.RaiseEvent <T>(route, context, eventArgs); if (_Parent != null) { _Parent.RaiseEvent(route, context, eventArgs); } }
public override async Task RaiseAsyncEvent <T>(DomainServiceEventRoute route, IDomainExecutionContext context, T eventArgs) { await base.RaiseAsyncEvent <T>(route, context, eventArgs); if (_Parent != null) { await _Parent.RaiseAsyncEvent(route, context, eventArgs); } }
protected override Delegate GetEventRouteDelegate(DomainServiceEventRoute route) { Delegate d; if (!_Events.TryGetValue(route, out d)) { return(null); } return(d); }
/// <summary> /// 注册事件路由。 /// </summary> /// <param name="route">事件路由。</param> public virtual void RegisterEventRoute(DomainServiceEventRoute route) { lock (_Events) { if (_Events.ContainsKey(route)) { throw new InvalidOperationException("已添加的事件路由。"); } _Events.Add(route, null); } }
protected override void SetEventRouteDelegate(DomainServiceEventRoute route, Delegate value) { if (_Events.ContainsKey(route)) { _Events[route] = value; } else { _Events.Add(route, value); } }
/// <summary> /// 重载事件。 /// </summary> /// <param name="route">事件路由。</param> /// <param name="ownerType">所有者类型。</param> /// <returns></returns> public static DomainServiceEventRoute OverrideEvent(DomainServiceEventRoute route, Type ownerType) { if (route == null) { throw new ArgumentNullException(nameof(route)); } if (ownerType == null) { throw new ArgumentNullException(nameof(ownerType)); } DomainServiceEventRoute newRoute = new DomainServiceEventRoute(route, ownerType); return(newRoute); }
/// <summary> /// 引发异步事件。 /// </summary> /// <param name="route">事件路由。</param> /// <param name="context">领域执行上下文。</param> public virtual async Task RaiseAsyncEvent(DomainServiceEventRoute route, IDomainExecutionContext context) { if (route == null) { throw new ArgumentNullException(nameof(route)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } if (route.HandlerType != typeof(DomainServiceAsyncEventHandler)) { throw new InvalidCastException("事件路由处理器类型不符。"); } Delegate d = GetEventRouteDelegate(route); if (d == null) { return; } foreach (var item in d.GetInvocationList().Cast <DomainServiceAsyncEventHandler>()) { if (route.AsyncMode == DomainServiceAsyncEventMode.Await) { await item(context); } else { Task itemTask = item(context); } } if (route.ParentRoute != null) { await RaiseAsyncEvent(route.ParentRoute, context); } }
public override void RaiseEvent <T>(DomainServiceEventRoute route, IDomainExecutionContext context, T eventArgs) { base.RaiseEvent <T>(route, context, eventArgs); DomainContextEventManager.GlobalEventManager.RaiseEvent(route, context, eventArgs); }
/// <summary> /// 移除事件处理器。 /// </summary> /// <param name="route">事件路由。</param> /// <param name="handler">事件处理器。</param> protected virtual void RemoveAsyncEventHandler(DomainServiceEventRoute route, DomainServiceAsyncEventHandler handler) { DomainServiceEventManager.GlobalEventManager.RemoveAsyncEventHandler(route, handler); }
/// <summary> /// 添加事件处理器。 /// </summary> /// <param name="route">事件路由。</param> /// <param name="handler">事件处理器。</param> protected virtual void AddEventHandler(DomainServiceEventRoute route, DomainServiceEventHandler handler) { DomainServiceEventManager.GlobalEventManager.AddEventHandler(route, handler); }
public override void RegisterEventRoute(DomainServiceEventRoute route) { throw new NotSupportedException("领域上下文当中的事件管理器不支持该方法。"); }
/// <summary> /// 移除事件处理器。 /// </summary> /// <param name="route">事件路由。</param> /// <param name="handler">事件处理器。</param> protected virtual void RemoveAsyncEventHandler <T>(DomainServiceEventRoute route, DomainServiceAsyncEventHandler <T> handler) where T : EventArgs { DomainServiceEventManager.GlobalEventManager.RemoveAsyncEventHandler(route, handler); }
protected virtual void RaiseEvent <TArgs>(DomainServiceEventRoute eventRoute, TArgs e) where TArgs : EventArgs { Context.DomainContext.EventManager.RaiseEvent(eventRoute, Context, e); }
protected virtual void RaiseEvent(DomainServiceEventRoute eventRoute) { Context.DomainContext.EventManager.RaiseEvent(eventRoute, Context); }
private DomainServiceEventRoute(DomainServiceEventRoute parentRoute, Type ownerType) : this(parentRoute.Name, ownerType, parentRoute.HandlerType, parentRoute.AsyncMode) { ParentRoute = parentRoute; }
/// <summary> /// 注册异步事件。 /// </summary> /// <param name="name">事件名称。</param> /// <param name="ownerType">所有者类型。</param> /// <returns></returns> public static DomainServiceEventRoute RegisterAsyncEvent(string name, Type ownerType) { DomainServiceEventRoute route = new DomainServiceEventRoute(name, ownerType, typeof(DomainServiceAsyncEventHandler)); return(route); }
protected virtual Task RaiseAsyncEvent(DomainServiceEventRoute eventRoute) { return(Context.DomainContext.EventManager.RaiseAsyncEvent(eventRoute, Context)); }
protected virtual Task RaiseAsyncEvent <TArgs>(DomainServiceEventRoute eventRoute, TArgs e) where TArgs : EventArgs { return(Context.DomainContext.EventManager.RaiseAsyncEvent(eventRoute, Context, e)); }
/// <summary> /// 设置事件路由委托。 /// </summary> /// <param name="route">事件路由。</param> /// <param name="value">事件委托。</param> protected virtual void SetEventRouteDelegate(DomainServiceEventRoute route, Delegate value) { _Events[route] = value; }
public override async Task RaiseAsyncEvent <T>(DomainServiceEventRoute route, IDomainExecutionContext context, T eventArgs) { await base.RaiseAsyncEvent <T>(route, context, eventArgs); await DomainContextEventManager.GlobalEventManager.RaiseAsyncEvent(route, context, eventArgs); }
/// <summary> /// 获取事件路由委托。 /// </summary> /// <param name="route">事件路由。</param> /// <returns>返回事件委托。</returns> protected virtual Delegate GetEventRouteDelegate(DomainServiceEventRoute route) { return(_Events[route]); }