public static Configure SetDefaultContainer(this Configure configure)
        {
            Logger.LogInformation(EventIdConstants.ChakadSetDefaultContainer,
                                  $"Chakad. Set Default Container. {typeof(DefaultContainer).FullName}");

            ChakadContainer.Run(new DefaultContainer());
            return(configure);
        }
        public static Configure SetContainer(this Configure configure, IChakadContainer container)
        {
            Logger.LogSystemLog(EventIdConstants.ChakadSetContainer,
                                $"Chakad. Set Custom Container. {container.GetType().FullName}");

            ChakadContainer.Run(container);
            return(configure);
        }
        public async Task <TOut> Run <TOut>(IBusinessQuery <TOut> query, TimeSpan?timeout = null,
                                            Action <Exception, TimeSpan> action           = null, SendOptions options = null)
            where TOut : QueryResult
        {
            var commandType = query.GetType();

            var baseType = query.GetType().BaseType;

            if (baseType == null)
            {
                throw new Exception();
            }

            var eventHandler = ChakadContainer.ResolveQueryHandler(commandType);

            if (eventHandler == null)
            {
                throw new ChakadPipelineNotFoundHandler(@"Not found handler for {0}", query);
            }

            if (timeout == null)
            {
                timeout = new TimeSpan(0, 0, 0, 0, 500);
            }

            if (action == null)
            {
                action = (ex, time) =>
                {
                    //TODO log ex
                    Console.WriteLine(ex.ToString());
                };
            }

            var policy = Policy.Handle <Exception>()
                         .WaitAndRetryAsync(1, retryAttempt => timeout.Value, action);

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

                TOut result = null;

                await policy.ExecuteAsync(async() =>
                {
                    result = await InvokeMessageHandle(query, eventHandler, handler);
                    //result = await (Task<TOut>) concreteType.GetMethod("Handle").Invoke(handler, new object[] {query});
                });

                return(result);
            }
        }
Beispiel #4
0
        public async Task UnSubscribe <T>(IWantToHandleEvent <T> eventHandler, Type myEvent) where T : IDomainEvent
        {
            if (myEvent == null || eventHandler == null)
            {
                return;
            }

            var type1 = eventHandler.GetType();

            var key = typeof(T);

            ChakadContainer.UnRegister(type1, key);
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="eventHandler"></param>
        /// <param name="myEvent"></param>
        public static async Task InterestedIn <T>(this IWantToHandleEvent <T> eventHandler, Type type)
            where T : IDomainEvent
        {
            if (type == null || eventHandler == null)
            {
                return;
            }

            var type1 = eventHandler.GetType();

            var key = typeof(T);

            ChakadContainer.Register(type1, key);
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="eventHandler"></param>
        /// <param name="myEvent"></param>
        public static async Task DivorceFrom <T>(this IWantToHandleEvent <T> eventHandler, Type myEvent)
            where T : IDomainEvent
        {
            if (myEvent == null || eventHandler == null)
            {
                return;
            }

            var type1 = eventHandler.GetType();

            var key = typeof(T);

            ChakadContainer.UnRegister(type1, key);
        }
Beispiel #7
0
        public async Task <TOut> Send <TOut>(IChakadRequest <TOut> command, TimeSpan?timeout = null,
                                             Action <Exception, TimeSpan> action             = null, SendOptions options = null) where TOut : ChakadResult, new()
        {
            var commandType = command.GetType();

            var baseType = command.GetType().BaseType;

            if (baseType == null)
            {
                throw new Exception();
            }

            var eventHandler = ChakadContainer.ResolveCommandHandler(commandType);

            if (eventHandler == null)
            {
                throw new ChakadPipelineNotFoundHandler(@"Not found handler for {0}", command);
            }

            if (timeout == null)
            {
                timeout = new TimeSpan(0, 0, 0, 0, 500);
            }

            if (action == null)
            {
                action = (ex, time) =>
                {
                    //TODO log ex
                    Console.WriteLine(ex.ToString());
                };
            }

            var policy = Policy.Handle <Exception>()
                         .WaitAndRetryAsync(1, retryAttempt => timeout.Value, action);

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

                TOut result = null;

                await policy.ExecuteAsync(async() =>
                {
                    result = await InvokeMessageHandle(command, eventHandler, handler);
                });

                return(result);
            }
        }
Beispiel #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="types"></param>
        public static Configure With(List <Type> types)
        {
            foreach (var type in types)
            {
                if (type.IsImplementInterface(typeof(IWantToHandleThisEventInterface)))
                {
                    RegisterSubscribers(type);
                }
                else if (type.IsImplementInterface(typeof(IHandleMessage)))
                {
                    ChakadContainer.RegisterMessageHandlers(type);
                }
            }

            return(Instance);
        }
        public async Task UnSubscribe <T>(IWantToSubscribeThisEvent <T> eventHandler, Type myEvent) where T : IDomainEvent
        {
            if (myEvent == null || eventHandler == null)
            {
                return;
            }

            var type1 = eventHandler.GetType();

            Logger.LogInformation(EventIdConstants.UnSubscribeFromEvent,
                                  $"Start UnSubscribing {type1.FullName} to {myEvent.FullName} .");

            var key = typeof(T);

            ChakadContainer.UnRegister(type1, key);
        }
        public async Task Subscribe <T>(IWantToSubscribeThisEvent <T> eventHandler, Type type)
            where T : IDomainEvent
        {
            if (type == null || eventHandler == null)
            {
                return;
            }

            Logger.LogInformation(EventIdConstants.SubscribeToEvent,
                                  $"Start Subscribing {eventHandler.GetType().FullName} to {type.FullName} .");

            var type1 = eventHandler.GetType();

            var key = typeof(T);

            ChakadContainer.Register(type1, key);
        }
        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 });
                }
            });
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="types"></param>
        public static Configure With(this Configure configure, List <Type> types)
        {
            Logger.LogSystemLog(EventIdConstants.ChakadInitilizingTypes,
                                $"Chakad Initializing types count is ={types.Count} ");

            foreach (var type in types)
            {
                if (type.IsImplementInterface(typeof(IWantToSubscribeThisEventInterface)))
                {
                    RegisterSubscribers(type);
                }
                else if (type.IsImplementInterface(typeof(IHandleMessage)))
                {
                    ChakadContainer.RegisterMessageHandlers(type);
                }
            }

            return(configure);
        }
        private static void RegisterSubscribers(Type type)
        {
            var handler =
                type.GetInterfaces()
                .ToList()
                .FirstOrDefault(type1 => type1 != typeof(IWantToSubscribeThisEventInterface));

            if (handler == null || !handler.IsGenericType)
            {
                return;
            }

            var genericArguments = handler.GetGenericArguments().ToList()
                                   .Where(type1 => type1.IsImplementInterface(typeof(IDomainEvent))).ToList();

            foreach (var type1 in genericArguments.ToList().Where(type1 => type1 != null))
            {
                ChakadContainer.Register(type, type1);
            }
        }
