public void InsertAggregateRootEntity()
        {
            // Arrange
            const int expectedSimpleId        = 10;
            const int expectedAggregateRootId = 20;

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

            AggregateRootEntity aggregateRootEntity = new AggregateRootEntity
            {
                Id           = expectedAggregateRootId,
                SimpleEntity = new SimpleEntity
                {
                    Id = expectedSimpleId
                }
            };

            // Act
            entityRepository.Insert(aggregateRootEntity);

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

            // Both the AggregateRoot and its child SimpleEntity should be added to the context, but only the aggregate root
            // should have an added state
            Assert.AreEqual(2, entityRepository.DbContext.ChangeTracker.Entries().Count());
            Assert.AreEqual(EntityState.Added, entityRepository.DbContext.FindEntity(expectedAggregateRootId).State);
            Assert.AreEqual(EntityState.Unchanged, entityRepository.DbContext.FindEntity(expectedSimpleId).State);
        }
        public void InsertGraphForNewAggregateRootEntityAndChildrenUsingConvention()
        {
            // Arrange
            FakeDbContext dbContext = new FakeDbContext();
            AggregateRootEntityRepository entityRepository = new AggregateRootEntityRepository(dbContext);

            AggregateRootEntity aggregateRootEntity = new AggregateRootEntity
            {
                SomeOtherProperty = "Some other property",
                SimpleEntity      = new SimpleEntity {
                    SomeProperty = "Some property"
                },
                OtherSimpleEntities = new List <AnotherSimpleEntity>
                {
                    new AnotherSimpleEntity {
                        SomeProperty = "Child property 1"
                    },
                    new AnotherSimpleEntity {
                        SomeProperty = "Child property 2"
                    },
                }
            };

            // 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 => Assert.AreEqual(EntityState.Added, entry.State));
        }
        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);
            });
        }