public void ShouldSeeChangeToBasePropertyWhenBaseObject()
        {
            var           detector = new DerivedChangeDetector();
            var           tracker  = new EntityChangeTracker <TestEntity>(detector);
            DerivedEntity entity   = new DerivedEntity()
            {
                IntValue = 123
            };

            tracker.Attach(entity);

            entity.IntValue = 234;

            var changes = tracker.DetectChanges();

            Assert.AreEqual(1, changes.Count(), "The wrong number of changes were detected.");
            IEntityChange <TestEntity> change = changes.Single();

            Assert.AreSame(entity, change.Entity, "A change was detected on the wrong entity.");
            Assert.AreEqual(EntityState.Modified, change.State, "The entity should have been modified.");

            Assert.AreEqual(1, change.GetChanges().Count(), "The wrong number of properties were seen as changed.");
            var propertyChange = change.GetChanges().Single();

            Assert.AreEqual(TestEntityChangeDetector.IntDescription, propertyChange.DisplayName, "The wrong property was recorded.");

            bool hasBaseChange = change.HasChange(x => x.IntValue);

            Assert.IsTrue(hasBaseChange, "The change was not detected.");

            bool hasDerivedChange = change.As <DerivedEntity>().HasChange(x => x.IntValue);

            Assert.IsTrue(hasDerivedChange, "The change was not detected.");
        }
        private IEnumerable <IEntityChange <TEntity> > detectChanges(EntityState state)
        {
            List <IEntityChange <TEntity> > changes = new List <IEntityChange <TEntity> >();

            foreach (Entity <TEntity> context in lookup.Values)
            {
                if (context.State == EntityState.Added && state.HasFlag(EntityState.Added))
                {
                    IEntityChange <TEntity> change = getEntityChange(context);
                    changes.Add(change);
                }
                else if (context.State == EntityState.Unmodified && (state.HasFlag(EntityState.Modified) || state.HasFlag(EntityState.Unmodified)))
                {
                    IEntityChange <TEntity> change = getEntityChange(context);
                    if (change.GetChanges().Any())
                    {
                        if (state.HasFlag(EntityState.Modified))
                        {
                            changes.Add(change);
                        }
                    }
                    else if (state.HasFlag(EntityState.Unmodified))
                    {
                        changes.Add(change);
                    }
                }
                else if (context.State == EntityState.Removed && state.HasFlag(EntityState.Removed))
                {
                    IEntityChange <TEntity> change = getEntityChange(context);
                    changes.Add(change);
                }
            }
            return(changes);
        }
        public void ShouldDetectChangeToDerivedPropertyWhenAdding()
        {
            var           detector = new DerivedChangeDetector();
            var           tracker  = new EntityChangeTracker <TestEntity>(detector);
            DerivedEntity entity   = new DerivedEntity()
            {
                DerivedValue = 123
            };

            tracker.Add(entity);

            var changes = tracker.DetectChanges();

            Assert.AreEqual(1, changes.Count(), "The wrong number of changes were detected.");
            IEntityChange <TestEntity> change = changes.Single();

            Assert.AreSame(entity, change.Entity, "A change was detected on the wrong entity.");
            Assert.AreEqual(EntityState.Added, change.State, "The entity should have been modified.");
            Assert.AreEqual(1, change.GetChanges().Count(), "The wrong number of properties were seen as changed.");
        }
        public void ShouldIncludeChangeToDoubleDerivedProperty()
        {
            var detector = new DoubleDerivedChangeDetector();
            var tracker  = new EntityChangeTracker <TestEntity>(detector);
            DoubleDerivedEntity entity = new DoubleDerivedEntity()
            {
                DoubleDerivedValue = "John"
            };

            tracker.Attach(entity);

            entity.DoubleDerivedValue = "Tom";

            var changes = tracker.DetectChanges();

            Assert.AreEqual(1, changes.Count(), "The wrong number of changes were detected.");
            IEntityChange <TestEntity> change = changes.Single();

            Assert.AreSame(entity, change.Entity, "A change was detected on the wrong entity.");
            Assert.AreEqual(EntityState.Modified, change.State, "The entity should have been modified.");
            Assert.AreEqual(1, change.GetChanges().Count(), "The wrong number of properties were seen as changed.");
        }