Example #1
0
 public Aggregate(Guid id, int version, AggregateRootEntity root)
 {
     if (root == null)
     throw new ArgumentNullException("root");
       if (version < InitialVersion)
     throw new ArgumentOutOfRangeException("version",
                                       string.Format(Resources.Aggregate_VersionOutOfRange, InitialVersion));
       _id = id;
       _version = version;
       _root = root;
 }
        public void Attach_AttachSameIdentifierTwice_ShouldThrowException()
        {
            // Arrange
            var identifier          = Guid.NewGuid().ToString("N");
            var aggregateRoot       = new Mock <FakeAggregateRoot>().Object;
            var aggregateRootEntity = new AggregateRootEntity <string, object>(identifier, aggregateRoot, 1);
            var unitOfWork          = new UnitOfWork <string, object>();

            // Act
            unitOfWork.Attach(aggregateRootEntity);

            // Act & Assert
            Action action = () => unitOfWork.Attach(aggregateRootEntity);

            action.Should().Throw <AggregateRootAlreadyAttachedException <string> >()
            .WithMessage($"Exception for aggregate root with identifier '{identifier}': Aggregate root already attached");
        }
        public void InsertGraphForNewAggregateRootEntityAndChildrenWhereChildrenSpecifiedAsUnchanged()
        {
            // Arrange
            const int expectedSimpleId1 = 10;
            const int expectedSimpleId2 = 11;
            const int expectedSimpleId3 = 12;

            FakeDbContext dbContext = new FakeDbContext();
            AggregateRootEntityRepository entityRepository = new AggregateRootEntityRepository(dbContext);

            // Create an aggregate root where one of the 'AnotherSimpleEntity' already exist (i.e. has a primary key)
            AggregateRootEntity aggregateRootEntity = new AggregateRootEntity
            {
                SomeOtherProperty = "Some other property",
                SimpleEntity      = new SimpleEntity {
                    Id = expectedSimpleId1, SomeProperty = "Some property", State = State.Unchanged
                },
                OtherSimpleEntities = new List <AnotherSimpleEntity>
                {
                    new AnotherSimpleEntity {
                        Id = expectedSimpleId2, SomeProperty = "Child property 1", State = State.Unchanged
                    },
                    new AnotherSimpleEntity {
                        Id = expectedSimpleId3, SomeProperty = "Child property 2", State = State.Unchanged
                    },
                }
            };

            // Act
            entityRepository.InsertOrUpdateGraph(aggregateRootEntity);

            // Assert
            Assert.IsNotNull(entityRepository.DbContext);

            // The AggregateRoot and its children should all be added to the context and all have a state of 'Added'
            Assert.AreEqual(4, entityRepository.DbContext.ChangeTracker.Entries().Count());
            entityRepository.DbContext.ChangeTracker.Entries().ToList().ForEach(entry =>
            {
                IEntityWithState <int> entityWithState = (IEntityWithState <int>)entry.Entity;
                Assert.AreEqual(entityWithState.Id == default(int) ? EntityState.Added : EntityState.Unchanged, entry.State);
            });
        }
 public void SetUp()
 {
     _sut = new InitializedThenChangedThenClearedAggregateRootEntity();
 }
 public void SetUp()
 {
     _sut = new ChangedAggregateRootEntity();
 }
 public void SetUp()
 {
     _sut = new InitializedAggregateRootEntity();
 }
 public void SetUp()
 {
     _sut = new PristineAggregateRootEntity();
 }
 public void SetUp()
 {
     _sut = new InitializedThenChangedThenClearedAggregateRootEntity();
 }
 public void SetUp()
 {
     _sut = new ChangedAggregateRootEntity();
 }
 public void SetUp()
 {
     _sut = new InitializedAggregateRootEntity();
 }
 public void SetUp()
 {
     _sut = new FirstChangedThenInitializedAggregateRootEntity();
 }
Example #12
0
 public AggregateAssertions(AggregateRootEntity <TAggregateId> instance)
 {
     Subject    = instance;
     Identifier = instance?.Id?.IdAsString();
 }
 public void Setup()
 {
     _sut = new ChangedThenClearedAggregateRootEntity();
 }
 public static AggregateAssertions <TAggregateId> Should <TAggregateId>(this AggregateRootEntity <TAggregateId> instance) where TAggregateId : IAggregateId
 {
     return(new AggregateAssertions <TAggregateId>(instance));
 }
Example #15
0
 public bool TryGet(Guid id, out AggregateRootEntity aggregate)
 => _aggregates.TryGetValue(id, out aggregate);
 public void Setup()
 {
     _sut = new ChangedThenClearedAggregateRootEntity();
 }
 public static AggregateRootEntityAssertions Should(this AggregateRootEntity subject)
 {
     return(new AggregateRootEntityAssertions(subject));
 }
Example #18
0
 public ChangedInstance()
 {
     _sut = new ChangedEntity();
 }
 public void SetUp()
 {
     _sut = new PristineAggregateRootEntity();
 }