コード例 #1
0
        public void SaveAggregateRootAndSubscribeEventTest()
        {
            var serviceCollection = new ServiceCollection();
            var names             = new List <string>();

            serviceCollection.AddSingleton(names);
            var executionContext = new ServiceProviderMessageHandlerExecutionContext(serviceCollection);

            using (var eventPublisher = new RabbitEventBus(connectionFactory, messageSerializer, executionContext, this.GetType().Name))
                using (var eventStore = new PostgreSqlEventStore(new AdoNetEventStoreConfiguration(PostgreSQLFixture.ConnectionString, new GuidKeyGenerator()), eventStoreSerializer))
                    using (var repository = new EventSourcingDomainRepository(eventStore, eventPublisher, snapshotProvider))
                    {
                        int eventsReceived = 0;
                        var ackCnt         = 0;
                        var subscriber     = (IEventSubscriber)eventPublisher;
                        subscriber.MessageReceived     += (a, b) => eventsReceived++;
                        subscriber.MessageAcknowledged += (x, y) => ackCnt++;
                        subscriber.Subscribe <NameChangedEvent, NameChangedEventHandler>();
                        subscriber.Subscribe <TitleChangedEvent, TitleChangedEventHandler>();

                        var aggregateRootId = Guid.NewGuid();
                        var employee        = new Employee(aggregateRootId);
                        employee.ChangeName("daxnet");
                        employee.ChangeTitle("developer");
                        repository.Save <Guid, Employee>(employee);
                        while (ackCnt < 2)
                        {
                            ;
                        }
                        Assert.Equal(2, eventsReceived);
                    }
        }
コード例 #2
0
        public void SaveAggregateRootAndSubscribeEventTest()
        {
            using (var eventPublisher = new EventBus(connectionFactory, serializer, this.GetType().Name))
                using (var eventStore = new PostgreSqlEventStore(new AdoNetEventStoreConfiguration(PostgreSQLFixture.ConnectionString, new GuidKeyGenerator()), serializer))
                    using (var repository = new EventSourcingDomainRepository(eventStore, eventPublisher))
                    {
                        int eventsReceived = 0;
                        var ackCnt         = 0;
                        var subscriber     = (IEventSubscriber)eventPublisher;
                        subscriber.MessageReceived     += (a, b) => eventsReceived++;
                        subscriber.MessageAcknowledged += (x, y) => ackCnt++;
                        subscriber.Subscribe();

                        var aggregateRootId = Guid.NewGuid();
                        var employee        = new Employee {
                            Id = aggregateRootId
                        };
                        employee.ChangeName("daxnet");
                        employee.ChangeTitle("developer");
                        repository.Save <Guid, Employee>(employee);
                        while (ackCnt < 2)
                        {
                            ;
                        }
                        Assert.Equal(2, eventsReceived);
                    }
        }
コード例 #3
0
        public void SimpleCQRSScenarioTest1()
        {
            var changedName      = string.Empty;
            var eventStore       = new DictionaryEventStore();
            var eventBus         = new EventBus();
            var snapshotProvider = new SuppressedSnapshotProvider();

            eventBus.MessageReceived += (s, e) =>
            {
                if (e.Message is NameChangedEvent)
                {
                    changedName = (e.Message as NameChangedEvent).Name;
                }
            };
            eventBus.Subscribe();
            var domainRepository = new EventSourcingDomainRepository(eventStore, eventBus, snapshotProvider);

            var id    = Guid.NewGuid();
            var model = new Employee(id);

            model.ChangeName("daxnet");
            domainRepository.Save <Guid, Employee>(model);

            Assert.Equal("daxnet", changedName);
        }
