public async Task <TResult> HandleAsync(TQuery query) { var result = await _innerHandler.HandleAsync(query).ConfigureAwait(false); string resultString; try { resultString = result.ToJson(); } catch (Exception) { resultString = "{}"; } _logger.Info($"{query.GetType().Name} ====> {_innerHandler.GetType().Name}: {query.ToJson()} => {resultString}"); return(result); }
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); }