Ejemplo n.º 1
0
        public static void Run(PersistenceStrategy strategy, IDependencyInjector container)
        {
            container.BindToConstant <ILogger>(new DefaultLogger());

            container.Get <ILogger>().Trace("Bootstrapper", "starting the bootstrapper.");

            SetupBus(container);
            SetupDomainCommandHandlers(container);

            switch (strategy)
            {
            case PersistenceStrategy.InMemory:
                container.Get <ILogger>().Trace("Bootstrapper", "bootstrapping In-Memory strategy");
                SetupInMemoryRepo(container);
                break;

            case PersistenceStrategy.InMemoryEventSourcing:
                container.Get <ILogger>().Trace("Bootstrapper", "bootstrapping In-Memory ES strategy");
                SetupInMemoryEsRepo(container);
                break;

            default:
                container.Get <ILogger>().Trace("Bootstrapper", "bootstrapping RavenDb strategy");
                SetupRavenDbRepo(container);
                break;
            }

            container.Get <ILogger>().Trace("Bootstrapper", "done.");
        }
Ejemplo n.º 2
0
        public void DisplaySomeResults()
        {
            var repo = _container.Get <IEmployeeRepository>();

            var employee1 = repo.Load("12345");

            _container.Get <ILogger>().Debug("Playground",
                                             $"Employee {employee1.Id} - {employee1.Name} salary is ${employee1.Salary}"
                                             );

            var employee2 = repo.Load("54321");

            _container.Get <ILogger>().Debug("Playground",
                                             $"Employee {employee2.Id} - {employee2.Name} salary is ${employee2.Salary}"
                                             );


            if (_strategy != PersistenceStrategy.RavenDb)
            {
                return;
            }

            var es = _container.Get <RavenDbEmployeeEventStore>();

            foreach (var entry in es.TopEventSourceEmployees())
            {
                Console.WriteLine($"Number of events to {entry.EmployeeId} is {entry.NumberOfEvents}");
            }

            foreach (var entry in es.TopSalaries())
            {
                Console.WriteLine($"{entry.EmployeeId} -  {entry.FullName} (${entry.Salary})");
            }
        }
Ejemplo n.º 3
0
        private static void SetupInMemoryRepo(IDependencyInjector container)
        {
            container.Get <ILogger>().Trace("Bootstrapper", "initialzing In-memory repository");

            container.BindToConstant <IEmployeeRepository>(
                container.Get <InMemoryEmployeeRepository>()
                );
        }
Ejemplo n.º 4
0
        private static void SetupBus(IDependencyInjector container)
        {
            container.Get <ILogger>().Trace("Bootstrapper", "initializing In-Memory bus.");

            var bus = container.Get <NaiveInMemoryBus>();

            container.BindToConstant <IBus>(bus);
        }
Ejemplo n.º 5
0
        private static void SetupInMemoryEsRepo(IDependencyInjector container)
        {
            container.Get <ILogger>().Trace("Bootstrapper", "initialzing In-memory event store repo");

            var esrepo = container.Get <InMemoryEmployeeEventSourceRepository>();

            container.BindToConstant <IEmployeeRepository>(esrepo);
            container.BindToConstant(esrepo);
            container.Get <IBus>().RegisterHandler <InMemoryEmployeeEventSourceRepository>();
        }
Ejemplo n.º 6
0
        private static void SetupDomainCommandHandlers(IDependencyInjector container)
        {
            container.Get <ILogger>().Trace("Bootstrapper", "registering command handlers.");

            var bus = container.Get <IBus>();

            bus.RegisterHandler <RegisterEmployeeHandler>();
            bus.RegisterHandler <RaiseEmployeeSalaryHandler>();
            bus.RegisterHandler <UpdateEmployeeHomeAddressHandler>();
        }
Ejemplo n.º 7
0
        private static void SetupRavenDbRepo(IDependencyInjector container)
        {
            container.Get <ILogger>().Trace("Bootstrapper", "initialzing RavenDB repositories");

            container.BindToConstant <IEmployeeRepository>(
                container.Get <RavenDbEmployeeRepository>()
                );


            container.BindToConstant(container.Get <RavenDbEmployeeEventStore>());
            container.Get <IBus>().RegisterHandler <RavenDbEmployeeEventStore>();
        }
Ejemplo n.º 8
0
        private static void ExecuteSampleCommands(IDependencyInjector container)
        {
            var bus = container.Get <IBus>();

            bus.SendCommand(new RegisterEmployeeCommand(
                                "12345",
                                new FullName("John", "Doe"),
                                100m
                                ));

            bus.SendCommand(new RegisterEmployeeCommand(
                                "54321",
                                new FullName("Mary", "Loo"),
                                103m
                                ));

            bus.SendCommand(new UpdateEmployeeHomeAddressCommand(
                                "12345",
                                BrazilianAddress.Factory.New("Nice street", 42, null, "Good Ville", "MyCity", "XX", "91234-123"))
                            );

            bus.SendCommand(new RaiseEmployeeSalaryCommand(
                                id: "12345",
                                amount: 10m));
            bus.SendCommand(new RaiseEmployeeSalaryCommand("12345", 20m));
            bus.SendCommand(new RaiseEmployeeSalaryCommand("12345", 13m));

            bus.SendCommand(new UpdateEmployeeHomeAddressCommand(
                                "12345",
                                BrazilianAddress.Factory.New("Main avenue", 42, null, "Good Ville", "MyCity", "XX", "91234-123"))
                            );

            bus.SendCommand(new RaiseEmployeeSalaryCommand("12345", 21m));
            bus.SendCommand(new RaiseEmployeeSalaryCommand("12345", 14m));
        }
Ejemplo n.º 9
0
        private static void SetupDomainCommandHandlers(IDependencyInjector container)
        {
            var bus = container.Get <IBus>();

            bus.RegisterHandler <RegisterEmployeeHandler>();
            bus.RegisterHandler <RaiseEmployeeSalaryHandler>();
            bus.RegisterHandler <UpdateEmployeeHomeAddressHandler>();

            bus.RegisterHandler <FailedToRegisterEmployeeHandler>();
        }
Ejemplo n.º 10
0
        private void Send <T>(T message) where T : Message
        {
            var constructedType = typeof(IMessageHandler <>)
                                  .MakeGenericType(typeof(T));

            var handlersToNotify = _handlers
                                   .Where(h => constructedType.IsAssignableFrom(h));

            foreach (var handler in handlersToNotify)
            {
                var instance = _injector.Get(handler);
                var method   = constructedType.GetMethod("Handle", new[] { typeof(T) });
                method.Invoke(instance, new object[] { message });
            }
        }