コード例 #4
0
        public void SimpleCQRSScenarioTest1()
        {
            var serviceCollection = new ServiceCollection();
            var messageHandlerExecutionContext = new ServiceProviderMessageHandlerExecutionContext(serviceCollection);

            var changedName      = string.Empty;
            var eventStore       = new DictionaryEventStore();
            var eventBus         = new SimpleEventBus(new MessageJsonSerializer(), messageHandlerExecutionContext);
            var snapshotProvider = new SuppressedSnapshotProvider();

            eventBus.MessageReceived += (s, e) =>
            {
                if (e.Message is NameChangedEvent)
                {
                    changedName = (e.Message as NameChangedEvent).Name;
                }
            };

            var domainRepository = new EventSourcingDomainRepository(eventStore, eventBus, snapshotProvider);

            var id    = Guid.NewGuid();
            var model = new Employee(id);

            model.ChangeName("daxnet");
            domainRepository.Save <Guid, Employee>(model);

            Assert.Equal("daxnet", changedName);
        }
コード例 #5
0
        public void SaveSnapshotCountTest2()
        {
            var aggregateRootId       = Guid.NewGuid();
            var employee              = new Employee(aggregateRootId);
            var localSnapshotProvider = new InMemorySnapshotProvider();
            var localRepository       = new EventSourcingDomainRepository(eventStore, eventPublisher, localSnapshotProvider);

            for (var i = 0; i < 31; i++)
            {
                employee.ChangeName($"daxnet_{i}");
            }
            localRepository.Save <Guid, Employee>(employee);

            for (var i = 0; i < 31; i++)
            {
                employee.ChangeTitle($"Developer_{i}");
            }
            localRepository.Save <Guid, Employee>(employee);

            Assert.Equal(2, localSnapshotProvider.snapshots.Count);
        }
コード例 #6
0
 public void SaveAggregateRootTest()
 {
     using (var eventPublisher = new EventBus(connectionFactory, messageSerializer, this.GetType().Name))
         using (var eventStore = new PostgreSqlEventStore(new AdoNetEventStoreConfiguration(PostgreSQLFixture.ConnectionString, new GuidKeyGenerator()), eventStoreSerializer))
             using (var repository = new EventSourcingDomainRepository(eventStore, eventPublisher, snapshotProvider))
             {
                 var aggregateRootId = Guid.NewGuid();
                 var employee        = new Employee(aggregateRootId);
                 employee.ChangeName("daxnet");
                 employee.ChangeTitle("developer");
                 Assert.Equal(3, employee.Version);
                 repository.Save <Guid, Employee>(employee);
                 Assert.Equal(3, employee.Version);
             }
 }
コード例 #7
0
        public void LoadFromSnapshotTest2()
        {
            var aggregateRootId       = Guid.NewGuid();
            var employee              = new Employee(aggregateRootId);
            var localSnapshotProvider = new InMemorySnapshotProvider();
            var localRepository       = new EventSourcingDomainRepository(eventStore, eventPublisher, localSnapshotProvider);

            for (var i = 0; i < 31; i++)
            {
                employee.ChangeName($"daxnet_{i}");
            }
            localRepository.Save <Guid, Employee>(employee);

            for (var i = 0; i < 4; i++)
            {
                employee.ChangeTitle($"Software Developer_{i}");
            }
            localRepository.Save <Guid, Employee>(employee);

            var employee2 = localRepository.GetById <Guid, Employee>(aggregateRootId);

            Assert.Equal(employee.Name, employee2.Name);
            Assert.Equal(employee.Title, employee2.Title);
        }
コード例 #8
0
        public void GetByVersionTest1()
        {
            using (var eventPublisher = new EventBus(connectionFactory, messageSerializer, this.GetType().Name))
                using (var eventStore = new PostgreSqlEventStore(new AdoNetEventStoreConfiguration(PostgreSQLFixture.ConnectionString, new GuidKeyGenerator()), eventStoreSerializer))
                    using (var repository = new EventSourcingDomainRepository(eventStore, eventPublisher, snapshotProvider))
                    {
                        var aggregateRootId = Guid.NewGuid();
                        var employee        = new Employee(aggregateRootId);
                        employee.ChangeName("daxnet");
                        employee.ChangeTitle("developer");
                        employee.Register();
                        repository.Save <Guid, Employee>(employee);

                        var employee2 = repository.GetById <Guid, Employee>(aggregateRootId, 2); // Load 2 events as the first one is the aggregate created event.
                        Assert.Equal(employee.Name, employee2.Name);
                        Assert.Null(employee2.Title);
                        Assert.Equal(DateTime.MinValue, employee2.DateRegistered);
                    }
        }
