コード例 #1
0
        public void Can_add_child_entity_to_parent_entity()
        {
            var dateTime = DateTime.UtcNow;

            var parent = new ParentEntity(ParentEntityId.Generate(), "test parent");
            var child  = new ChildEntity(parent, Guid.NewGuid().ToString(), "test child");

            child.Rename("test child renamed");

            parent.FixDateTime(dateTime);

            var createdEvents = parent.AsIEventSourcedEntity().UncommittedEvents.ToList();

            createdEvents[0].Should().Be(new ParentCreated(parent.Id, "test parent", dateTime));
            createdEvents[1].Should().Be(new ChildCreated(parent.Id, child.Id, "test child", dateTime));
            createdEvents[2].Should().Be(new ChildRenamed(parent.Id, child.Id, "test child renamed", dateTime));

            child.StateModel.Id.Should().Be(child.Id);
            child.StateModel.Name.Should().Be("test child renamed");

            var childStates = parent.StateModel.ChildStates.ToList();

            childStates[0].Should().Be(child.StateModel);
            (childStates[0] as ChildState).Name.Should().Be("test child renamed");
        }
コード例 #2
0
        public void SetAggregateRoot_with_aggregateRoot_null_throws_ArgumentNullException()
        {
            var child = new ChildEntity();

            ((Action)(() => ((IChildEntityInternal)child).SetAggregateRoot(null, ChildEntityId.Generate()))).Should().Throw <ArgumentNullException>()
            .And.ParamName.Should().Be("aggregateRoot");
        }
コード例 #3
0
        public void SetAggregateRoot_cannot_be_used_twice()
        {
            var child = new ChildEntity();

            ((IChildEntityInternal)child).SetAggregateRoot(new ParentEntity(ParentEntityId.Generate(), "name"), ChildEntityId.Generate());

            ((Action)(() => ((IChildEntityInternal)child).SetAggregateRoot(new ParentEntity(ParentEntityId.Generate(), "name"), ChildEntityId.Generate()))).Should().Throw <InvalidOperationException>()
            .And.Message.Should().Be("AggregateRoot cannot be reassigned in ChildEntity!");
        }
コード例 #4
0
        public void Can_add()
        {
            var dateTime = DateTime.UtcNow;

            var parent = new ParentEntity(Guid.NewGuid().ToString(), "test parent");
            var child  = new ChildEntity(parent, Guid.NewGuid().ToString(), "test child");

            child.Rename("test child renamed");

            parent.FixDateTime(dateTime);

            child.StateModel.Id.Should().Be(child.Id);
            child.StateModel.Name.Should().Be("test child renamed");
            var childStates = parent.StateModel.ChildStates.ToList();

            childStates[0].Should().Be(child.StateModel);
            (childStates[0] as ChildState).Name.Should().Be("test child renamed");

            var createdEvents = parent.AsIEventSourcedEntity().UncommittedEvents.ToList();

            createdEvents[0].Should().Be(new ParentCreated(parent.Id, "test parent", dateTime));
            createdEvents[1].Should().Be(new ChildCreated(parent.Id, child.Id, "test child", dateTime));
            createdEvents[2].Should().Be(new ChildRenamed(parent.Id, child.Id, "test child renamed", dateTime));

            var parent2 = new ParentEntity(createdEvents);

            (parent2.StateModel.ChildStates.Single() as ChildState).Name.Should().Be("test child renamed");
            var child2 = new ChildEntity(parent2, createdEvents.Where(x => x is IChildEntityEvent).Cast <IChildEntityEvent>());

            (parent2 as IEventSourcedEntityInternal).RaiseEvent(new ChildRenamed(child.AggregateRootId, child.Id, "new new 3", dateTime));

            parent.StateModel.StreamName.Should().Be(parent2.StateModel.StreamName);
            parent.StateModel.Name.Should().Be(parent2.StateModel.Name);

            child2.StateModel.Id.Should().Be(child.Id);
            child2.StateModel.AggregateRootId.Should().Be(parent2.StateModel.Id);
            child2.Id.Should().Be(child.Id);
            child2.StateModel.Name.Should().Be("new new 3");
        }