Ejemplo n.º 1
0
        public async Task LoadDomainEvents_IdAndStuffIsSetCorreclty(PersistenceLayerProvider layerProvider)
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEvent1>();
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEvent2>();

            var eventRepository = layerProvider.EventRepository;

            var newGuid    = Guid.NewGuid();
            var testEvent1 = new TestEvent1(newGuid);
            var testEvent2 = new TestEvent2(newGuid);
            var events     = new List <IDomainEvent> {
                testEvent1, testEvent2
            };

            var res = await eventRepository.AppendAsync(events, 0);

            res.Check();

            var loadEventsByEntity = await eventRepository.LoadEventsByEntity(newGuid.ToString());

            Assert.AreEqual(2, loadEventsByEntity.Value.Count());
            Assert.AreEqual(1, loadEventsByEntity.Value.ToList()[0].OverallVersion);
            Assert.AreEqual(2, loadEventsByEntity.Value.ToList()[1].OverallVersion);
            Assert.AreEqual(1, loadEventsByEntity.Value.ToList()[0].EntityStreamVersion);
            Assert.AreEqual(2, loadEventsByEntity.Value.ToList()[1].EntityStreamVersion);
            Assert.IsTrue(newGuid.ToString() == loadEventsByEntity.Value.ToList()[0].DomainEvent.EntityId);
        }
Ejemplo n.º 2
0
        public async Task AddAndLoadEvents_OrderIsCorrect(PersistenceLayerProvider layerProvider)
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEvent1>();
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEvent2>();
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEvent3>();

            var eventRepository = layerProvider.EventRepository;

            var newGuid = Guid.NewGuid();
            var events  = new List <IDomainEvent> {
                new TestEvent1(newGuid)
            };
            var events2 = new List <IDomainEvent> {
                new TestEvent2(newGuid), new TestEvent3(newGuid, "TestName")
            };
            var res = await eventRepository.AppendAsync(events, 0);

            var res2 = await eventRepository.AppendAsync(events2, 1);

            res.Check();
            res2.Check();

            var loadEventsByEntity = await eventRepository.LoadEventsByEntity(newGuid.ToString());

            Assert.AreEqual(3, loadEventsByEntity.Value.Count());
            Assert.AreEqual(1, loadEventsByEntity.Value.ToList()[0].EntityStreamVersion);
            Assert.AreEqual(2, loadEventsByEntity.Value.ToList()[1].EntityStreamVersion);
            Assert.AreEqual(3, loadEventsByEntity.Value.ToList()[2].EntityStreamVersion);
        }
Ejemplo n.º 3
0
        public async Task AddEvents_ForeachMethod()
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEventAllOk>();

            var eventRepository = new EventRepositoryMongoDb(EventMongoDb, new VersionCache(EventMongoDb));
            var snapShotRepo    = new Mock <ISnapShotRepository>();

            snapShotRepo.Setup(re => re.LoadSnapShot <TestEntity>(It.IsAny <string>()))
            .ReturnsAsync(SnapShotResult <TestEntity> .Default());

            var eventStore = new EventStore(eventRepository, snapShotRepo.Object, new SnapShotConfig());

            var newGuid = Guid.NewGuid();

            await eventStore.AppendAsync(
                new List <IDomainEvent> {
                new TestEventAllOk(newGuid, "Simon")
            },
                0);

            var result = await eventRepository.LoadEvents();

            Assert.AreEqual(1, result.Value.Count());
            Assert.AreEqual(newGuid.ToString(), result.Value.Single().DomainEvent.EntityId);
            Assert.AreEqual("Simon", ((TestEventAllOk)result.Value.Single().DomainEvent).Name);
        }
