public void Run <TQuery>(TQuery query) where TQuery : IQuery { if (query == null) { throw new ArgumentNullException(nameof(query)); } IQueryHandler <TQuery> queryHandler; try { queryHandler = QueryHandlerFactory.Create <TQuery>(); } catch (Exception ex) { throw new CannotCreateQueryHandlerException(ex); } if (queryHandler == null) { throw new CannotCreateQueryHandlerException(); } this.OnBeforeExecuteHandler(queryHandler, query); queryHandler.Execute(query); }
/// <summary> /// Executes a query handler based on the query type. /// </summary> /// <typeparam name="TQuery">A query type</typeparam> /// <typeparam name="TResult">A query result type</typeparam> /// <param name="query">An instance of a query with data</param> /// <returns>A collection of query results</returns> public IEnumerable <TResult> Execute <TQuery, TResult>(TQuery query) where TQuery : IQuery { var queryHandler = _queryHandlerFactory.Create <TQuery>(); try { return(queryHandler.Execute <TResult>(query)); } finally { _queryHandlerFactory.Release(queryHandler); } }
public QueryResult <TResult> Execute <TQuery, TResult>(TQuery query) where TQuery : IQuery <TResult> { var commandHandler = _queryHandlerFactory.Create <TQuery, TResult>(); return(commandHandler.Handle(query)); }
private IQueryHandler GetHandler <TResult>(IQuery <TResult> query) { var queryType = query.GetType(); var handlerType = _registry.GetHandler(queryType); var handler = _factory.Create(handlerType); return(handler); }
public MegaWorkContext(IQueryHandlerFactory queryHandlerFactory) { _queryHandlerFactory = queryHandlerFactory; //Здесь можно прикрутить асинхронщину и всё такое _users = new Lazy <UsersDto>(() => { var defaultQueryHandler = _queryHandlerFactory.Create <AllUsersDefaultQuery, UsersDto>(); return(defaultQueryHandler.Execute(new AllUsersDefaultQuery())); }); _roles = new Lazy <RolesDto>(() => { var defaultQueryHandler = _queryHandlerFactory.Create <AllRolesDefaultQuery, RolesDto>(); return(defaultQueryHandler.Execute(new AllRolesDefaultQuery())); }); }
public TResult Handle <TQuery, TResult>(TQuery query) where TQuery : IQuery <TResult> where TResult : IResponse { var handler = _factory.Create <TQuery, TResult>(); var result = handler.Handle(query); return(result); }
public Task <TQueryResult> Dispatch <TQuery, TQueryResult>(TQuery query) where TQuery : class where TQueryResult : class { var queryHandler = queryHandlerFactory.Create <TQuery, TQueryResult>(query); try { return(queryHandler.Handle(query)); } finally { queryHandlerFactory.Destroy(queryHandler); } }
private (Type handlerType, IQueryHandler handler) ResolveHandler(Type queryType) { _logger.DebugFormat("Looking up handler type in handler registry..."); var handlerType = _handlerRegistry.Get(queryType); if (handlerType == null) { throw new MissingHandlerException($"No handler registered for query: {queryType.FullName}"); } _logger.DebugFormat("Found handler type for {QueryType} in handler registry: {HandlerType}", queryType.Name, handlerType.Name); _logger.Debug("Resolving handler instance..."); var handler = _handlerFactory.Create(handlerType); if (handler == null) { throw new MissingHandlerException($"Handler could not be created for type: {handlerType.FullName}"); } return(handlerType, handler); }
private TResult ExecuteQuery <TQuery, TResult>(TQuery query, IExecutionContext executionContext) where TQuery : IQuery <TResult> { if (query == null) { return(default(TResult)); } var cx = CreateExecutionContext(executionContext); var handler = _queryHandlerFactory.Create <TQuery, TResult>(); if (handler == null) { throw new MissingHandlerMappingException(typeof(TQuery)); } _modelValidationService.Validate(query); _executePermissionValidationService.Validate(query, handler, cx); var result = handler.Execute(query, cx); return(result); }
private Tuple <Type, dynamic> ResolveHandler(Type requestType) { _logger.DebugFormat("Looking up handler type in handler registry..."); var handlerType = _handlerRegistry.Get(requestType); if (handlerType == null) { throw new MissingHandlerException($"No handler registered for query: {requestType.FullName}"); } _logger.DebugFormat("Found handler type for {RequestType} in handler registry: {HandlerType}", requestType.Name, handlerType.Name); _logger.Debug("Resolving handler instance..."); var handler = _handlerFactory.Create <dynamic>(handlerType); if (handler == null) { throw new MissingHandlerException($"Handler could not be created for type: {handlerType.FullName}"); } _logger.Debug("Resolved handler instance"); return(new Tuple <Type, dynamic>(handlerType, handler)); }
public Task <TResult> Handle <TQuery, TResult>(TQuery query) where TQuery : IQuery where TResult : class { var handler = _queryHandlerFactory.Create <TQuery, TResult>(query); return(handler.Handle(query)); }
public async Task <TResult> ExecuteAsync <TArgs, TResult>(TArgs args) { var handler = _queryHandlerFactory.Create <TArgs, TResult>(); return(await handler.ExecuteAsync(args).ConfigureAwait(false)); }