Пример #1
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());
        }
Пример #2
0
 /// <summary>
 /// Creates new instance for rebuilding events from <paramref name="store"/> using <paramref name="deserializer"/>
 /// for loading event instances.
 /// </summary>
 /// <param name="store">The store containing already published events.</param>
 /// <param name="deserializer">The deserializer from loading event instances.</param>
 public Rebuilder(IEventRebuilderStore store, IDeserializer deserializer)
 {
     Ensure.NotNull(store, "store");
     Ensure.NotNull(deserializer, "deserializer");
     this.eventDispatcher = new PersistentEventDispatcher(new EmptyEventStore());
     this.store           = store;
     this.deserializer    = deserializer;
 }
Пример #3
0
        private void Domain()
        {
            Converts.Repository
            .AddJsonEnumSearchHandler()
            .AddJsonPrimitivesSearchHandler()
            .AddJsonObjectSearchHandler()
            .AddJsonKey()
            .AddJsonTimeSpan()
            .Add(new ColorConverter());

            EventStore      = new EntityEventStore(Factory.Default <EventSourcingContext>());
            eventDispatcher = new PersistentEventDispatcher(new EmptyEventStore());
            eventDispatcher.DispatcherExceptionHandlers.Add(this);
            eventDispatcher.EventExceptionHandlers.Add(this);

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

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

            EventFormatter = new CompositeEventFormatter(typeProvider, compositeStorageFactory);

            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()
                );

            PriceFactory = new PriceFactory("CZK");
            DomainFacade = new DefaultDomainFacade(
                OutcomeRepository,
                CategoryRepository,
                PriceFactory
                );
        }
Пример #4
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();
        }
Пример #5
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);
        }