コード例 #1
0
    public static void Test()
    {
        var entities = new MyEntities();
        var entity1  = new MyEntity1();

        entities.Add(entity1);
        var entity2 = new MyEntity2();

        entities.Add(entity1);
    }
コード例 #2
0
        public async Task GetAsync_FilterReplacesReturnValue()
        {
            var replacementEntity = new MyEntity2(Guid.NewGuid(), 5);

            repositoryFilter1.FilterResult <IEventSourcedAggregateRoot>(null)
            .ReturnsForAnyArgs(replacementEntity);

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

            Assert.Equal(replacementEntity, await sut.GetAsync <MyEntity2>(entity2Id));
        }
コード例 #3
0
        public void Should_abort_if_there_is_no_primary_key()
        {
            var cmd      = Substitute.For <DbCommand>();
            var uow      = Substitute.For <IAdoNetUnitOfWork>();
            var myEntity = new MyEntity2();
            var ps       = Substitute.For <DbParameterCollection>();

            cmd.Parameters.Returns(ps);
            cmd.CreateParameter().Returns(Substitute.For <DbParameter>());
            uow.CreateCommand().Returns(cmd);

            Func <Task> actual = async() => await uow.DeleteAsync(new MyMapper2(), myEntity);

            actual.Should().Throw <MappingException>();
        }
コード例 #4
0
        public void MainTest()
        {
            using (var session = Domain.OpenSession()) {
                using (var transactionScope = session.OpenTransaction()) {
                    // Creating new persistent object
                    // Creating new persistent object
                    var helloWorld = new MyEntity
                    {
                        Text = "Hello World!"
                    }; var helloWorld2 = new MyEntity2
                    {
                        Text = "Hello World!"
                    };

                    // Committing transaction
                    transactionScope.Complete();
                }

                using (var transactionScope = session.OpenTransaction()) {
                    var c = session.Query.All <MyEntity>()
                            .Select(a => new ValObj {
                        Id = a.Id, Text = a.Text
                    })
                            .ToList();
                    var d = session.Query.All <MyEntity2>()
                            .Select(a => new ValObj {
                        Id = a.Id, Text = a.Text
                    })
                            .ToList();
                    var a11 = new ArrayList()
                    {
                        session.Query.All <MyEntity>().Select("new(Id,Text)")
                    };
                    var b12 = new ArrayList()
                    {
                        session.Query.All <MyEntity2>().Select("new(Id,Text)")
                    };
                }
            }
        }
コード例 #5
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);
        }