Exemplo n.º 1
0
        private void InitializeDispatchTable(IEnumerable <Type> types)
        {
            // Create a temporary holding dictionary for the type->instance map
            var handlers = new Dictionary <MessageRegistration, IMessageHandler>();

            var filteredTypes = types.Where(t => typeof(IMessageHandler).IsAssignableFrom(t));

            // Iterate through the types and generate a type->instance of type
            // map
            foreach (var type in filteredTypes)
            {
                // Each handler may be registered to multiple types
                var registrations = type.GetCustomAttributes(
                    typeof(MessageHandlerAttribute), true);

                foreach (MessageHandlerAttribute registration in registrations)
                {
                    // Create an instance of the type and registration key
                    var handler = _resolver.GetService(type) as IMessageHandler;
                    if (handler == null)
                    {
                        Logger.Error(
                            "Requested type {0} does not return a valid IMessageHandler from the dependency resolver",
                            type.AssemblyQualifiedName);
                        continue;
                    }

                    var key = new MessageRegistration
                    {
                        Type    = registration.MessageType,
                        Version = registration.Version
                    };

                    Logger.Debug(
                        "Registering type {0} to handle message type {1}.",
                        type.AssemblyQualifiedName,
                        key);

                    // Add to the set of handlers
                    handlers.Add(key, handler);
                }
            }

            // Convert the temporary map into an immutable dictionary for
            // performance in concurrent applications
            Logger.Debug("Registering {0} handlers", handlers.Keys.Count);
            this._dispatchTable = handlers.ToImmutableDictionary();
        }
        public void WhenVersionsDiffer_ThenRegistrationsShouldBeEqual()
        {
            const string SharedType = "some type";

            var a = new MessageRegistration
            {
                Type = SharedType,
                Version = 1
            };
            var b = new MessageRegistration
            {
                Type = SharedType,
                Version = 2
            };

            Assert.False(a.Equals(b));
        }
        public void WhenTypeDiffers_ThenRegistrationsShouldNotBeEqual()
        {
            const int SharedVersion = 99;

            var a = new MessageRegistration
            {
                Type = "first type",
                Version = SharedVersion
            };
            var b = new MessageRegistration
            {
                Type = "second type",
                Version = SharedVersion
            };

            Assert.False(a.Equals(b));
        }
        public void WhenTypeAndVersionMatch_ThenRegistrationsShouldBeEqual()
        {
            const string SharedType = "some type";
            const int SharedVersion = 99;

            var a = new MessageRegistration
                        {
                            Type = SharedType,
                            Version = SharedVersion
                        };
            var b = new MessageRegistration
                        {
                            Type = SharedType,
                            Version = SharedVersion
                        };

            Assert.True(a.Equals(b));
        }
        private void InitializeDispatchTable(IEnumerable<Type> types)
        {
            // Create a temporary holding dictionary for the type->instance map
            var handlers = new Dictionary<MessageRegistration, IMessageHandler>();

            var filteredTypes = types.Where(t => typeof(IMessageHandler).IsAssignableFrom(t));

            // Iterate through the types and generate a type->instance of type
            // map
            foreach (var type in filteredTypes)
            {
                // Each handler may be registered to multiple types
                var registrations = type.GetCustomAttributes(
                    typeof(MessageHandlerAttribute), true);

                foreach (MessageHandlerAttribute registration in registrations)
                {
                    // Create an instance of the type and registration key
                    var handler = _resolver.GetService(type) as IMessageHandler;
                    if (handler == null)
                    {
                        Logger.Error(
                            "Requested type {0} does not return a valid IMessageHandler from the dependency resolver",
                            type.AssemblyQualifiedName);
                        continue;
                    }

                    var key = new MessageRegistration
                    {
                        Type = registration.MessageType,
                        Version = registration.Version
                    };

                    Logger.Debug(
                        "Registering type {0} to handle message type {1}.",
                        type.AssemblyQualifiedName,
                        key);

                    // Add to the set of handlers
                    handlers.Add(key, handler);
                }
            }

            // Convert the temporary map into an immutable dictionary for
            // performance in concurrent applications
            Logger.Debug("Registering {0} handlers", handlers.Keys.Count);
            this._dispatchTable = handlers.ToImmutableDictionary();
        }