public void ShouldCommitToMultipleContexts()
        {
            // Arrange
            ICollection <ChangeSet> changeSets;

            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                var context1     = this.CreateContext();
                var contextMock2 = new Mock <ISampleContextTwo>();
                contextMock2.Setup(m => m.SaveChanges()).Returns(new ChangeSet(typeof(ISampleContextTwo), new List <IChange> {
                    Change.CreateAddedChange(new Person())
                }));
                var context2 = contextMock2.Object;

                context1.Set <Employee>().Add(Testdata.Employees.CreateEmployee1());

                unitOfWork.RegisterContext(context1);
                unitOfWork.RegisterContext(context2);

                // Act
                changeSets = unitOfWork.Commit();
            }

            // Assert
            changeSets.Should().HaveCount(2);

            var assertContext1 = this.CreateContext();

            assertContext1.Set <Employee>().ToList().Should().HaveCount(1);
        }
Example #2
0
        /// <summary>
        ///     Determines the changes that are transferred to the persistence layer.
        /// </summary>
        /// <returns>ChangeSet.</returns>
        private ChangeSet GetChangeSet()
        {
            var updatedEntries = this.ChangeTracker.Entries()
                                 .Where(e => e.State == EntityState.Modified && e.Entity != null);

            var updateChanges = new List <IChange>();

            foreach (var dbEntityEntry in updatedEntries)
            {
                IList <PropertyChangeInfo> changes = new List <PropertyChangeInfo>();
                foreach (var propertyName in dbEntityEntry.CurrentValues.Properties.Select(p => p.Name))
                {
                    var property = dbEntityEntry.Property(propertyName);
                    if (property.Metadata.IsShadowProperty())
                    {
                        // BUG: Workaround for resetting IsModified of Discriminator property
                        property.IsModified = false;
                    }

                    if (property.IsModified)
                    {
                        changes.Add(new PropertyChangeInfo(propertyName, property.CurrentValue));
                    }
                }
                updateChanges.Add(Change.CreateUpdateChange(dbEntityEntry.Entity, changes));
            }

            var addChanges = this.ChangeTracker.Entries()
                             .Where(e => e.State == EntityState.Added && e.Entity != null)
                             .Select(e => Change.CreateAddedChange(e.Entity));

            var deleteChanges = this.ChangeTracker.Entries()
                                .Where(e => e.State == EntityState.Deleted && e.Entity != null)
                                .Select(n => Change.CreateDeleteChange(n.Entity));

            var allChanges = new List <IChange>(addChanges);

            allChanges.AddRange(deleteChanges);
            allChanges.AddRange(updateChanges);

            return(new ChangeSet(typeof(TContext), allChanges));
        }
        public void ShouldCommitChangesOfContext()
        {
            // Arrange
            IUnitOfWork unitOfWork = new UnitOfWork();

            var contextMock = new Mock <IContext>();
            var changeSet   = new ChangeSet(contextMock.GetType(), new List <IChange> {
                Change.CreateAddedChange(new object())
            });

            contextMock.Setup(c => c.SaveChanges()).Returns(changeSet);

            unitOfWork.RegisterContext(contextMock.Object);

            // Act
            var numberOfChanges = unitOfWork.Commit();

            // Assert
            numberOfChanges.Should().HaveCount(1);
        }
        /// <summary>
        ///     Determins the changes that are transfered to the persistence layer.
        /// </summary>
        /// <returns>ChangeSet.</returns>
        private ChangeSet GetChangeSet()
        {
            var updatedEntries = this.ChangeTracker.Entries().Where(e => e.State == EntityState.Modified && e.Entity != null);
            var updateChanges = new List<IChange>();

            foreach (var dbEntityEntry in updatedEntries)
            {
                IList<PropertyChangeInfo> changes = new List<PropertyChangeInfo>();
                foreach (var propertyName in dbEntityEntry.CurrentValues.PropertyNames)
                {
                    var property = dbEntityEntry.Property(propertyName);
                    if (property.IsModified)
                    {
                        changes.Add(new PropertyChangeInfo(propertyName, property.CurrentValue));
                    }
                }
                updateChanges.Add(Change.CreateUpdateChange(dbEntityEntry.Entity, changes));
            }

            var addChanges = this.ChangeTracker.Entries().Where(e => e.State == EntityState.Added && e.Entity != null).Select(e => Change.CreateAddedChange(e.Entity));
            var deleteChanges = this.ChangeTracker.Entries().Where(e => e.State == EntityState.Deleted && e.Entity != null).Select(n => Change.CreateDeleteChange(n.Entity));

            var allChanges = new List<IChange>(addChanges);
            allChanges.AddRange(deleteChanges);
            allChanges.AddRange(updateChanges);

            return new ChangeSet(typeof(TContext), allChanges);
        }