示例#1
0
 public void Invoke(IEvent evnt, MethodInfo handlerMethod, EventDispatchingContext context)
 {
     if (TypeUtil.IsAttributeDefinedInMethodOrDeclaringClass(handlerMethod, typeof(HandleAsyncAttribute)))
     {
         Task.Factory.StartNew(() => InvokeHandler(evnt, handlerMethod, context));
     }
     else
     {
         InvokeHandler(evnt, handlerMethod, context);
     }
 }
示例#2
0
        private void InvokeHandler(IEvent evnt, MethodInfo method, EventDispatchingContext context)
        {
            var handlerType = method.DeclaringType;
            var handler = CreateHandlerInstance(handlerType, context);

            try
            {
                method.Invoke(handler, new object[] { evnt });
            }
            catch (Exception ex)
            {
                throw new EventHandlerException("Event handler throws an exception, please check inner exception for detail. Handler type: " + handlerType + ".", ex);
            }
        }
示例#3
0
        public void Dispatch(IEvent evnt, EventDispatchingContext context)
        {
            Require.NotNull(evnt, "evnt");
            Require.NotNull(context, "context");

            var eventType = evnt.GetType();

            // Dispatch event to system handlers
            Dispatch(evnt, context, _handlerRegistry.FindHandlerMethods(EventConstants.SystemHandlerGroup, eventType));

            // Dispatch event to plugin handlers
            foreach (var plugin in PluginManager.EnabledPlugins)
            {
                Dispatch(evnt, context, _handlerRegistry.FindHandlerMethods(plugin.Name, eventType));
            }
        }
示例#4
0
        private object CreateHandlerInstance(Type handlerType, EventDispatchingContext context)
        {
            try
            {
                var handler = Activator.CreateInstance(handlerType);
                var unitOfWorkAware = handler as IUnitOfWorkAware;

                if (unitOfWorkAware != null)
                {
                    unitOfWorkAware.UnitOfWork = context.UnitOfWork;
                }

                return handler;
            }
            catch (Exception ex)
            {
                throw new EventHandlerException("Failed creating event handler instance. Handler type: " + handlerType + ".", ex);
            }
        }
示例#5
0
        private void Dispatch(IEvent evnt, EventDispatchingContext context, IEnumerable<MethodInfo> handlerMethods)
        {
            foreach (var method in handlerMethods)
            {
                var awaitCommit = TypeUtil.IsAttributeDefinedInMethodOrDeclaringClass(method, typeof(AwaitCommittedAttribute));

                if (awaitCommit && context.UnitOfWorkStage != UnitOfWorkStage.Committed)
                {
                    continue;
                }

                if (!awaitCommit && context.UnitOfWorkStage == UnitOfWorkStage.Committed)
                {
                    continue;
                }

                _handlerInvoker.Invoke(evnt, method, context);
            }
        }