Esempio n. 1
0
        public async Task DispatchAsync <T>(T command, CancellationToken cancellationToken = default) where T : ICommand
        {
            try
            {
                // Pre

                foreach (var preHandler in _scope.Resolve <IEnumerable <IPreHandleCommand> >())
                {
                    await preHandler.PreHandleAsync(command, cancellationToken);
                }

                foreach (dynamic preHandler in (IEnumerable <dynamic>)_scope.Resolve(typeof(IEnumerable <>).MakeGenericType(typeof(IPreHandleCommand <>).MakeGenericType(command.GetType()))))
                {
                    await preHandler.PreHandleAsync((dynamic)command, cancellationToken);
                }

                // Inner

                await _next.DispatchAsync(command, cancellationToken);

                // Post

                foreach (dynamic postHandler in (IEnumerable <dynamic>)_scope.Resolve(typeof(IEnumerable <>).MakeGenericType(typeof(IPostHandleCommand <>).MakeGenericType(command.GetType()))))
                {
                    await postHandler.PostHandleAsync((dynamic)command, cancellationToken);
                }

                foreach (var postHandler in _scope.Resolve <IEnumerable <IPostHandleCommand> >())
                {
                    await postHandler.PostHandleAsync(command, cancellationToken);
                }
            }
            catch (Exception exception)
            {
                // Error

                foreach (var errorHandler in _scope.Resolve <IEnumerable <IHandleCommandFailure> >())
                {
                    await errorHandler.HandleFailureAsync(command, exception, cancellationToken);
                }

                foreach (dynamic errorHandler in (IEnumerable <dynamic>)_scope.Resolve(typeof(IEnumerable <>).MakeGenericType(typeof(IHandleCommandFailure <>).MakeGenericType(command.GetType()))))
                {
                    await errorHandler.HandleFailureAsync((dynamic)command, exception, cancellationToken);
                }

                throw;
            }
        }
        public async Task DispatchAsync <T>(T command, CancellationToken cancellationToken = default) where T : ICommand
        {
            var stopWatch = new System.Diagnostics.Stopwatch();

            stopWatch.Start();

            try
            {
                await _next.DispatchAsync(command, cancellationToken);

                stopWatch.Stop();
                _log.Verbose("{command} processed in {duration} ms", command.GetType().Name, stopWatch.ElapsedMilliseconds);
            }
            catch
            {
                stopWatch.Stop();
                _log.Verbose("{command} failed in {duration} ms", command.GetType().Name, stopWatch.ElapsedMilliseconds);
                throw;
            }
        }
 public async Task ReceiveAsync <TCommand>(TCommand command, CancellationToken cancellationToken = default) where TCommand : ICommand
 {
     await _commandDispatcher.DispatchAsync(command, cancellationToken);
 }
 public async Task HandleAsync(WaterPoisoningTried e)
 {
     log.Info($"Someone tries to poison the {e.RiverName} river");
     await _commandDispatcher.DispatchAsync(new TryPerformFeat { Id = Guid.NewGuid(), Description = $"Protect the {e.RiverName} river!", SuperheroId = Guid.NewGuid() /*Should be taken from repository*/, DisasterId = e.Id });
 }
Esempio n. 5
0
 public async Task HandleAsync(SuperheroAdded e)
 {
     var appropriateSuperVillainId = Guid.NewGuid();
     await _commandDispatcher.DispatchAsync(new LaughEvil { SuperVillainId = appropriateSuperVillainId, PunchLine = $"Oh, pity! { e.Name } is new superhero!" });
 }