Ejemplo n.º 4
0
        public async Task IntegrationWithRepo()
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEventEventStore>();
            var snapShotRepo = new Mock <ISnapShotRepository>();

            snapShotRepo.Setup(re => re.LoadSnapShot <TestEntity>(It.IsAny <string>()))
            .ReturnsAsync(SnapShotResult <TestEntity> .Default());
            var entityId   = Guid.NewGuid();
            var eventStore = new EventStore(
                new EventRepositoryMongoDb(EventMongoDb,
                                           new VersionCache(EventMongoDb)),
                snapShotRepo.Object, new SnapShotConfig());

            await eventStore.AppendAsync(new List <IDomainEvent> {
                new TestEventEventStore(entityId, "Test")
            }, 0);

            var loadAsync = await eventStore.LoadAsync <TestEntity>(entityId.ToString());

            var loadAsync2 = await eventStore.LoadAsync <TestEntity>(entityId.ToString());

            Assert.AreEqual(entityId, loadAsync.Value.Id);
            Assert.AreEqual("Test", loadAsync.Value.Name);

            Assert.AreEqual(entityId, loadAsync2.Value.Id);
            Assert.AreEqual("Test", loadAsync2.Value.Name);
        }
        public static IServiceCollection AddMicrowavePersistenceLayerMongoDb(
            this IServiceCollection services,
            Action <MicrowaveMongoDb> mongoDb = null)
        {
            var action           = mongoDb ?? (c => { });
            var microwaveMongoDb = new MicrowaveMongoDb();

            action.Invoke(microwaveMongoDb);

            services.AddTransient <IStatusRepository, StatusRepositoryMongoDb>();

            services.AddTransient <IVersionRepository, VersionRepositoryMongoDb>();
            services.AddTransient <IReadModelRepository, ReadModelRepositoryMongoDb>();
            services.AddSingleton(microwaveMongoDb);
            services.AddSingleton <IEventLocationCache>(new EventLocationCache());

            services.AddTransient <IEventRepository, EventRepositoryMongoDb>();
            services.AddSingleton <IVersionCache, VersionCache>();
            services.AddTransient <ISnapShotRepository, SnapShotRepositoryMongoDb>();

            foreach (var assembly in GetAllAssemblies())
            {
                BsonMapRegistrationHelpers.AddBsonMapsForMicrowave(assembly);
            }

            return(services);
        }
Ejemplo n.º 6
0
        public async Task AddEvents_AutoProperty_NotNamedEntityId()
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEvent_BsonBug_AutoProperty_NotWithEntityIdName>();

            var eventRepository = new EventRepositoryMongoDb(EventMongoDb, new VersionCache(EventMongoDb));
            var snapShotRepo    = new Mock <ISnapShotRepository>();

            snapShotRepo.Setup(re => re.LoadSnapShot <TestEntity>(It.IsAny <string>()))
            .ReturnsAsync(SnapShotResult <TestEntity> .Default());

            var eventStore = new EventStore(eventRepository, snapShotRepo.Object, new SnapShotConfig());

            await eventStore.AppendAsync(new List <IDomainEvent> {
                new TestEvent_BsonBug_AutoProperty_NotWithEntityIdName("whatever", "Peter")
            },
                                         0);

            var result = await eventRepository.LoadEvents();

            Assert.AreEqual(1, result.Value.Count());
            var domainEvent = result.Value.Single().DomainEvent as TestEvent_BsonBug_AutoProperty_NotWithEntityIdName;

            Assert.AreEqual("whatever", domainEvent.EntityId);
            Assert.AreEqual("Peter", domainEvent.Name);
        }
Ejemplo n.º 7
0
        public async Task SnapshotExactlyOnSnapShotTime_DoesNotReturnNotFoundBug(PersistenceLayerProvider layerProvider)
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <Event1>();
            BsonMapRegistrationHelpers.AddBsonMapFor <Event2>();

            var eventStore = new EventStore(layerProvider.EventRepository, layerProvider.SnapShotRepository, new
                                            SnapShotConfig(new List <ISnapShot> {
                new SnapShot <User>(1)
            }));

            var entityId = Guid.NewGuid();
            await eventStore.AppendAsync(new List <IDomainEvent>
            {
                new Event1(entityId),
                new Event2(entityId, "Peter"),
                new Event2(entityId, "Peterneu")
            }, 0);

            await eventStore.LoadAsync <User>(entityId.ToString());

            var result = await eventStore.LoadAsync <User>(entityId.ToString());

            Assert.AreEqual("Peterneu", result.Value.Name);
            Assert.AreEqual(3, result.Version);
        }
