コード例 #1
0
        public static void RegisterEvent <T>(Action <T> handle, string subscriberName) where T : IEvent
        {
            var t = typeof(T).FullName;

            lock (_eventLocker)
            {
                List <Action <IEvent> > ax;

                if (_eventHandler.TryGetValue(t, out ax))
                {
                    ax.Add(p => handle((T)p));
                }
                else
                {
                    ax = new List <Action <IEvent> >()
                    {
                        p => handle((T)p)
                    };
                }

                _eventHandler[t] = ax;
            }
            RegisterCommandsEventsForWorker(typeof(T));

            EngineeEventWorkerQueue.Subscribe(t, (o) =>
            {
                handle((T)o);
            }, subscriberName);

            _listSubscriber[subscriberName] = subscriberName;
        }
コード例 #2
0
        public static void RegisterAssembly(Assembly executingAssembly)
        {
            var allTypes = executingAssembly.GetTypes();

            RegisterCommandsEventsForWorker(allTypes);

            var listHandler = allTypes.Where(t => typeof(ICqrsHandle).IsAssignableFrom(t) &&
                                             t.IsClass && !t.IsAbstract).ToList();

            var assemblyFullName = executingAssembly.FullName;

            if (listHandler.Count <= 0)
            {
                //Console.WriteLine("Not found ICqrsHandle in " + assemblyFullName);
                return;
            }

            Console.WriteLine($"+-{assemblyFullName}");
            //Console.WriteLine($"Found {listHandler.Count} handle(s) to register to message buss");

            foreach (var handlerType in listHandler)
            {
                var cqrsHandler = (ICqrsHandle)Activator.CreateInstance(handlerType);
                if (cqrsHandler == null)
                {
                    continue;
                }

                Console.WriteLine($"Found ICqrsHandle type: {cqrsHandler.GetType()}");

                MethodInfo[] allMethod = cqrsHandler.GetType()
                                         .GetMethods(BindingFlags.Public | BindingFlags.Instance);

                foreach (var mi in allMethod)
                {
                    var methodName = mi.Name;
                    if (!methodName.Equals("handle", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    var pParameterType = mi.GetParameters().SingleOrDefault().ParameterType;

                    if (typeof(IEvent).IsAssignableFrom(pParameterType))
                    {
                        var t = pParameterType.FullName;

                        lock (_eventLocker)
                        {
                            RegisterCommandsEventsForWorker(pParameterType);

                            List <Action <IEvent> > ax;

                            if (_eventHandler.TryGetValue(t, out ax))
                            {
                                ax.Add(p =>
                                {
                                    mi.Invoke(cqrsHandler, new object[] { p });
                                });
                            }
                            else
                            {
                                ax = new List <Action <IEvent> >()
                                {
                                    p =>
                                    {
                                        mi.Invoke(cqrsHandler, new object[] { p });
                                    }
                                };
                            }

                            _eventHandler[t] = ax;
                        }

                        var className = mi.DeclaringType.FullName;

                        EngineeEventWorkerQueue.Subscribe(t, (o) =>
                        {
                            mi.Invoke(cqrsHandler, new object[] { o });
                        }, className);

                        _listSubscriber[className] = className;

                        Console.WriteLine($"Regsitered method to process Event type: {pParameterType}");
                    }

                    if (typeof(ICommand).IsAssignableFrom(pParameterType))
                    {
                        lock (_commandLocker)
                        {
                            var t = pParameterType.FullName;
                            RegisterCommandsEventsForWorker(pParameterType);

                            Action <ICommand> ax;

                            if (_commandHandler.TryGetValue(t, out ax))
                            {
                                return;
                                // throw new Exception($"Should only one handle to cover type: {t}. Check DomainEngine.Boot");
                            }

                            _commandHandler[t] = (p) =>
                            {
                                mi.Invoke(cqrsHandler, new object[] { p });
                            };
                        }
                        Console.WriteLine($"Regsitered method to process Command type: {pParameterType}");
                    }
                }
            }
        }