public void DistributorAndThreadPool()
        {
            PersistentCommandDispatcherBuilder builder = new PersistentCommandDispatcherBuilder()
                                                         .UseCommandDistributor(new SerialCommandDistributor())
                                                         .UseFormatter(new CompositeCommandFormatter(new ReflectionCompositeTypeProvider(new ReflectionCompositeDelegateFactory()), Factory.Default <JsonCompositeStorage>()))
                                                         .UseSchedulingProvider(new TimerSchedulingProvider(new TimerSchedulingProvider.DateTimeNowProvider()))
                                                         .UseStore(new EmptyCommandStore());

            CommandHandlerService service = new CommandHandlerService();

            PersistentCommandDispatcher dispatcher = builder.Create();

            dispatcher.Handlers
            .Add(new SlowCommandHandler(service))
            .Add(new FastCommandHandler(service));

            dispatcher.HandleAsync(new SlowCommand()).Wait();
            dispatcher.HandleAsync(new FastCommand()).Wait();
            dispatcher.HandleAsync(new FastCommand()).Wait();
            dispatcher.HandleAsync(new SlowCommand()).Wait();
            dispatcher.HandleAsync(new FastCommand()).Wait();

            Thread.Sleep(10000);
            Assert.AreEqual(5, service.Log.Count);
            Assert.AreEqual(CommandType.Slow, service.Log[0]);
            Assert.AreEqual(CommandType.Fast, service.Log[1]);
            Assert.AreEqual(CommandType.Fast, service.Log[2]);
            Assert.AreEqual(CommandType.Slow, service.Log[3]);
            Assert.AreEqual(CommandType.Fast, service.Log[4]);
        }
Пример #2
0
        public void SaveAndLoadWithEntityRepository()
        {
            Converts.Repository
            .AddJsonKey()
            .AddJsonEnumSearchHandler()
            .AddJsonPrimitivesSearchHandler()
            .AddJsonObjectSearchHandler();

            ICompositeTypeProvider       compositeTypeProvider = new ReflectionCompositeTypeProvider(new ReflectionCompositeDelegateFactory());
            IFactory <ICompositeStorage> storageFactory        = new GetterFactory <ICompositeStorage>(() => new JsonCompositeStorage());

            EventSourcingContext context    = new EventSourcingContext(@"Data Source=.\sqlexpress; Initial Catalog=EventStore;Integrated Security=SSPI");
            EntityEventStore     eventStore = new EntityEventStore(context);

            PersistentEventDispatcher eventDispatcher = new PersistentEventDispatcher(eventStore);

            AggregateRootRepository <Order> repository = new AggregateRootRepository <Order>(
                eventStore,
                new CompositeEventFormatter(compositeTypeProvider, storageFactory),
                new ReflectionAggregateRootFactory <Order>(),
                eventDispatcher,
                new NoSnapshotProvider(),
                new EmptySnapshotStore()
                );

            PersistentCommandDispatcher commandDispatcher = new PersistentCommandDispatcher(
                new SerialCommandDistributor(),
                new EntityCommandStore(context),
                new CompositeCommandFormatter(compositeTypeProvider, storageFactory)
                );

            CreateOrderHandler  createHandler  = new CreateOrderHandler(repository);
            AddOrderItemHandler addItemHandler = new AddOrderItemHandler(repository);

            commandDispatcher.Handlers
            .Add <CreateOrder>(createHandler)
            .Add <AddOrderItem>(addItemHandler);

            CreateOrder create = new CreateOrder();

            commandDispatcher.HandleAsync(create);

            eventDispatcher.Handlers.Await <OrderPlaced>().Wait();

            IEnumerable <EventModel> serializedEvents = eventStore.Get(create.OrderKey).ToList();

            Assert.AreEqual(1, serializedEvents.Count());

            AddOrderItem addItem = new AddOrderItem(create.OrderKey, GuidKey.Create(Guid.NewGuid(), "Product"), 5);

            commandDispatcher.HandleAsync(Envelope.Create(addItem).AddDelay(TimeSpan.FromMinutes(1)));

            Task <OrderTotalRecalculated> task = eventDispatcher.Handlers.Await <OrderTotalRecalculated>();

            task.Wait();
            Console.WriteLine(task.Result);

            serializedEvents = eventStore.Get(create.OrderKey).ToList();
            Assert.AreEqual(4, serializedEvents.Count());
        }
