Пример #1
0
        public CommandHandlerProvider(Type type, CommandHandlerActionDescriptor memberDescriptor)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            _type             = type;
            _memberDescriptor = memberDescriptor;
        }
Пример #2
0
        public CommandHandlerInvoker(object handler, CommandHandlerActionDescriptor memberDescriptor, IServiceProvider serviceProvider)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            _handler          = handler;
            _memberDescriptor = memberDescriptor;
            _serviceProvider  = serviceProvider;
        }
Пример #3
0
        private bool TryGetHandlingMember(MethodInfo member, out CommandHandlerActionDescriptor result)
        {
            var parameters = member.GetParameters();

            if (parameters.Length == 0)
            {
                result = default;
                return(false);
            }

            if (parameters.Any(p => p.ParameterType.IsByRef))
            {
                result = default;
                return(false);
            }

            if (member.IsGenericMethod || member.IsGenericMethodDefinition)
            {
                result = default;
                return(false);
            }

            if (member.IsDefined <NoCommandHandlerActionAttribute>())
            {
                result = default;
                return(false);
            }

            var commandType = parameters[0].ParameterType;

            var actionAttribute = member.GetCustomAttribute <CommandHandlerActionAttribute>();

            if (actionAttribute != null && actionAttribute.CommandType != null)
            {
                if (!commandType.IsAssignableFrom(actionAttribute.CommandType))
                {
                    throw new InvalidOperationException();
                }

                commandType = actionAttribute.CommandType;
            }

            // Synchronous handler
            if ((member.Name == "Handle" || actionAttribute != null) &&
                (member.ReturnType == typeof(void) || !typeof(Task).IsAssignableFrom(member.ReturnType)))
            {
                result = new CommandHandlerActionDescriptor(commandType, member);
                return(true);
            }

            // Asynchronous handler
            if ((member.Name == "HandleAsync" || actionAttribute != null) &&
                (typeof(Task).IsAssignableFrom(member.ReturnType)))
            {
                result = new CommandHandlerActionDescriptor(commandType, member);
                return(true);
            }

            result = default;
            return(false);
        }