Exemplo n.º 1
0
        protected virtual IAsyncQueryHandler <TQuery, TResult> Resolve <TQuery, TResult>(CancellationToken cancellationToken) where TQuery : IQuery <TResult>
        {
            cancellationToken.ThrowIfCancellationRequested();

            this.logger.LogDebug($"Resolving the \"{typeof(TQuery).GetTypeInfo().Name}\" query and \"{typeof(TResult).GetTypeInfo().Name}\" result.");
            IAsyncQueryHandler <TQuery, TResult> handler = this.GetQueryHandler <TQuery, TResult>();

            if (handler == null)
            {
                this.logger.LogCritical($"Trying to resolve a handler for the \"{typeof(TQuery).GetTypeInfo().Name}\" query and \"{typeof(TResult).GetTypeInfo().Name}\" result failed. No handler found.");
                throw new RequiredHandlerNotFoundException();
            }

            //try {
            IEnumerable <Attribute> attrs = typeof(TQuery).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(TQuery).GetTypeInfo().Name}\" query and \"{typeof(TResult).GetTypeInfo().Name}\" result.");
                // Let's get the permission check handler, we need it.
                IAsyncQueryHandler <TQuery, TResult> permissionCheckHandler = this.GetPermissionCheckHandler <TQuery, TResult>(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(TQuery), typeof(TResult));
                    }
                    else
                    {
                        this.logger.LogWarning($"A permission check decorator was found for the \"{typeof(TQuery).GetTypeInfo().Name}\" query and \"{typeof(TResult).GetTypeInfo().Name}\" result, but no handler was located.");
                    }
                }
                else
                {
                    handler = permissionCheckHandler;
                    this.logger.LogDebug($"Found the permission check handle \"{permissionCheckHandler.GetType().Name}\" for the \"{typeof(TQuery).Name}\" query and \"{typeof(TResult).GetTypeInfo().Name}\" result.");
                }
            }
            else
            {
                this.logger.LogWarning($"No permission decorator found for the \"{typeof(TQuery).GetTypeInfo().Name}\" query and \"{typeof(TResult).GetTypeInfo().Name}\" result.");
            }

            // Any validation decorator found on the query?
            if (attrs.Any(a => a.GetType() == typeof(CreativeMinds.CQS.Decorators.ValidateAttribute)))
            {
                this.logger.LogDebug($"Found a validation decorator for the \"{typeof(TQuery).Name}\" query and \"{typeof(TResult).GetTypeInfo().Name}\" result.");
                // Let's get the validation handler, we need it.
                IAsyncQueryHandler <TQuery, TResult> validationHandler = this.GetValidationHandler <TQuery, TResult>(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(TQuery), typeof(TResult));
                    }
                    else
                    {
                        this.logger.LogWarning($"A validation decorator was found for the \"{typeof(TQuery).GetTypeInfo().Name}\" query and \"{typeof(TResult).GetTypeInfo().Name}\" result, but no handler was located.");
                    }
                }
                else
                {
                    handler = validationHandler;
                    this.logger.LogDebug($"Found the validation handle \"{validationHandler.GetType().Name}\" for the \"{typeof(TQuery).Name}\" query and \"{typeof(TResult).GetTypeInfo().Name}\" result.");
                }
            }
            else
            {
                this.logger.LogWarning($"No validation decorator found for the \"{typeof(TQuery).GetTypeInfo().Name}\" query and \"{typeof(TResult).GetTypeInfo().Name}\" result.");
            }

            //}
            //catch (Exception ex) {
            //	// TODO: log
            //	throw ex;
            //}

            this.logger.LogInformation($"Found the \"{handler.GetType().Name}\" QueryHandler for the \"{typeof(TQuery).GetTypeInfo().Name}\" query and \"{typeof(TResult).GetTypeInfo().Name}\" result");
            return(handler);
        }
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);
        }