private async Task <IEventSourcedAggregateRoot> LoadAggregateAsync(Guid aggregateId) { IReadOnlyDictionary <string, string> eventStreamMetadata = eventStreamMetadata = await eventStore.FindStreamMetadataAsync(aggregateId); if (eventStreamMetadata == null) { return(null); } IReadOnlyCollection <IEventStoreRecord> eventRecords = await eventStore.FindEventsAsync(aggregateId); return(eventSourcedAggregateFactory.ConstructAndLoadEntityFromEvents(aggregateId, eventStreamMetadata, eventRecords?.Count > 0 ? eventRecords : new IEventStoreRecord[0])); }
public EventSourcedAggregateStoreTests() { publishEventBuffer = Substitute.For <IPublishEventBuffer>(); eventStore = Substitute.For <IEventStore>(); entityTypeManager = Substitute.For <IEntityTypeManager>(); repositoryFilter1 = Substitute.For <IRepositoryFilter>(); repositoryFilter2 = Substitute.For <IRepositoryFilter>(); FakeClock.Setup(); entityEvents = new Dictionary <Guid, List <IEventStoreRecord> >() { { entity2Id, new List <IEventStoreRecord>() { new FakeEventStoreRecord() { Event = new SetFooEvent() { AggregateId = entity2Id }, StreamSequenceNumber = 1 } } }, { entity3Id, new List <IEventStoreRecord>() { new FakeEventStoreRecord() { Event = new SetFooEvent() { AggregateId = entity3Id }, StreamSequenceNumber = 1 } } }, { entity4Id, new List <IEventStoreRecord>() { new FakeEventStoreRecord() { Event = new SetFooEvent() { AggregateId = entity4Id }, StreamSequenceNumber = 1 } } } }; eventStore.FindEventsAsync(Arg.Any <Guid>()) .Returns(ci => { var id = ci.ArgAt <Guid>(0); if (entityEvents.TryGetValue(id, out var events)) { return(events); } return(new List <IEventStoreRecord>()); }); eventStore.BatchFindEventsAsync(Arg.Any <Guid[]>()) .Returns(ci => { var ids = ci.ArgAt <Guid[]>(0); var result = new Dictionary <Guid, IReadOnlyCollection <IEventStoreRecord> >(); foreach (Guid id in ids) { if (entityEvents.TryGetValue(id, out var events)) { result.Add(id, events); } } return(result); }); entityMetadata = new Dictionary <Guid, IReadOnlyDictionary <string, string> >() { { entity2Id, new Dictionary <string, string>() { { "TestKey", "TestValue" }, { AggregateEventStreamMetadataNames.ClassId, entity2ClassId.ToString() } } }, { entity3Id, new Dictionary <string, string>() { { "TestKey", "TestValue" }, { AggregateEventStreamMetadataNames.ClassId, entity3ClassId.ToString() } } }, { entity4Id, new Dictionary <string, string>() { { "TestKey", "TestValue" }, { AggregateEventStreamMetadataNames.ClassId, entity2ClassId.ToString() } } } }; eventStore.FindStreamMetadataAsync(Arg.Any <Guid>()) .Returns(ci => { var id = ci.ArgAt <Guid>(0); if (entityMetadata.TryGetValue(id, out var metadata)) { return(metadata); } return(new Dictionary <string, string>()); }); eventStore.BatchFindStreamMetadataAsync(Arg.Any <Guid[]>()) .Returns(ci => { var ids = ci.ArgAt <Guid[]>(0); var result = new Dictionary <Guid, IReadOnlyDictionary <string, string> >(); foreach (Guid id in ids) { if (entityMetadata.TryGetValue(id, out var metadata)) { result.Add(id, metadata); } } return(result); }); eventStore.PushEventsAsync(Guid.Empty, null).ReturnsForAnyArgs(ci => { var events = ci.ArgAt <IEnumerable <IUncommittedEventStoreRecord> >(1); return(events.Select(x => new FakeEventStoreRecord() { AdditionalMetadata = x.Metadata, Event = x.Event, EventId = Guid.NewGuid(), StoreDate = DateTimeOffset.Now, StreamSequenceNumber = 0 }).ToList()); }); DomainClassInfo[] domainClasses = new[] { new DomainClassInfo(entityClassId, null, typeof(MyEntity)), new DomainClassInfo(entity2ClassId, null, typeof(MyEntity2)), new DomainClassInfo(entity3ClassId, null, typeof(MyEntity3LoadsAsDeleted)) }; entityTypeManager.GetClassInfoByClassId(Guid.Empty) .ReturnsForAnyArgs(ci => domainClasses.Single(x => x.Id == ci.Arg <Guid>())); entityTypeManager.TryGetClassInfoByClrType(null) .ReturnsForAnyArgs(ci => domainClasses.SingleOrDefault(x => x.ClrType == ci.Arg <Type>())); entityTypeManager.GetClassInfoByClrType(null) .ReturnsForAnyArgs(ci => domainClasses.Single(x => x.ClrType == ci.Arg <Type>())); eventMessageFactory = Substitute.For <IEventMessageFactory>(); eventMessageFactory.CreateMessageAsync(null).ReturnsForAnyArgs(ci => { var @event = ci.ArgAt <IEvent>(0); Type messageType = typeof(EventMessageDraft <>).MakeGenericType(@event.GetType()); IEventMessageDraft messageDraft = (IEventMessageDraft)messageType.GetConstructor(new[] { @event.GetType() }).Invoke(new[] { @event }); messageDraft.SetMetadata("TestKey", "TestValue"); return(messageDraft); }); // TODO something more lightweight? eventSourcedAggregateFactory = Substitute.For <IEventSourcedAggregateFactory>(); entity2 = new MyEntity2(entity2Id); eventSourcedAggregateFactory.ConstructAndLoadEntityFromEvents(entity2Id, Arg.Is <IReadOnlyDictionary <string, string> >(x => x.SequenceEqual(entityMetadata[entity2Id])), Arg.Is <IReadOnlyCollection <IEventStoreRecord> >(x => x.SequenceEqual(entityEvents[entity2Id]))) .Returns(entity2); entity3 = new MyEntity3LoadsAsDeleted(entity3Id); eventSourcedAggregateFactory.ConstructAndLoadEntityFromEvents(entity3Id, Arg.Is <IReadOnlyDictionary <string, string> >(x => x.SequenceEqual(entityMetadata[entity3Id])), Arg.Is <IReadOnlyCollection <IEventStoreRecord> >(x => x.SequenceEqual(entityEvents[entity3Id]))) .Returns(entity3); entity4 = new MyEntity2(entity4Id); eventSourcedAggregateFactory.ConstructAndLoadEntityFromEvents(entity4Id, Arg.Is <IReadOnlyDictionary <string, string> >(x => x.SequenceEqual(entityMetadata[entity4Id])), Arg.Is <IReadOnlyCollection <IEventStoreRecord> >(x => x.SequenceEqual(entityEvents[entity4Id]))) .Returns(entity4); sut = new EventSourcedAggregateStore(eventStore, entityTypeManager, publishEventBuffer, new IRepositoryFilter[] { }, eventMessageFactory, eventSourcedAggregateFactory); }