Ejemplo n.º 8
0
        public async Task SnapshotGetSavedAfterThirdEvent(PersistenceLayerProvider layerProvider)
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <Event1>();
            BsonMapRegistrationHelpers.AddBsonMapFor <Event2>();
            BsonMapRegistrationHelpers.AddBsonMapFor <Event3>();

            var snapShotRepository = layerProvider.SnapShotRepository;
            var eventStore         = new EventStore(layerProvider.EventRepository, snapShotRepository
                                                    , new SnapShotConfig(new
                                                                         List <ISnapShot> {
                new SnapShot <User>(3)
            }));

            var entityId = Guid.NewGuid();
            await eventStore.AppendAsync(new List <IDomainEvent>
            {
                new Event1(entityId),
                new Event2(entityId, "Peter")
            }, 0);

            await eventStore.LoadAsync <User>(entityId.ToString());

            var snapShotDboOld = await snapShotRepository.LoadSnapShot <User>(entityId.ToString());

            Assert.IsNull(snapShotDboOld.Value.Id);
            Assert.IsNull(snapShotDboOld.Value.Name);

            await eventStore.AppendAsync(new List <IDomainEvent>
            {
                new Event3(entityId, 14),
                new Event2(entityId, "PeterNeu")
            }, 2);

            var eventstoreResult = await eventStore.LoadAsync <User>(entityId.ToString());

            var user = eventstoreResult;

            Assert.AreEqual(4, eventstoreResult.Version);
            Assert.AreEqual(14, user.Value.Age);
            Assert.AreEqual("PeterNeu", user.Value.Name);
            Assert.AreEqual(entityId.ToString(), user.Value.Id);

            var snapShotDbo = await snapShotRepository.LoadSnapShot <User>(entityId.ToString());

            Assert.AreEqual(4, snapShotDbo.Version);
            Assert.AreEqual(entityId.ToString(), snapShotDbo.Value.Id);
            var userSnapShot = snapShotDbo.Value;

            Assert.AreEqual(14, userSnapShot.Age);
            Assert.AreEqual("PeterNeu", userSnapShot.Name);
            Assert.AreEqual(entityId.ToString(), userSnapShot.Id);
        }
Ejemplo n.º 9
0
        public async Task Entitystream_LoadEventsSince_IdNotDefault()
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEv3>();
            var entityStreamRepository = new EventRepositoryMongoDb(EventMongoDb, new VersionCache(EventMongoDb));

            var entityStreamTestEvent = new TestEv3(Guid.NewGuid());
            await entityStreamRepository.AppendAsync(new[] { entityStreamTestEvent }, 0);

            var eventsSince = await entityStreamRepository.LoadEvents();

            Assert.AreEqual(entityStreamTestEvent.EntityId, eventsSince.Value.Single().DomainEvent.EntityId);
            Assert.AreNotEqual(entityStreamTestEvent.EntityId, Guid.Empty.ToString());
        }
Ejemplo n.º 10
0
        public async Task AddEvents_IdSet(PersistenceLayerProvider layerProvider)
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEvent1>();

            var eventRepository = layerProvider.EventRepository;

            var testEvent1 = new TestEvent1(Guid.NewGuid());
            await eventRepository.AppendAsync(new[] { testEvent1 }, 0);

            var result = await eventRepository.LoadEventsByEntity(testEvent1.EntityId);

            var domainEvent = result.Value.Single().DomainEvent;

            Assert.IsTrue(domainEvent.EntityId.Equals(testEvent1.EntityId));
        }
Ejemplo n.º 11
0
        public async Task TestDeserializationOfIdInInterface(PersistenceLayerProvider layerProvider)
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEv2>();
            var entityStreamRepository = layerProvider.EventRepository;

            var domainEvent = new TestEv2(Guid.NewGuid());
            await entityStreamRepository.AppendAsync(new List <IDomainEvent> {
                domainEvent
            }, 0);

            var deserialize = (await entityStreamRepository.LoadEvents()).Value.Single().DomainEvent;

            Assert.AreEqual(deserialize.EntityId, domainEvent.EntityId);
            Assert.AreNotEqual(deserialize.EntityId, Guid.Empty.ToString());
        }
