Exemplo n.º 1
0
        private async Task <List <IEventMessageDraft> > CreateEventMessagesAsync(IEventSourcedAggregateRoot aggregate, IReadOnlyCollection <DomainAggregateEvent> events)
        {
            var  messages         = new List <IEventMessageDraft>();
            Guid?aggregateClassId = entityTypeManager.TryGetClassInfoByClrType(aggregate.GetType())?.Id;

            if (aggregateClassId == null)
            {
                throw new InvalidOperationException($"Cannot save event sourced aggregate of type {aggregate.GetType()}: its class ID has not been defined");
            }

            foreach (DomainAggregateEvent ev in events)
            {
                IEventMessageDraft message = await eventMessageFactory.CreateMessageAsync(ev);

                message.SetMetadata(BasicEventMetadataNames.AggregateClassId, aggregateClassId.Value.ToString());
                message.SetMetadata(BasicEventMetadataNames.AggregateVersion, (aggregate.Version + 1).ToString());

                if (aggregate is ITenantOwned tenantOwned)
                {
                    message.SetMetadata(BasicEventMetadataNames.AggregateTenantId, tenantOwned.TenantId?.ToString());
                }

                messages.Add(message);
            }

            return(messages);
        }
Exemplo n.º 2
0
        private async Task<List<IEventMessageDraft>> CreateEventMessagesAsync(IAggregateRoot aggregate, IReadOnlyCollection<DomainAggregateEvent> events)
        {
            var messages = new List<IEventMessageDraft>();
            Guid? aggregateClassId = entityTypeManager.TryGetClassInfoByClrType(aggregate.GetType())?.Id;

            foreach (DomainAggregateEvent ev in events)
            {
                IEventMessageDraft message = await eventMessageFactory.CreateMessageAsync(ev);
                if (aggregateClassId != null)
                {
                    message.SetMetadata(BasicEventMetadataNames.AggregateClassId, aggregateClassId.Value.ToString());
                }
                
                if (aggregate is ITenantOwned tenantOwned)
                {
                    message.SetMetadata(BasicEventMetadataNames.AggregateTenantId, tenantOwned.TenantId?.ToString());
                }

                if (message.Metadata.GetEventId() == null)
                {
                    message.SetMetadata(BasicEventMetadataNames.EventId, Guid.NewGuid().ToString());
                }

                messages.Add(message);
            }

            return messages;
        }
Exemplo n.º 3
0
        public CrudAggregateStoreTests()
        {
            crudRepository     = new InMemoryCrudRepository();
            entityTypeManager  = Substitute.For <IEntityTypeManager>();
            publishEventBuffer = Substitute.For <IPublishEventBuffer>();

            domainClasses = new List <DomainClassInfo>()
            {
                new DomainClassInfo(TestAggregateClassId, null, typeof(TestAggregate))
            };

            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 CrudAggregateStore(crudRepository, entityTypeManager, publishEventBuffer, eventMessageFactory);
        }
Exemplo n.º 4
0
        public EventSourcedRepositoryTests()
        {
            publishEventBuffer = Substitute.For <IPublishEventBuffer>();
            eventStore         = Substitute.For <IEventStore>();
            entityTypeManager  = Substitute.For <IEntityTypeManager>();
            repositoryFilter1  = Substitute.For <IRepositoryFilter>();
            repositoryFilter2  = Substitute.For <IRepositoryFilter>();

            FakeClock.Setup();

            eventStore.GetEventsAsync(entity2Id)
            .Returns(new List <IEventStoreRecord>()
            {
                new FakeEventStoreRecord()
                {
                    Event = new SetFooEvent()
                    {
                        AggregateId = entity2Id
                    },
                    StreamSequenceNumber = 1
                }
            });

            eventStore.GetStreamMetadataAsync(entity2Id)
            .Returns(new Dictionary <string, string>()
            {
                { "TestKey", "TestValue" },
                { AggregateEventStreamMetadataNames.ClassId, entity2ClassId.ToString() }
            });

            eventStore.GetStreamMetadataAsync(entity3Id)
            .Returns(new Dictionary <string, string>()
            {
                { "TestKey", "TestValue" },
                { AggregateEventStreamMetadataNames.ClassId, entity3ClassId.ToString() }
            });

            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 EventSourcedAggregateRepository(eventStore,
                                                      entityTypeManager, publishEventBuffer, new IRepositoryFilter[] {}, eventMessageFactory, new EntityFactory());
        }
Exemplo n.º 5
0
        public async Task PublishAsync <TEvent>(TEvent @event) where TEvent : IEvent
        {
            var message = (IEventMessage <TEvent>) await eventMessageFactory.CreateMessageAsync(@event);

            await PublishAsync(message);
        }
Exemplo n.º 6
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);
        }