예제 #1
0
        /// <summary>
        /// Creates a message dispatcher and registers all the message handlers
        /// specified in the <see cref="Apworks.Config.IConfigSource"/> instance.
        /// </summary>
        /// <param name="configSource">The <see cref="Apworks.Config.IConfigSource"/> instance
        /// that contains the definitions for message handlers.</param>
        /// <param name="messageDispatcherType">The type of the message dispatcher.</param>
        /// <param name="args">The arguments that is used for initializing the message dispatcher.</param>
        /// <returns>A <see cref="Apworks.Bus.IMessageDispatcher"/> instance.</returns>
        public static IMessageDispatcher CreateAndRegister(IConfigSource configSource,
                                                           Type messageDispatcherType,
                                                           params object[] args)
        {
            IMessageDispatcher messageDispatcher = (IMessageDispatcher)Activator.CreateInstance(messageDispatcherType,
                                                                                                args);

            HandlerElementCollection handlerElementCollection = configSource.Config.Handlers;

            foreach (HandlerElement handlerElement in handlerElementCollection)
            {
                switch (handlerElement.SourceType)
                {
                case HandlerSourceType.Type:
                    string typeName    = handlerElement.Source;
                    Type   handlerType = Type.GetType(typeName);
                    RegisterType(messageDispatcher, handlerType);
                    break;

                case HandlerSourceType.Assembly:
                    string   assemblyString = handlerElement.Source;
                    Assembly assembly       = Assembly.Load(assemblyString);
                    RegisterAssembly(messageDispatcher, assembly);
                    break;
                }
            }
            return(messageDispatcher);
        }
        public IDictionary <Type, Type> GetCommandHandlers()
        {
            IDictionary <Type, Type> commandHandlerDictionary = new Dictionary <Type, Type>();

            HandlerElementCollection handlerElements = EAppRuntime.Instance.CurrentApp.ConfigSource.Config.Handlers;

            if (handlerElements != null &&
                handlerElements.Count > 0)
            {
                for (int handlerIndex = 0; handlerIndex < handlerElements.Count; handlerIndex++)
                {
                    HandlerElement handlerElement = handlerElements[handlerIndex];

                    string handlerName = handlerElement.Name;

                    string handlerTypeName = handlerElement.Type;

                    Type handlerType = Type.GetType(handlerTypeName);

                    var commandHandlerInterfaceQuery = from c in handlerType.GetInterfaces()
                                                       where c.IsGenericType &&
                                                       c.GetGenericTypeDefinition() == typeof(ICommandHandler <>)
                                                       select c;

                    foreach (var commandHandlerInterface in commandHandlerInterfaceQuery)
                    {
                        Type commandType = commandHandlerInterface.GetGenericArguments().FirstOrDefault();

                        if (typeof(ICommand).IsAssignableFrom(commandType))
                        {
                            commandHandlerDictionary.Add(commandType, handlerType);
                        }
                    }
                }
            }

            return(commandHandlerDictionary);
        }