Exemple #1
0
        private async Task <Dictionary <Guid, IEventSourcedAggregateRoot> > LoadAggregatesAsync(Guid[] aggregateIds)
        {
            IReadOnlyDictionary <Guid, IReadOnlyDictionary <string, string> > eventStreamMetadata = await eventStore.BatchFindStreamMetadataAsync(aggregateIds);

            IDictionary <Guid, IReadOnlyCollection <IEventStoreRecord> > eventRecords = await eventStore.BatchFindEventsAsync(aggregateIds);

            return(eventStreamMetadata.ToDictionary(x => x.Key,
                                                    x =>
            {
                eventRecords.TryGetValue(x.Key, out var events);
                return eventSourcedAggregateFactory.ConstructAndLoadEntityFromEvents(x.Key, x.Value,
                                                                                     events?.Count > 0 ? events : new IEventStoreRecord[0]);
            }));
        }
Exemple #2
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, IReadOnlyCollection <IEventStoreRecord> >()
            {
                {
                    entity2Id,
                    new List <IEventStoreRecord>()
                    {
                        new FakeEventStoreRecord()
                        {
                            Event = new SetFooEvent()
                            {
                                AggregateId = entity2Id
                            },
                            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, 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?

            sut = new EventSourcedAggregateStore(eventStore, entityTypeManager, publishEventBuffer,
                                                 new IRepositoryFilter[] { }, eventMessageFactory, new EntityFactory());
        }