コード例 #1
0
        public async Task ShouldInterceptCommandWithMultipleDependencies()
        {
            var container = new ServiceContainer();

            container.RegisterCommandHandlers();
            container.Register <IFoo, Foo>();
            container.Register <IBar, Bar>();

            bool invoked   = false;
            IFoo passedFoo = null;
            IBar passedBar = null;

            container.RegisterCommandInterceptor <SampleCommand, (IBar bar, IFoo foo)>(async(command, handler, dependencies, token) =>
            {
                passedFoo = dependencies.foo;
                passedBar = dependencies.bar;
                invoked   = true;
                await handler.HandleAsync(command, token);
            });

            var command = new SampleCommand();

            using (var scope = container.BeginScope())
            {
                await container.GetInstance <ICommandHandler <SampleCommand> >().HandleAsync(command);

                invoked.Should().BeTrue();
                command.WasHandled.Should().BeTrue();
                passedFoo.Should().BeOfType <Foo>();
                passedBar.Should().BeOfType <Bar>();
            }
        }
コード例 #2
0
        public async Task ShouldInterceptCommandHandlerWithDependency()
        {
            var container = new ServiceContainer();

            container.RegisterCommandHandlers();
            container.Register <IFoo, Foo>();
            bool invoked   = false;
            IFoo passedFoo = null;

            container.RegisterCommandInterceptor <SampleCommand, IFoo>(async(command, handler, foo, token) =>
            {
                invoked   = true;
                passedFoo = foo;
                await handler.HandleAsync(command, token);
            }
                                                                       );

            var command = new SampleCommand();

            using (var scope = container.BeginScope())
            {
                await container.GetInstance <ICommandHandler <SampleCommand> >().HandleAsync(command);

                invoked.Should().BeTrue();
                command.WasHandled.Should().BeTrue();
                passedFoo.Should().BeOfType <Foo>();
            }
        }
コード例 #3
0
        public async Task ShouldExecuteCommandHandler()
        {
            var container = new ServiceContainer();

            container.RegisterCommandHandlers();
            using (var scope = container.BeginScope())
            {
                var commandExecutor = scope.GetInstance <ICommandExecutor>();
                var command         = new SampleCommand();
                await commandExecutor.ExecuteAsync(command);

                command.WasHandled.Should().BeTrue();
            }
        }