private void SubscribeToProductEntitiesUpdatedQueue(string queueName)
        {
            var responseType = typeof(EntitiesChangedResponse <>);

            //This is where we store the handlers to pass to busManager
            HandlerManager handlerManager = new HandlerManager();

            //Loop through entity types and create QueueMessageHandler of type
            foreach (var entityType in EntityTypes.EntityBaseTypes)
            {
                //Get onRequest method with appropriate request and entity type
                var entityResponseType    = responseType.MakeGenericType(entityType);
                var onResponseMethod      = GenericHelpers.GetMethodExt(this.GetType(), "OnAllEntitiesUpdatedResponse", typeof(void), new Type[] { entityResponseType, typeof(MessageReceivedInfo) });
                var onResponseMethodTyped = onResponseMethod.MakeGenericMethod(entityType);

                //Create action delegate from typed onRequest method
                var actionType = typeof(Action <,>).MakeGenericType(entityResponseType, typeof(MessageReceivedInfo));
                var action     = Delegate.CreateDelegate(actionType, this, onResponseMethodTyped);

                //Create QueueMessageHandler of approriate type and pass in action and queueName
                var queueMessageHandlerType = typeof(QueueMessageHandler <>).MakeGenericType(entityResponseType);
                var accessor            = TypeAccessor.Create(queueMessageHandlerType);
                var queueMessageHandler = accessor.CreateNew();
                accessor[queueMessageHandler, "Handler"]   = action;
                accessor[queueMessageHandler, "QueueName"] = queueName;

                //Get AddHandler method from handlerManager to add appropriate request type and invoke
                var addHandlerMethod      = GenericHelpers.GetMethodExt(handlerManager.GetType(), "AddHandler", typeof(void), new Type[] { queueMessageHandlerType });
                var addHandlerMethodTyped = addHandlerMethod.MakeGenericMethod(entityResponseType);
                addHandlerMethodTyped.Invoke(handlerManager, new object[] { queueMessageHandler });
            }

            //Subscribe with our filled up handlerManager
            _busManager.Subscribe(_busManager.TopicExchange, queueName, handlerManager);
        }
Beispiel #2
0
        private void SubscribeToEntityQueues()
        {
            //Add message handlers for all request types and entity types
            foreach (var requestType in EntityTypes.EntityRequestTypes)
            {
                //This is where we store the handlers to pass to busManager
                HandlerManager handlerManager = new HandlerManager();

                //Do one queue/request type at a time
                string requestQueue = null;

                foreach (var entityType in EntityTypes.EntityBaseTypes)
                {
                    //Get onRequest method (below) with appropriate request and entity type
                    var entityRequestType    = requestType.MakeGenericType(entityType);
                    var onRequestMethod      = GenericHelpers.GetMethodExt(this.GetType(), "OnRequest", typeof(void), new Type[] { requestType, typeof(MessageReceivedInfo) });
                    var onRequestMethodTyped = onRequestMethod.MakeGenericMethod(entityType);

                    //Create action delegate from typed onRequest method
                    var actionType = typeof(Action <,>).MakeGenericType(entityRequestType, typeof(MessageReceivedInfo));
                    var action     = Delegate.CreateDelegate(actionType, this, onRequestMethodTyped);

                    //Instantiate a request of appropriate type to get at its request queue
                    if (requestQueue == null)
                    {
                        var requestAccessor = TypeAccessor.Create(entityRequestType);
                        var request         = requestAccessor.CreateNew();
                        requestQueue = (string)requestAccessor[request, "RequestQueue"];
                    }

                    //Create QueueMessageHandler of approriate type and pass in action and queueName
                    var queueMessageHandlerType = typeof(QueueMessageHandler <>).MakeGenericType(entityRequestType);
                    var accessor            = TypeAccessor.Create(queueMessageHandlerType);
                    var queueMessageHandler = accessor.CreateNew();
                    accessor[queueMessageHandler, "Handler"]   = action;
                    accessor[queueMessageHandler, "QueueName"] = requestQueue;

                    //Get AddHandler method from handlerManager to add appropriate request type
                    var addHandlerMethod      = GenericHelpers.GetMethodExt(handlerManager.GetType(), "AddHandler", typeof(void), new Type[] { queueMessageHandlerType });
                    var addHandlerMethodTyped = addHandlerMethod.MakeGenericMethod(entityRequestType);
                    addHandlerMethodTyped.Invoke(handlerManager, new object[] { queueMessageHandler });
                }

                //Subscribe with our filled up handlerManager
                _busManager.Subscribe(_busManager.TopicExchange, requestQueue, handlerManager);
            }
        }
        private IHandlerRegistration AddHandlers(IHandlerRegistration handlerReg, IQueue queue)
        {
            //This is gonna be a bit NASTY
            //For this queue we need to find all the types we're currently holding handlers for
            //then add the internal onMessageReceived handler for each type
            //The reason for all this is calling consume on a queue with existing handlers for types
            //wipes them out and we have to add all type handlers again - so each time a type is added to be subscribed to
            //the whole previous set need to be re-added

            foreach (Type requestType in EntityTypes.EntityRequestAndResponseTypes)
            {
                foreach (Type entityType in EntityTypes.EntityBaseTypes)
                {
                    var requestWithEntityType   = requestType.MakeGenericType(entityType);
                    var queueMessageHandlerType = typeof(QueueMessageHandler <>).MakeGenericType(requestWithEntityType);
                    var queueMessageHandlerList = GenericHelpers.InvokeGenericMethod(_handlerManager, requestWithEntityType, typeof(IList), "GetHandlersForType", queue.Name);
                    var list = queueMessageHandlerList as IList;

                    if (list != null && list.Count > 0)
                    {
                        var messageType    = typeof(IMessage <>).MakeGenericType(requestWithEntityType);
                        var addParamType   = typeof(Action <,>).MakeGenericType(messageType, typeof(MessageReceivedInfo));
                        var addMethod      = GenericHelpers.GetMethodExt(handlerReg.GetType(), "Add", handlerReg.GetType(), addParamType);
                        var addMethodTyped = addMethod.MakeGenericMethod(requestWithEntityType);

                        var onMessageMethod      = GenericHelpers.GetMethodExt(this.GetType(), "OnMessageReceived", typeof(void), new Type[] { messageType, typeof(MessageReceivedInfo) });
                        var onMessageMethodTyped = onMessageMethod.MakeGenericMethod(requestWithEntityType);

                        var action = Delegate.CreateDelegate(addParamType, this, onMessageMethodTyped);

                        handlerReg = (IHandlerRegistration)addMethodTyped.Invoke(handlerReg, new object[] { action });
                    }
                }
            }

            return(handlerReg);
        }