Example #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="type"></param>
 /// <param name="handles"></param>
 /// <returns></returns>
 public static Configure Order(this Configure configure, Type type, List <Type> handles)
 //where T : IDomainEvent
 //where THandler : IWantToHandleEvent<T>
 {
     OrderConfiger.SetOrder(type, handles);
     return(configure);
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <param name="handles"></param>
        /// <returns></returns>
        public static Configure Order(this Configure configure, Type type, List <Type> handles)
        //where T : IDomainEvent
        //where THandler : IWantToHandleEvent<T>
        {
            Logger.LogSystemLog(EventIdConstants.ChakadSetOrder,
                                $"Set orders of receiving events by subscribers for event ={type.FullName}");

            string subOrderName = "";

            handles.ForEach(h => subOrderName += $"'{h.FullName}' ");

            Logger.LogSystemLog(EventIdConstants.ChakadSetOrder,
                                $"Subscribers order for event={type.FullName} are {handles}");

            OrderConfiger.SetOrder(type, handles);
            return(configure);
        }
        private async Task PublishThisEvent <T>(T domainEvent) where T : IDomainEvent
        {
            var type = domainEvent.GetType();

            var eventHandlers = ChakadContainer.ResolveEventSubscribers(type);

            var subscribersCount = eventHandlers != null ? eventHandlers.Count : 0;

            Logger.LogInformation(EventIdConstants.StartPublishingEvent,
                                  domainEvent.CorrelationId,
                                  $"PublishEvent Correlation_Id is ={domainEvent.CorrelationId} . There is {subscribersCount } Subscriber(s) was found!");

            var orderOf = OrderConfiger.GetOrderOf(type);

            Parallel.ForEach(orderOf, async order =>
            {
                if (!eventHandlers.Contains(order))
                {
                    return;
                }

                using (var scope = ChakadContainer.Autofac.BeginLifetimeScope(ChakadContainer.AutofacScopeName))
                {
                    var handler = scope.ResolveOptional(order);

                    var concreteType = typeof(IWantToSubscribeThisEvent <>).MakeGenericType(order);

                    await(Task) concreteType.GetMethod("Handle").Invoke(handler, new object[] { domainEvent });
                }
            }
                             );

            Parallel.ForEach(eventHandlers.Except(orderOf), async newInstance =>
            {
                using (var scope = ChakadContainer.Autofac.BeginLifetimeScope(ChakadContainer.AutofacScopeName))
                {
                    var handler = scope.ResolveOptional(newInstance);

                    var concreteType = typeof(IWantToSubscribeThisEvent <>).MakeGenericType(newInstance);

                    await(Task) concreteType.GetMethod("Handle").Invoke(handler, new object[] { domainEvent });
                }
            });
        }
Example #4
0
        private static async Task PublishThisEvent <T>(T domainEvent) where T : IDomainEvent
        {
            var type = domainEvent.GetType();

            var eventHandlers = ChakadContainer.ResolveEventSubscribers(type);

            var orderOf = OrderConfiger.GetOrderOf(type);

            Parallel.ForEach(orderOf, async order =>
            {
                if (!eventHandlers.Contains(order))
                {
                    return;
                }

                using (var scope = ChakadContainer.Autofac.BeginLifetimeScope(ChakadContainer.AutofacScopeName))
                {
                    var handler = scope.ResolveOptional(order);

                    var concreteType = typeof(IWantToHandleEvent <>).MakeGenericType(order);

                    await(Task) concreteType.GetMethod("Handle").Invoke(handler, new object[] { domainEvent });
                }
            }
                             );

            Parallel.ForEach(eventHandlers.Except(orderOf), async newInstance =>
            {
                using (var scope = ChakadContainer.Autofac.BeginLifetimeScope(ChakadContainer.AutofacScopeName))
                {
                    var handler = scope.ResolveOptional(newInstance);

                    var concreteType = typeof(IWantToHandleEvent <>).MakeGenericType(newInstance);

                    await(Task) concreteType.GetMethod("Handle").Invoke(handler, new object[] { domainEvent });
                }
            });
        }