public async Task Execute(CreateUserCommand cmd, ICqrsContext ctx, CancellationToken cancellationToken = new CancellationToken()) { await Task.Delay(10000, cancellationToken); Console.WriteLine($"Отправили пользователю {cmd.Login} смс о регистрации"); await Task.CompletedTask; }
public async Task Execute(CreateUserCommand cmd, ICqrsContext ctx, CancellationToken cancellationToken = new CancellationToken()) { await Task.Delay(20000, cancellationToken); Console.WriteLine($"Ура, у нас появился новый пользователь, вот он - {cmd.Login}"); await Task.CompletedTask; }
/// <summary> /// Initializes a new instance of the <see cref="CommandHandler<TCommand>"/> class. /// </summary> /// <param name="context">The context.</param> public CommandHandler(ICqrsContext context) { if (context == null) { throw new ArgumentNullException("context"); } Context = context; }
protected virtual async Task Proceed <TCommand>(TCommand cmd, ICqrsContext ctx, CancellationToken cancellationToken) where TCommand : class, ICommand { var handler = GetHandler <TCommand>(cmd); if (handler == null) { throw new NotImplementedException($"Не определен обработчик для команды {cmd.GetType()}"); } await handler.Execute(cmd, ctx, cancellationToken); }
public async Task <TResult> ExecuteQuery <TQuery, TResult>(TQuery query, ICqrsContext ctx, CancellationToken cancellationToken = new CancellationToken()) where TQuery : class, IQuery <TResult> { if (query == null) { throw new ArgumentNullException(nameof(query), "Запрос не передан"); } var result = await Proceed <TQuery, TResult>(query, ctx, cancellationToken); return(result); }
public async Task ExecuteCommand <TCommand>(TCommand cmd, ICqrsContext ctx, CancellationToken cancellationToken = new CancellationToken()) where TCommand : class, ICommand { if (cmd == null) { throw new ArgumentNullException(nameof(cmd), "Команда не передана"); } await Before <TCommand>(cmd, ctx, cancellationToken); await Proceed <TCommand>(cmd, ctx, cancellationToken); await After <TCommand>(cmd, ctx, cancellationToken); }
protected virtual async Task Before <TCommand>(TCommand cmd, ICqrsContext ctx, CancellationToken cancellationToken) where TCommand : class, ICommand { var handlers = GetBeforeHandlers(cmd); if (handlers != null) { foreach (var handler in handlers) { if (handler == null) { continue; } await handler.Execute(cmd, ctx, cancellationToken); } } }
protected virtual async Task After <TCommand>(TCommand cmd, ICqrsContext ctx, CancellationToken cancellationToken) where TCommand : class, ICommand { var handlers = GetAfterHandlers(cmd); if (handlers != null) { var tasks = new List <Task>(); foreach (var handler in handlers) { if (handler == null) { continue; } tasks.Add(handler.Execute(cmd, ctx, cancellationToken)); } Task.WaitAll(tasks.ToArray()); } await Task.CompletedTask; }
//protected abstract IEnumerable<IAfterQueryHandler<TQuery, TResult>> GetAfterHandlers<TQuery, TResult>(TQuery query) where TQuery : class, IQuery<TResult>; protected virtual async Task <TResult> Proceed <TQuery, TResult>(TQuery query, ICqrsContext ctx, CancellationToken cancellationToken) where TQuery : class, IQuery <TResult> { var handler = GetHandler <TQuery, TResult>(query); if (handler == null) { throw new NotImplementedException($"Не определен обработчик для запроса {query.GetType()}"); } return(await handler.Execute(query, ctx, cancellationToken)); }
/// <summary> /// Initializes a new instance of the <see cref="CommandHandler<TCommand, TAggregate>"/> class. /// </summary> /// <param name="context">The context.</param> public CommandHandler(ICqrsContext context) : base(context) { }
public async Task <UserDto> Execute(GetUserQuery query, ICqrsContext ctx, CancellationToken cancellationToken = new CancellationToken()) { return(await Task.FromResult(new UserDto { Id = query.Id, Login = "******", FirstName = "Иван", MiddleName = "Иванович", LastName = "Иванов" })); }
public async Task Execute(CreateUserCommand cmd, ICqrsContext ctx, CancellationToken cancellationToken = new CancellationToken()) { cmd.Id = 1; Console.WriteLine($"Я создал пользователя: {cmd.Login}"); await Task.CompletedTask; }