public When_registering_specific_handlers()
        {
            _testHandleRegistrar = new TestHandleRegistrar();
            _locator             = new TestServiceLocator(_testHandleRegistrar);
            var register = new RouteRegistrar(_locator);

            register.RegisterHandlers(
                typeof(TestAggregateDoSomethingHandler),
                typeof(TestAggregateDoSomethingElseHandler),
                typeof(AbstractTestAggregateDoSomethingElseHandler),
                typeof(TestAggregateDoSomethingHandlerExplicit),
                typeof(TestAggregateDidSomethingHandler),
                typeof(AllHandler));
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMemoryCache();

            //Add Cqrs services
            services.AddSingleton <Router>(new Router());
            services.AddSingleton <ICommandSender>(y => y.GetService <Router>());
            services.AddSingleton <IEventPublisher>(y => y.GetService <Router>());
            services.AddSingleton <IHandlerRegistrar>(y => y.GetService <Router>());
            services.AddSingleton <IEventStore, InMemoryEventStore>();
            services.AddSingleton <ICache, MemoryCache>();
            services.AddScoped <IRepository>(y => new CacheRepository(new Repository(y.GetService <IEventStore>()), y.GetService <IEventStore>(), y.GetService <ICache>()));
            services.AddScoped <ISession, Session>();

            services.AddSingleton <IOrderRepository, OrderRepository>();

            //Scan for commandhandlers and eventhandlers
            services.Scan(scan => scan
                          .FromAssemblies(typeof(OrderCommandHandler).GetTypeInfo().Assembly)
                          .AddClasses(classes => classes.Where(x => {
                var allInterfaces = x.GetInterfaces();
                return
                (allInterfaces.Any(y => y.GetTypeInfo().IsGenericType&& y.GetTypeInfo().GetGenericTypeDefinition() == typeof(IHandler <>)) ||
                 allInterfaces.Any(y => y.GetTypeInfo().IsGenericType&& y.GetTypeInfo().GetGenericTypeDefinition() == typeof(ICancellableHandler <>)));
            }))
                          .AsSelf()
                          .WithTransientLifetime()
                          );

            // Add framework services.
            services.AddMvc(options => options.EnableEndpointRouting = false);


            //Register routes
            var serviceProvider = services.BuildServiceProvider();
            var registrar       = new RouteRegistrar(new Provider(serviceProvider));

            registrar.RegisterHandlers(typeof(OrderCommandHandler));

            return(serviceProvider);
        }
Example #3
0
        private static void Main()
        {
            Console.WriteLine("CQRSlite Banking Account sample");

            // Infrastructure setup
            _database   = new InMemoryDatabase();
            _router     = new Router();
            _eventStore = new InMemoryEventStore(_router);

            var locator   = new ServiceLocator(_router, _eventStore, _database);
            var registrar = new RouteRegistrar(locator);

            registrar.RegisterHandlers(
                typeof(AccountCommandsHandler),
                typeof(BankAccountsListView)
                );

            // start using it
            Console.WriteLine("Create bank accounts");
            CreateAccountRequest("Frank");
            CreateAccountRequest("Peter");

            PayInRequest("Frank", 15);
            TransferRequest("Frank", "Peter", 10);
            PayOutRequest("Peter", 5);

            Console.WriteLine("First read model: List of available bank accounts");
            var list = _database.GetAccounts();

            foreach (var account in list)
            {
                var balance = _database.GetAccountBalanceDto(account.Id);
                Console.WriteLine($"Account {account.Name}: {balance.Balance}");
            }

            Console.WriteLine("Sample done.");
            Console.ReadLine();
        }