Пример #3
0
        private void Domain()
        {
            Converts.Repository
            .AddStringTo <int>(Int32.TryParse)
            .AddStringTo <bool>(Boolean.TryParse)
            .AddEnumSearchHandler(false)
            .AddJsonEnumSearchHandler()
            .AddJsonPrimitivesSearchHandler()
            .AddJsonObjectSearchHandler()
            .AddJsonKey()
            .AddJsonTimeSpan()
            .Add(new ColorConverter())
            .AddToStringSearchHandler();

            EventStore      = new EntityEventStore(ServiceProvider.EventSourcingContextFactory);
            eventDispatcher = new PersistentEventDispatcher(new EmptyEventStore());
            eventDispatcher.DispatcherExceptionHandlers.Add(this);
            eventDispatcher.EventExceptionHandlers.Add(this);

            IFactory <ICompositeStorage> compositeStorageFactory = Factory.Default <JsonCompositeStorage>();

            typeProvider = new ReflectionCompositeTypeProvider(
                new ReflectionCompositeDelegateFactory(),
                BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public
                );

            CommandFormatter = new CompositeCommandFormatter(typeProvider, compositeStorageFactory);
            EventFormatter   = new CompositeEventFormatter(typeProvider, compositeStorageFactory, new List <ICompositeFormatterExtender>()
            {
                new UserKeyEventExtender()
            });

            commandDispatcher = new PersistentCommandDispatcher(new SerialCommandDistributor(), new EmptyCommandStore(), CommandFormatter);

            OutcomeRepository = new AggregateRootRepository <Outcome>(
                EventStore,
                EventFormatter,
                new ReflectionAggregateRootFactory <Outcome>(),
                eventDispatcher,
                new NoSnapshotProvider(),
                new EmptySnapshotStore()
                );

            CategoryRepository = new AggregateRootRepository <Category>(
                EventStore,
                EventFormatter,
                new ReflectionAggregateRootFactory <Category>(),
                eventDispatcher,
                new NoSnapshotProvider(),
                new EmptySnapshotStore()
                );

            CurrencyListRepository = new AggregateRootRepository <CurrencyList>(
                EventStore,
                EventFormatter,
                new ReflectionAggregateRootFactory <CurrencyList>(),
                eventDispatcher,
                new NoSnapshotProvider(),
                new EmptySnapshotStore()
                );

            Money.BootstrapTask bootstrapTask = new Money.BootstrapTask(
                commandDispatcher.Handlers,
                Factory.Instance(OutcomeRepository),
                Factory.Instance(CategoryRepository),
                Factory.Instance(CurrencyListRepository)
                );
            bootstrapTask.Initialize();
        }
Пример #4
0
        private void Domain(IServiceProvider provider)
        {
            Converts.Repository
            .AddStringTo <int>(Int32.TryParse)
            .AddStringTo <bool>(Boolean.TryParse)
            .AddEnumSearchHandler(false)
            .AddJsonEnumSearchHandler()
            .AddJsonPrimitivesSearchHandler()
            .AddJsonObjectSearchHandler()
            .AddJsonKey()
            .AddJsonTimeSpan()
            .Add(new ColorConverter())
            .AddToStringSearchHandler();

            var eventSourcingContextFactory = provider.GetRequiredService <IFactory <EventSourcingContext> >();

            eventStore      = new EntityEventStore(eventSourcingContextFactory);
            eventDispatcher = new PersistentEventDispatcher(new EmptyEventStore());
            eventDispatcher.DispatcherExceptionHandlers.Add(exceptionHandlerBuilder);
            eventDispatcher.EventExceptionHandlers.Add(exceptionHandlerBuilder);

            IFactory <ICompositeStorage> compositeStorageFactory = Factory.Default <JsonCompositeStorage>();

            typeProvider = new ReflectionCompositeTypeProvider(
                new ReflectionCompositeDelegateFactory(),
                BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public
                );

            commandFormatter = new CompositeCommandFormatter(typeProvider, compositeStorageFactory);
            eventFormatter   = new CompositeEventFormatter(typeProvider, compositeStorageFactory, new List <ICompositeFormatterExtender>()
            {
                new UserKeyEventExtender()
            });
            queryFormatter     = new CompositeListFormatter(typeProvider, compositeStorageFactory, logFactory);
            exceptionFormatter = new CompositeExceptionFormatter(typeProvider, compositeStorageFactory);

            commandDispatcher = new PersistentCommandDispatcher(new SerialCommandDistributor(), new EmptyCommandStore(), commandFormatter);
            commandDispatcher.DispatcherExceptionHandlers.Add(exceptionHandlerBuilder);
            commandDispatcher.CommandExceptionHandlers.Add(exceptionHandlerBuilder);

            queryDispatcher = new DefaultQueryDispatcher();

            var snapshotProvider = new NoSnapshotProvider();
            var snapshotStore    = new EmptySnapshotStore();

            var outcomeRepository = new AggregateRootRepository <Outcome>(
                eventStore,
                eventFormatter,
                new ReflectionAggregateRootFactory <Outcome>(),
                eventDispatcher,
                snapshotProvider,
                snapshotStore
                );

            var expenseTemplateRepository = new AggregateRootRepository <ExpenseTemplate>(
                eventStore,
                eventFormatter,
                new ReflectionAggregateRootFactory <ExpenseTemplate>(),
                eventDispatcher,
                snapshotProvider,
                snapshotStore
                );

            var incomeRepository = new AggregateRootRepository <Income>(
                eventStore,
                eventFormatter,
                new ReflectionAggregateRootFactory <Income>(),
                eventDispatcher,
                snapshotProvider,
                snapshotStore
                );

            var categoryRepository = new AggregateRootRepository <Category>(
                eventStore,
                eventFormatter,
                new ReflectionAggregateRootFactory <Category>(),
                eventDispatcher,
                snapshotProvider,
                snapshotStore
                );

            var currencyListRepository = new AggregateRootRepository <CurrencyList>(
                eventStore,
                eventFormatter,
                new ReflectionAggregateRootFactory <CurrencyList>(),
                eventDispatcher,
                snapshotProvider,
                snapshotStore
                );

            Money.BootstrapTask bootstrapTask = new Money.BootstrapTask(
                commandDispatcher.Handlers,
                Factory.Instance(outcomeRepository),
                Factory.Instance(expenseTemplateRepository),
                Factory.Instance(incomeRepository),
                Factory.Instance(categoryRepository),
                Factory.Instance(currencyListRepository)
                );

            bootstrapTask.Initialize();

            ServiceProvider serviceProvider = services.BuildServiceProvider();
            UserHandler     userHandler     = new UserHandler(
                serviceProvider.GetRequiredService <UserManager <User> >(),
                serviceProvider.GetRequiredService <IFactory <AccountContext> >(),
                eventDispatcher,
                allowedUserPropertyKeys
                );

            commandDispatcher.Handlers.AddAll(userHandler);
            queryDispatcher.AddAll(userHandler);
        }