コード例 #9
0
        public void EventSequenceAfterSaveTest()
        {
            using (var eventPublisher = new EventBus(connectionFactory, messageSerializer, this.GetType().Name))
                using (var eventStore = new PostgreSqlEventStore(new AdoNetEventStoreConfiguration(PostgreSQLFixture.ConnectionString, new GuidKeyGenerator()), eventStoreSerializer))
                    using (var repository = new EventSourcingDomainRepository(eventStore, eventPublisher, snapshotProvider))
                    {
                        var aggregateRootId = Guid.NewGuid();
                        var employee        = new Employee(aggregateRootId);
                        employee.ChangeName("daxnet");
                        employee.ChangeTitle("developer");
                        repository.Save <Guid, Employee>(employee);

                        var events = eventStore.Load <Guid>(typeof(Employee).AssemblyQualifiedName, aggregateRootId).ToList();
                        Assert.Equal(3, events.Count);
                        Assert.Equal(1, (events[0] as IDomainEvent).Sequence);
                        Assert.Equal(2, (events[1] as IDomainEvent).Sequence);
                        Assert.Equal(3, (events[2] as IDomainEvent).Sequence);
                    }
        }
コード例 #10
0
        public void LoadAggregateRootTest()
        {
            using (var eventPublisher = new EventBus(connectionFactory, serializer, this.GetType().Name))
                using (var eventStore = new PostgreSqlEventStore(new AdoNetEventStoreConfiguration(PostgreSQLFixture.ConnectionString, new GuidKeyGenerator()), serializer))
                    using (var repository = new EventSourcingDomainRepository(eventStore, eventPublisher))
                    {
                        var aggregateRootId = Guid.NewGuid();
                        var employee        = new Employee {
                            Id = aggregateRootId
                        };
                        employee.ChangeName("daxnet");
                        employee.ChangeTitle("developer");
                        repository.Save <Guid, Employee>(employee);

                        var employee2 = repository.GetById <Guid, Employee>(aggregateRootId);
                        Assert.Equal("daxnet", employee2.Name);
                        Assert.Equal("Sr. developer", employee2.Title);
                        Assert.Equal(2, employee2.Version);
                    }
        }
コード例 #11
0
        public void LoadAggregateRootTest()
        {
            var serviceCollection = new ServiceCollection();
            var executionContext  = new ServiceProviderMessageHandlerExecutionContext(serviceCollection);

            using (var eventPublisher = new RabbitEventBus(connectionFactory, messageSerializer, executionContext, this.GetType().Name))
                using (var eventStore = new PostgreSqlEventStore(new AdoNetEventStoreConfiguration(PostgreSQLFixture.ConnectionString, new GuidKeyGenerator()), eventStoreSerializer))
                    using (var repository = new EventSourcingDomainRepository(eventStore, eventPublisher, snapshotProvider))
                    {
                        var aggregateRootId = Guid.NewGuid();
                        var employee        = new Employee(aggregateRootId);
                        employee.ChangeName("daxnet");
                        employee.ChangeTitle("developer");
                        repository.Save <Guid, Employee>(employee);

                        var employee2 = repository.GetById <Guid, Employee>(aggregateRootId);
                        Assert.Equal("daxnet", employee2.Name);
                        Assert.Equal("Sr. developer", employee2.Title);
                        Assert.Equal(3, employee2.Version);
                    }
        }