Ejemplo n.º 12
0
        public async Task SnapshotIsChangedAfterSavingIt_InMemoryBug(PersistenceLayerProvider layerProvider)
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <Event1>();
            BsonMapRegistrationHelpers.AddBsonMapFor <Event2>();
            if (layerProvider.SnapShotRepository.GetType() == typeof(SnapShotRepositoryInMemory))
            {
                return;                                                                                   // this is not supported in memory
            }
            var eventStore = new EventStore(layerProvider.EventRepository, layerProvider.SnapShotRepository, new
                                            SnapShotConfig(new List <ISnapShot> {
                new SnapShot <UserWithAutoAply>(2)
            }));

            var event1           = UserWithAutoAply.Create();
            var userWithAutoAply = new UserWithAutoAply();

            userWithAutoAply.Apply(event1);
            var changeNameEvent1 = userWithAutoAply.AppendName("neuer Name");
            var changeNameEvent2 = userWithAutoAply.AppendName("neuer Name");
            var changeNameEvent3 = userWithAutoAply.AppendName("neuer Name");

            var entityId = event1.Id;
            await eventStore.AppendAsync(new List <IDomainEvent>
            {
                event1, changeNameEvent1, changeNameEvent2, changeNameEvent3
            }, 0);

            var result = await eventStore.LoadAsync <UserWithAutoAply>(entityId.ToString());

            var userLoaded = result.Value;

            Assert.AreEqual(3, userLoaded.Names.Count());
            Assert.AreEqual(4, result.Version);

            userLoaded.AppendName("new stuff");

            Assert.AreEqual(4, userLoaded.Names.Count());

            var resultLoadedAgain = await eventStore.LoadAsync <UserWithAutoAply>(entityId.ToString());

            Assert.AreEqual(3, resultLoadedAgain.Value.Names.Count());
        }
Ejemplo n.º 13
0
        public async Task AddAndLoadEventsByTimeStamp_SavedAsType(PersistenceLayerProvider layerProvider)
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEvent1>();

            var eventRepository = layerProvider.EventRepository;

            var newGuid     = Guid.NewGuid();
            var domainEvent = new TestEvent1(newGuid);

            await eventRepository.AppendAsync(new List <IDomainEvent> {
                domainEvent
            }, 0);

            var result = await eventRepository.LoadEventsByTypeAsync(typeof(TestEvent1).Name);

            Assert.AreEqual(1, result.Value.Count());
            Assert.AreEqual(1, result.Value.ToList()[0].EntityStreamVersion);
            Assert.AreEqual(newGuid.ToString(), result.Value.ToList()[0].DomainEvent.EntityId);
            Assert.AreEqual(typeof(TestEvent1), result.Value.ToList()[0].DomainEvent.GetType());
        }
Ejemplo n.º 14
0
        public async Task AddAndLoadEventsByTimeStamp(PersistenceLayerProvider layerProvider)
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEvent1>();
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEvent2>();

            var eventRepository = layerProvider.EventRepository;

            var newGuid = Guid.NewGuid();
            var events  = new List <IDomainEvent> {
                new TestEvent1(newGuid), new TestEvent2(newGuid), new TestEvent1(newGuid), new TestEvent2(newGuid)
            };

            await eventRepository.AppendAsync(events, 0);

            var result = await eventRepository.LoadEvents();

            Assert.AreEqual(4, result.Value.Count());
            Assert.AreEqual(1, result.Value.ToList()[0].EntityStreamVersion);
            Assert.AreEqual(newGuid.ToString(), result.Value.ToList()[0].DomainEvent.EntityId);
        }
Ejemplo n.º 15
0
        public async Task AddEvents_RunTypeProjection(PersistenceLayerProvider layerProvider)
        {
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEvent1>();
            BsonMapRegistrationHelpers.AddBsonMapFor <TestEvent2>();

            var eventRepository = layerProvider.EventRepository;

            var newGuid = Guid.NewGuid();
            var events  = new List <IDomainEvent> {
                new TestEvent1(newGuid), new TestEvent2(newGuid), new TestEvent1(newGuid), new TestEvent2(newGuid)
            };

            await eventRepository.AppendAsync(events, 0);

            var result = await eventRepository.LoadEventsByTypeAsync(typeof(TestEvent1).Name);

            Assert.AreEqual(2, result.Value.Count());
            Assert.AreEqual(1, result.Value.ToList()[0].EntityStreamVersion);
            Assert.AreEqual(3, result.Value.ToList()[1].EntityStreamVersion);
            Assert.AreEqual(newGuid.ToString(), result.Value.ToList()[0].DomainEvent.EntityId);
            Assert.AreEqual(typeof(TestEvent1), result.Value.ToList()[0].DomainEvent.GetType());
        }