Ejemplo n.º 1
0
        public void Handle_should_resolve_and_not_produce_exception()
        {
            // Arrange
            var handler = iocContainer.GetInstance <FindTodosByCategoryQueryHandler>();
            var query   = new FindTodosByCategoryQuery();

            // Act & Assert
            handler.Handle(query);
        }
Ejemplo n.º 2
0
        public void FindTodosByCategoryQuery_should_find_stuff()
        {
            // Arrange
            var handler = iocContainer.GetInstance <FindTodosByCategoryQueryHandler>();
            var query   = new FindTodosByCategoryQuery();

            query.Category = "Shopping";

            // Act
            var task = handler.Handle(query);

            // Assert
            Assert.That(task.Result.Count(), Is.EqualTo(2));
        }
Ejemplo n.º 3
0
        private async static void WorkWithQueryProcessorAndAsync()
        {
            System.Console.WriteLine("\n-- Best of all worlds --");
            var broker = IoC.ThisContainer.GetInstance <IPubSub>();

            broker.Subscribe(typeof(CreateTodoCommand).Name, o => System.Console.WriteLine("Pub/Sub activation: CreateTodoCommand: {0} ", o));
            broker.Subscribe(typeof(FindTodosByCategoryQuery).Name, o => System.Console.WriteLine("Pub/Sub activation: FindTodosByCategoryQuery: {0} ", o));

            var cmd1 = new CreateTodoCommand {
                Data = new Todo {
                    Caption = "Cowboy: Good", Category = "Cowboy"
                }
            };
            var cmd2 = new CreateTodoCommand {
                Data = new Todo {
                    Caption = "Cowboy: Bad ", Category = "Cowboy"
                }
            };
            var cmd3 = new CreateTodoCommand {
                Data = new Todo {
                    Caption = "Cowboy: Ugly", Category = "Cowboy"
                }
            };

            var cmdProcessor   = new CommandProcessor();
            var queryProcessor = new QueryProcessor();

            // Force wait on all, but you can just as well just "await" them
            Task.WaitAll(
                cmdProcessor.Handle(cmd1),
                cmdProcessor.Handle(cmd2)
                );

            await cmdProcessor.Handle(cmd3).ContinueWith(p =>
            {
                // This code below will be executed truly async
                System.Console.WriteLine("- async: cmd3 handled!, this is the continuation. Cmd3.Output = " + cmd3.Output);
            });

            var query = new FindTodosByCategoryQuery {
                Category = "Cowboy"
            };

            // This code below will be executed truly async
            System.Console.WriteLine("- async: results of a find query -");
            var results = await queryProcessor.Handle(query);

            PrintResults(results);
        }
Ejemplo n.º 4
0
        private static void WorkWithCommandsWithIoc()
        {
            System.Console.WriteLine("\n-- WITH IoC --");

            // Get handlers by manual invocation of IoC
            var createHandler = IoC.ThisContainer.GetInstance <ICommandHandler <CreateTodoCommand> >();
            var qhandler      = IoC.ThisContainer.GetInstance <IQueryHandler <FindTodosByCategoryQuery, Todo[]> >();

            // Line up some commands and a query
            var cmd1 = new CreateTodoCommand {
                Data = new Todo {
                    Caption = "Tidy room", Category = "Etc"
                }
            };
            var cmd2 = new CreateTodoCommand {
                Data = new Todo {
                    Caption = "Do the dishes", Category = "Etc"
                }
            };
            var cmd3 = new CreateTodoCommand {
                Data = new Todo {
                    Caption = "Swipe the floors", Category = "Etc"
                }
            };
            var query1 = new FindTodosByCategoryQuery {
                Category = "Etc"
            };

            // Stand handling
            Task.WaitAll(
                createHandler.Handle(cmd1),
                createHandler.Handle(cmd2),
                createHandler.Handle(cmd3)
                );
            var results = qhandler.Handle(query1).Result;

            PrintResults(results);
        }
Ejemplo n.º 5
0
        private static void WorkWithQueryProcessor()
        {
            System.Console.WriteLine("\n-- Using QueryProcessor --");

            var queryProcessor = new QueryProcessor();
            var cmdProcessor   = new CommandProcessor();

            // Run a query with help of the query processor
            var query = new FindTodosByCategoryQuery {
                Category = "Etc"
            };
            var queryResult = queryProcessor.Handle(query).Result;

            // Run a delete command with the command processor and see how the decorator is used
            var delCommand = new DeleteTodoCommand()
            {
                Id = queryResult[0].Id
            };

            cmdProcessor.Handle(delCommand);

            PrintResults(queryResult);
        }
