/// <summary> /// Trigger domain event /// </summary> /// <param name="triggerTime">Trigger time</param> /// <param name="domainEvents">Domain events</param> internal void TriggerDomainEvent(EventTriggerTime triggerTime, IEnumerable <IDomainEvent> domainEvents) { if (domainEvents.IsNullOrEmpty()) { return; } foreach (var domainEvent in domainEvents) { if (domainEvent == null) { continue; } //overall event handler overallEventHandlerCollection.TryGetValue(triggerTime, out var allHandlers); //spectial event handler var eventType = domainEvent.GetType(); if (eventHandlerCollection.TryGetValue(eventType.GUID, out var eventTimeHandlers) && !eventTimeHandlers.IsNullOrEmpty() && eventTimeHandlers.TryGetValue(triggerTime, out var handlers) && !handlers.IsNullOrEmpty()) { if (allHandlers == null) { allHandlers = handlers; } else { allHandlers.AddRange(handlers); } } if (allHandlers.IsNullOrEmpty()) { continue; } if (triggerTime == EventTriggerTime.Immediately) { foreach (var handler in allHandlers) { handler.Execute(domainEvent); } } else { foreach (var handler in allHandlers) { var domainEventHandler = handler; ThreadPool.QueueUserWorkItem(s => { domainEventHandler.Execute(domainEvent); }); } } } }
/// <summary> /// executed domain event /// </summary> /// <param name="eventExecuteTime">event time</param> /// <param name="domainEvents">domain events</param> internal async Task ExecutedTimeDomainEventAsync(EventTriggerTime eventExecuteTime, params IDomainEvent[] domainEvents) { if (domainEvents.IsNullOrEmpty()) { return; } foreach (var domainEvent in domainEvents) { if (domainEvent == null) { continue; } //overall event handler if (overallEventHandlerCollection.TryGetValue(eventExecuteTime, out var overallHandlers) && !overallHandlers.IsNullOrEmpty()) { foreach (var overallHandler in overallHandlers) { await overallHandler.ExecuteAsync(domainEvent).ConfigureAwait(false); } } //spectial event handler var eventType = domainEvent.GetType(); if (!eventHandlerCollection.TryGetValue(eventType.GUID, out var eventTimeHandlers) || eventTimeHandlers.IsNullOrEmpty()) { continue; } if (!eventTimeHandlers.TryGetValue(eventExecuteTime, out var handlers) || handlers.IsNullOrEmpty()) { continue; } foreach (var handler in handlers) { await handler.ExecuteAsync(domainEvent).ConfigureAwait(false); } } }
/// <summary> /// executed domain event /// </summary> /// <param name="eventExecuteTime">event time</param> /// <param name="domainEvents">domain events</param> internal void ExecutedTimeDomainEvent(EventTriggerTime eventExecuteTime, params IDomainEvent[] domainEvents) { ExecutedTimeDomainEventAsync(eventExecuteTime, domainEvents).Wait(); }
/// <summary> /// subscribe domain event in global area /// </summary> /// <typeparam name="Event"></typeparam> /// <param name="eventHandleOperationAsync">event handle operation</param> /// <param name="executeTime">execute time</param> public void SubscribeAll(Func <IDomainEvent, Task <DomainEventExecuteResult> > eventHandleOperationAsync, EventTriggerTime executeTime = EventTriggerTime.Immediately) { IDomainEventHandler domainEventHandler = null; switch (executeTime) { case EventTriggerTime.Immediately: domainEventHandler = new DefaultImmediatelyDomainEventHandler <IDomainEvent>() { ExecuteEventOperation = eventHandleOperationAsync }; break; case EventTriggerTime.WorkCompleted: domainEventHandler = new DefaultWorkCompletedDomainEventHandler <IDomainEvent>() { ExecuteEventOperation = eventHandleOperationAsync }; break; } SubscribeAll(domainEventHandler); }
/// <summary> /// subscribe domain event in global area /// </summary> /// <typeparam name="Event">event</typeparam> /// <param name="eventHandleOperation">event handle operation</param> /// <param name="executeTime">execute time</param> public void SubscribeAll(Func <IDomainEvent, DomainEventExecuteResult> eventHandleOperation, EventTriggerTime executeTime = EventTriggerTime.Immediately) { if (eventHandleOperation == null) { return; } Func <IDomainEvent, Task <DomainEventExecuteResult> > asyncOperation = x => Task.FromResult(eventHandleOperation(x)); SubscribeAll(asyncOperation, executeTime); }
/// <summary> /// subscribe domain event in global area /// </summary> /// <param name="eventHandleOperation">event handle operation</param> /// <param name="executeTime">execute time</param> public static void GlobalSubscribeAll(Func <IDomainEvent, DomainEventExecuteResult> eventHandleOperation, EventTriggerTime executeTime = EventTriggerTime.Immediately) { globalDomainEventManager?.SubscribeAll(eventHandleOperation, executeTime); }
/// <summary> /// subscribe domain event in global area /// </summary> /// <typeparam name="Event"></typeparam> /// <param name="eventHandleOperationAsync">event handle operation</param> /// <param name="executeTime">execute time</param> public static void GlobalSubscribe <Event>(Func <Event, Task <DomainEventExecuteResult> > eventHandleOperationAsync, EventTriggerTime executeTime = EventTriggerTime.Immediately) where Event : class, IDomainEvent { globalDomainEventManager?.Subscribe(eventHandleOperationAsync, executeTime); }
/// <summary> /// subscribe domain event in global area /// </summary> /// <param name="eventHandleOperationAsync">event handle operation</param> /// <param name="executeTime">execute time</param> public static void WorkSubscribeAll(Func <IDomainEvent, Task <DomainEventExecuteResult> > eventHandleOperationAsync, EventTriggerTime executeTime = EventTriggerTime.Immediately) { WorkFactory.Current?.DomainEventManager?.SubscribeAll(eventHandleOperationAsync, executeTime); }
/// <summary> /// subscribe domain event in global area /// </summary> /// <typeparam name="Event">event</typeparam> /// <param name="eventHandleOperation">event handle operation</param> /// <param name="executeTime">execute time</param> public static void WorkSubscribe <Event>(Func <Event, DomainEventExecuteResult> eventHandleOperation, EventTriggerTime executeTime = EventTriggerTime.Immediately) where Event : class, IDomainEvent { WorkFactory.Current?.DomainEventManager?.Subscribe(eventHandleOperation, executeTime); }
/// <summary> /// Subscribe domain event /// </summary> /// <typeparam name="TEvent">Domain event</typeparam> /// <param name="eventHandleOperation">event handle operation</param> /// <param name="executeTime">Execute time</param> public void Subscribe <TEvent>(Func <TEvent, DomainEventExecuteResult> eventHandleOperation, EventTriggerTime executeTime = EventTriggerTime.Immediately) where TEvent : class, IDomainEvent { IDomainEventHandler domainEventHandler = null; switch (executeTime) { case EventTriggerTime.Immediately: domainEventHandler = new DefaultImmediatelyDomainEventHandler <TEvent>() { ExecuteEventOperation = eventHandleOperation }; break; case EventTriggerTime.WorkCompleted: domainEventHandler = new DefaultWorkCompletedDomainEventHandler <TEvent>() { ExecuteEventOperation = eventHandleOperation }; break; } Subscribe <TEvent>(domainEventHandler); }
/// <summary> /// Trigger domain event /// </summary> /// <param name="triggerTime">Trigger time</param> /// <param name="domainEvents">Domain events</param> internal void TriggerDomainEvent(EventTriggerTime triggerTime, params IDomainEvent[] domainEvents) { IEnumerable <IDomainEvent> eventCollection = domainEvents; TriggerDomainEvent(triggerTime, eventCollection); }
/// <summary> /// Subscribe domain event in global area /// </summary> /// <typeparam name="TEvent">Domain event</typeparam> /// <param name="eventHandleOperation">Event handle operation</param> /// <param name="executeTime">Execute time</param> public static void GlobalSubscribe <TEvent>(Func <TEvent, DomainEventExecuteResult> eventHandleOperation, EventTriggerTime executeTime = EventTriggerTime.Immediately) where TEvent : class, IDomainEvent { GlobalDomainEventManager?.Subscribe(eventHandleOperation, executeTime); }
/// <summary> /// Subscribe domain event in global area /// </summary> /// <param name="eventHandleOperation">Event handle operation</param> /// <param name="executeTime">Execute time</param> public static void WorkSubscribeAll(Func <IDomainEvent, DomainEventExecuteResult> eventHandleOperation, EventTriggerTime executeTime = EventTriggerTime.Immediately) { WorkManager.Current?.DomainEventManager?.SubscribeAll(eventHandleOperation, executeTime); }