public async Task HandleAsync(TCommand command)
        {
            _logger.Info($"{command.GetType().Name}  ====>  {_innerHandler.GetType().Name}: {command.ToJson()}");

            await _innerHandler.HandleAsync(command);
        }
Exemplo n.º 2
0
        protected virtual IAsyncCommandHandler <TCommand> Resolve <TCommand>() where TCommand : ICommand
        {
            this.logger.LogDebug($"Resolving the \"{typeof(TCommand).Name}\" command.");
            //try {
            // Let's get the command handler, we're going to need this, no matter what!!
            IAsyncCommandHandler <TCommand> handler = this.GetCommandHandler <TCommand>();

            if (handler == null)
            {
                this.logger.LogCritical($"Trying to resolve a handler for the \"{typeof(TCommand).Name}\" command failed. No handler found.");
                throw new RequiredHandlerNotFoundException();
            }

            IEnumerable <Attribute> attrs = typeof(TCommand).GetTypeInfo().GetCustomAttributes();

            // Any permission check decorator found on the command?
            if (attrs.Any(a => a.GetType() == typeof(CreativeMinds.CQS.Decorators.CheckPermissionsAttribute)) ||
                attrs.Any(a => a.GetType().GetTypeInfo().BaseType == typeof(CreativeMinds.CQS.Decorators.CheckPermissionsAttribute)))
            {
                this.logger.LogDebug($"Found a permission check decorator for the \"{typeof(TCommand).Name}\" command.");
                // Let's get the permission check handler, we need it.
                IAsyncCommandHandler <TCommand> permissionCheckHandler = this.GetPermissionCheckHandler <TCommand>(handler);
                if (permissionCheckHandler == null)
                {
                    CreativeMinds.CQS.Decorators.CheckPermissionsAttribute attr = attrs.FirstOrDefault(a => a.GetType() == typeof(CreativeMinds.CQS.Decorators.CheckPermissionsAttribute)) as CreativeMinds.CQS.Decorators.CheckPermissionsAttribute;
                    if (attr == null)
                    {
                        attr = attrs.FirstOrDefault(a => a.GetType().BaseType == typeof(CreativeMinds.CQS.Decorators.CheckPermissionsAttribute)) as CreativeMinds.CQS.Decorators.CheckPermissionsAttribute;
                    }

                    if (attr.FailWhenMissing == true)
                    {
                        throw new NoCheckPermissionsHandlerImplementedException(typeof(TCommand));
                    }
                    else
                    {
                        this.logger.LogWarning($"A permission check decorator was found for the \"{typeof(TCommand).GetTypeInfo().Name}\" command, but no handler was located.");
                    }
                }
                else
                {
                    handler = permissionCheckHandler;
                    this.logger.LogDebug($"Found the permission check handle \"{permissionCheckHandler.GetType().Name}\" for the \"{typeof(TCommand).Name}\" command.");
                }
            }
            else
            {
                this.logger.LogWarning($"No permission decorator found for the \"{typeof(TCommand).Name}\" command.");
            }

            // Any validation decorator found on the command?
            if (attrs.Any(a => a.GetType() == typeof(CreativeMinds.CQS.Decorators.ValidateAttribute)))
            {
                this.logger.LogDebug($"Found a validation decorator for the \"{typeof(TCommand).Name}\" command.");
                // Let's get the validation handler, we need it.
                IAsyncCommandHandler <TCommand> validationHandler = this.GetValidationHandler <TCommand>(handler);
                if (validationHandler == null)
                {
                    CreativeMinds.CQS.Decorators.ValidateAttribute attr = attrs.FirstOrDefault(a => a.GetType() == typeof(CreativeMinds.CQS.Decorators.ValidateAttribute)) as CreativeMinds.CQS.Decorators.ValidateAttribute;
                    if (attr == null)
                    {
                        attr = attrs.FirstOrDefault(a => a.GetType().BaseType == typeof(CreativeMinds.CQS.Decorators.ValidateAttribute)) as CreativeMinds.CQS.Decorators.ValidateAttribute;
                    }

                    if (attr.FailWhenMissing == true)
                    {
                        throw new NoValidatorHandlerImplementedException(typeof(TCommand));
                    }
                    else
                    {
                        this.logger.LogWarning($"A validation decorator was found for the \"{typeof(TCommand).GetTypeInfo().Name}\" command, but no handler was located.");
                    }
                }
                else
                {
                    handler = validationHandler;
                    this.logger.LogDebug($"Found the validation handle \"{validationHandler.GetType().Name}\" for the \"{typeof(TCommand).Name}\" command.");
                }
            }
            else
            {
                this.logger.LogWarning($"No validation decorator found for the \"{typeof(TCommand).Name}\" command.");
            }

            //}
            //catch (Exception ex) {
            //	logger.LogError("An exception occured trying to ")
            //	throw ex;
            //}

            this.logger.LogInformation($"Found the \"{handler.GetType().Name}\" CommandHandler for the \"{typeof(TCommand).GetTypeInfo().Name}\" command");
            return(handler);
        }