Beispiel #14
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 });
                }
            });
        }
Beispiel #15
0
        public async Task <TOut> StartProcess <TOut>(IBusinessQuery <TOut> query, TimeSpan?timeout = null,
                                                     Action <Exception, TimeSpan> action           = null, SendOptions options = null)
            where TOut : class
        {
            var queryType = query.GetType();

            Logger.LogInformation(EventIdConstants.QueryStartProcess, queryType.FullName, query, $"Start Process query with Correlation_Id = {query.CorrelationId}");

            var baseType = query.GetType().BaseType;

            if (baseType == null)
            {
                Logger.LogError(EventIdConstants.QueryBaseTypeIsEmpty, query.CorrelationId, $"StartProcess. Correlation_Id is ={query.CorrelationId} Query base type is null.");

                throw new Exception();
            }

            var eventHandler = ChakadContainer.ResolveQueryHandler(queryType);

            if (eventHandler == null)
            {
                var exeption = new ChakadPipelineNotFoundHandler(@"Not found handler for {0}", query.CorrelationId);

                Logger.LogError(EventIdConstants.QueryNotFoundHandler, exeption, query.CorrelationId,
                                $"StartProcess. Correlation_Id is ={query.CorrelationId}. Not found handler");
                throw exeption;
            }

            if (timeout == null)
            {
                timeout = new TimeSpan(0, 0, 0, 0, 500);
            }

            if (action == null)
            {
                action = (ex, time) =>
                {
                    //TODO log ex
                    Console.WriteLine(ex.ToString());
                };
            }

            var policy = Policy.Handle <Exception>()
                         .WaitAndRetryAsync(1, retryAttempt => timeout.Value, action);

            Logger.LogInformation(EventIdConstants.QueryInitializeCircuteBreaker,
                                  query.CorrelationId,
                                  $"Start Process. Correlation_Id is ={query.CorrelationId}  Set Retry Count equals to 2 in case of any failures in invoking message handler.");


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

                TOut result = null;

                await policy.ExecuteAsync(async() =>
                {
                    result = await InvokeMessageHandle(query, eventHandler, handler);
                });

                return(result);
            }
        }
 private static void ConfigContainer()
 {
     _chakavakContainer = new ChakadContainer()
                          .RegisterIdentity();
 }
 private static void BuildContainer()
 {
     ChakadContainer.Build();
 }
Beispiel #18
0
 public static Configure SetDefaultContainer(this Configure configure)
 {
     ChakadContainer.Run(new DefaultContainer());
     return(configure);
 }
Beispiel #19
0
 public static Configure SetContainer(this Configure configure, IChakadContainer container)
 {
     ChakadContainer.Run(container);
     return(configure);
 }