Ejemplo n.º 6
0
        public void LogDecorator_should_be_used_when_using_handlers()
        {
            // Arrange
            var mockedLogger = new Mock <ILogger>(MockBehavior.Strict);

            mockedLogger.Setup(p =>
                               p.Write(
                                   It.IsAny <LoggerLevel>(),
                                   It.IsAny <string>(),
                                   It.IsAny <object[]>()
                                   ));

            var mockedRepo     = new Mock <CachedRepository <Todo> >(MockBehavior.Loose);
            var domainAssembly = typeof(FindTodosByCategoryQueryHandler).Assembly;
            var query          = new FindTodosByCategoryQuery {
                Category = "Etc"
            };

            iocContainer.RegisterManyForOpenGeneric(typeof(IQueryHandler <,>), domainAssembly);
            iocContainer.RegisterManyForOpenGeneric(typeof(ICommandHandler <>), domainAssembly);

            iocContainer.Register <ILogger>(() => mockedLogger.Object, Lifestyle.Singleton);
            iocContainer.Register <IRepository <Todo> >(() => mockedRepo.Object);
            iocContainer.RegisterDecorator(typeof(IQueryHandler <,>), typeof(LoggingQueryHandlerDecorator <,>));

            // Act
            var qhandler = iocContainer.GetInstance <IQueryHandler <FindTodosByCategoryQuery, Todo[]> >();

            qhandler.Handle(query);

            // Assert
            Assert.That(
                qhandler.GetType().Name,
                Is.EqualTo(typeof(LoggingQueryHandlerDecorator <,>).Name)
                );
            mockedLogger.VerifyAll();
        }
Ejemplo n.º 7
0
        private static void WorkWithCommandsWithoutIoc()
        {
            System.Console.WriteLine("\n-- No IoC --");

            // Line up some commands to execute
            var itemToCreateThenChange = new Todo {
                Caption = "Feed cat", Category = "Etc"
            };
            var cmd1 = new CreateTodoCommand {
                Data = new Todo {
                    Caption = "I SHOULD BE DELETED", Category = "Etc"
                }
            };                                                                                                          // id will become 1
            var cmd2 = new CreateTodoCommand {
                Data = itemToCreateThenChange
            };                                                                  // id will become 2
            var cmd3 = new CreateTodoCommand {
                Data = new Todo {
                    Caption = "Pay bills", Category = "Etc"
                }
            };                                                                                                // id will become 3
            var cmd4 = new CreateTodoCommand {
                Data = new Todo {
                    Caption = "I DO NOT PASS THE FILTER", Category = "Uncategorized"
                }
            };                                                                                                                        // id will become 3
            var updateCommand = new UpdateTodoCommand {
                Data = new Todo {
                    Id = itemToCreateThenChange.Id, Category = itemToCreateThenChange.Category, Caption = "Feed BOTH cats"
                }
            };
            var deleteCommand = new DeleteTodoCommand {
                Id = 1
            };                                                    // delete milk

            // Prepare the handlers
            var createHandler = new CreateTodoCommandHandler(new CachedRepository <Todo>());
            var updateHandler = new UpdateTodoCommandHandler(new CachedRepository <Todo>());
            var deleteHandler = new DeleteTodoCommandHandler(new CachedRepository <Todo>());
            // Example using a decorator. We're instantiating the decorator manually here but the idea is to it with IoC.
            // This decorator outputs some logging to to Console.Out.
            var decorating_deleteHandler = new LoggingCommandHandlerDecorator <DeleteTodoCommand>(deleteHandler, (ILogger) new ConsoleLogger());

            // Create something then queue it for deletion
            createHandler.Handle(cmd1).Wait();
            deleteCommand.Id = cmd1.Output;

            Task.WaitAll(
                createHandler.Handle(cmd2)
                , createHandler.Handle(cmd3)
                , createHandler.Handle(cmd4)
                );

            updateHandler.Handle(updateCommand).Wait();
            decorating_deleteHandler.Handle(deleteCommand).Wait();

            // Example using a queryhandler to deliver results
            var qhandler = new FindTodosByCategoryQueryHandler(new CachedRepository <Todo>());
            var query    = new FindTodosByCategoryQuery {
                Category = "Etc"
            };
            var queryResult = qhandler.Handle(query);

            // Only two items should show up since we deleted one, and one doesn't match the query.
            PrintResults(queryResult.Result);
        }