Exemplo n.º 1
0
        /// <inheritdoc />
        public async Task <TResult> ProcessAsync <TResult>(ICommand <TResult> command, CancellationToken cancellationToken)
        {
            Ensure.Arg.NotNull(command, nameof(command));

            Type commandType = command.GetType();

            var pipeline =
                (ICommandPipeline <TResult>)CommandPipelineFactory.CreateCommandPipeline(commandType, _activator);

            CommandDescriptor commandDescriptor = _commandDescriptorFactory.CreateDescriptor(commandType);
            ICommandContext   commandContext    = pipeline.CreateCommandContext(commandDescriptor, command);

            if (_commandContextAccessor != null)
            {
                _commandContextAccessor.CommandContext = commandContext;
            }

            try
            {
                return(await pipeline.InvokeAsync(commandContext, cancellationToken)
                       .ConfigureAwait(false));
            }
            finally
            {
                if (_commandContextAccessor != null)
                {
                    _commandContextAccessor.CommandContext = null;
                }
            }
        }
Exemplo n.º 2
0
        public CommandProcessorTests()
        {
            _commandHandler   = Substitute.For <ICommandHandler <TestCommand, TestResult> >();
            _commandBehaviors = Enumerable.Empty <ICommandPipelineBehavior <TestCommand, TestResult> >();

            _commandDescriptorFactory = Substitute.For <ICommandDescriptorFactory>();
            _commandDescriptorFactory.CreateDescriptor(Arg.Is(typeof(TestCommand)))
            .Returns(
                new CommandDescriptor(typeof(TestCommand), new Dictionary <string, object>()));

            _commandContextAccessor = Substitute.For <ICommandContextAccessor>();

            _serviceProvider = Substitute.For <IServiceProvider>();
            _serviceProvider.GetService(Arg.Is(typeof(ICommandHandler <TestCommand, TestResult>)))
            .Returns(_ => _commandHandler);

            _serviceProvider.GetService(Arg.Is(typeof(IEnumerable <ICommandPipelineBehavior <TestCommand, TestResult> >)))
            .Returns(_ => _commandBehaviors);

            _processor = new CommandProcessor(new ServiceProviderActivator(_serviceProvider), _commandDescriptorFactory, _commandContextAccessor);
        }