Esempio n. 1
0
        public Task PublishAsync <TEvent>(TEvent payload)
        {
            Ensure.NotNull(payload, "payload");

            Type eventType = typeof(TEvent);
            TypeResolverResult eventTypeDescriptor = eventTypeResolver.Resolve(eventType);

            // Throw, because passing in context is not supported.
            if (eventTypeDescriptor.IsContext)
            {
                throw Ensure.Exception.NotSupported("Event manager can publish event context.");
            }

            // Unwrap envelope, create context and invoke.
            if (eventTypeDescriptor.IsEnvelope)
            {
                Type   contextType = typeof(DefaultEventHandlerContext <>).MakeGenericType(eventTypeDescriptor.DataType);
                object context     = Activator.CreateInstance(contextType, payload, this, this);

                // Get publishing method and make it generic for current event.
                MethodInfo publishInternalMethod = typeof(DefaultEventManager).GetMethod("PublishInternalAsyc", BindingFlags.NonPublic | BindingFlags.Instance);
                if (publishInternalMethod == null)
                {
                    throw Ensure.Exception.NotImplemented("Bug in implementation of DefaultEventManager. Unnable to find publishing method.");
                }

                return((Task)publishInternalMethod.MakeGenericMethod(eventTypeDescriptor.DataType).Invoke(this, new object[] { context }));
            }

            // Create context and invoke.
            return(PublishInternalAsyc <TEvent>(new DefaultEventHandlerContext <TEvent>(payload, this, this)));
        }
Esempio n. 2
0
        public IEventHandlerCollection Add <TEvent>(IEventHandler <TEvent> handler)
        {
            Ensure.NotNull(handler, "handler");

            Type eventType = typeof(TEvent);
            TypeResolverResult eventTypeDescriptor = eventTypeResolver.Resolve(eventType);

            if (eventTypeDescriptor.IsContext)
            {
                registry.AddContextHandler(eventTypeDescriptor.DataType, handler);
            }
            else if (eventTypeDescriptor.IsEnvelope)
            {
                registry.AddEnvelopeHandler(eventTypeDescriptor.DataType, handler);
            }
            else
            {
                registry.AddDirectHandler(eventTypeDescriptor.DataType, handler);
            }

            return(this);
        }