public void CreateFromDiffWithNothingToUpdate()
        {
            var documentA = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" },
                { "RegisteredDate", new DateTime(2017, 10, 1) },
                { "IsActive", true },
                { "RelatedIds", new int[] { 1, 3, 5, 7 } }
            });

            var documentB = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" },
                { "RegisteredDate", new DateTime(2017, 10, 1) },
                { "IsActive", true },
                { "RelatedIds", new int[] { 1, 3, 5, 7 } }
            });

            var expectedResult = new BsonDocument();

            var rawResult      = UpdateDefinitionHelper.CreateFromDiff <BsonDocument>(documentA, documentB);
            var renderedResult = RenderDefinition(rawResult);

            Assert.AreEqual(expectedResult, renderedResult);
        }
        public void CreateFromDiffWithUpdatedArrayItems()
        {
            var documentA = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" },
                { "RelatedIds", new int[] { 1, 3, 5 } }
            });

            var documentB = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" },
                { "RelatedIds", new int[] { 2, 4, 6 } }
            });

            var expectedResult = new BsonDocument(new Dictionary <string, object>
            {
                {
                    "$set", new Dictionary <string, object>
                    {
                        { "RelatedIds.0", 2 },
                        { "RelatedIds.1", 4 },
                        { "RelatedIds.2", 6 }
                    }
                }
            });

            var rawResult      = UpdateDefinitionHelper.CreateFromDiff <BsonDocument>(documentA, documentB);
            var renderedResult = RenderDefinition(rawResult);

            Assert.AreEqual(expectedResult, renderedResult);
            Assert.AreEqual(documentB, PerformUpdateAgainstServer(documentA, rawResult));
        }
 public UpdateDefinition <BsonDocument> Benchmark()
 {
     return(UpdateDefinitionHelper.CreateFromDiff <BsonDocument>(
                new BsonDocument(new Dictionary <string, object>
     {
         { "Age", 20 },
         { "Name", "Peter" },
         { "FavouriteNumber", 8 },
         { "RegisteredDate", new DateTime(2017, 10, 1) },
         { "FavouriteDate", new DateTime(2019, 8, 1) },
         { "SomeBoolean", true },
         { "RelatedIds", new int[] { 1, 3, 5, 7 } },
         { "Status", "Active" },
         { "DescriptionOne", null },
         { "DescriptionTwo", "Hello World" },
         { "ExclusivePropertyToFirst", "Hello World" }
     }),
                new BsonDocument(new Dictionary <string, object>
     {
         { "Age", 21 },
         { "Name", "Simon" },
         { "FavouriteNumber", 8 },
         { "RegisteredDate", new DateTime(2017, 8, 1) },
         { "FavouriteDate", new DateTime(2019, 8, 1) },
         { "SomeBoolean", false },
         { "RelatedIds", new int[] { 1, 3, 6, 7 } },
         { "MoreNumbers", new int[] { 1, 3, 5, 7, 9 } },
         { "Status", "Active" },
         { "DescriptionOne", "Hello World" },
         { "DescriptionTwo", null },
         { "ExclusivePropertyToSecond", "Hello World" }
     })
                ));
 }
        public IEnumerable <WriteModel <TEntity> > GetModel()
        {
            var definition      = EntityMapping.GetOrCreateDefinition(typeof(TEntity));
            var updateDefintion = UpdateDefinitionHelper.CreateFromDiff <TEntity>(EntityEntry.OriginalValues, EntityEntry.CurrentValues);

            //MongoDB doesn't like it if an UpdateDefinition is empty.
            //This is primarily to work around a mutation that may set an entity to its default state.
            if (updateDefintion.HasChanges())
            {
                yield return(new UpdateOneModel <TEntity>(definition.CreateIdFilterFromEntity(EntityEntry.Entity), updateDefintion));
            }
        }
示例#5
0
        public IEnumerable <WriteModel <TEntity> > GetModel(WriteModelOptions options)
        {
            var entity = EntityEntry.Entity as TEntity;

            var validationContext = new ValidationContext(entity);

            Validator.ValidateObject(entity, validationContext);

            var definition      = EntityMapping.GetOrCreateDefinition(typeof(TEntity));
            var updateDefintion = UpdateDefinitionHelper.CreateFromDiff <TEntity>(EntityEntry.OriginalValues, EntityEntry.CurrentValues);

            yield return(new UpdateOneModel <TEntity>(definition.CreateIdFilterFromEntity(entity), updateDefintion));
        }
        public void CreateFromDiffWithUpdatedRootProperties()
        {
            var documentA = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" },
                { "RegisteredDate", new DateTime(2017, 10, 1) },
                { "IsActive", true },
                { "RelatedIds", new int[] { 1, 3, 5, 7 } },
                { "Status", "Active" },
                { "Description", null }
            });

            var documentB = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 17 },
                { "Name", "Sam" },
                { "RegisteredDate", new DateTime(2018, 3, 10) },
                { "IsActive", false },
                { "RelatedIds", new int[] { 2, 4, 6, 8 } },
                { "Status", null },
                { "Description", "Sam I am!" }
            });

            var expectedResult = new BsonDocument(new Dictionary <string, object>
            {
                {
                    "$set", new Dictionary <string, object>
                    {
                        { "Age", 17 },
                        { "Name", "Sam" },
                        { "RegisteredDate", new DateTime(2018, 3, 10) },
                        { "IsActive", false },
                        { "RelatedIds.0", 2 },
                        { "RelatedIds.1", 4 },
                        { "RelatedIds.2", 6 },
                        { "RelatedIds.3", 8 },
                        { "Status", null },
                        { "Description", "Sam I am!" }
                    }
                }
            });

            var rawResult      = UpdateDefinitionHelper.CreateFromDiff <BsonDocument>(documentA, documentB);
            var renderedResult = RenderDefinition(rawResult);

            Assert.AreEqual(expectedResult, renderedResult);
            Assert.AreEqual(documentB, PerformUpdateAgainstServer(documentA, rawResult));
        }
        public void CreateFromDiffWithNewProperties()
        {
            var documentA = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" }
            });

            var documentB = new BsonDocument(new Dictionary <string, object>
            {
                { "Age", 20 },
                { "Name", "Peter" },
                {
                    "Address", new Dictionary <string, object>
                    {
                        { "Street", "My Test Road" },
                        { "Suburb", "Other Suburb" }
                    }
                },
                { "RegisteredDate", new DateTime(2018, 3, 10) }
            });

            var expectedResult = new BsonDocument(new Dictionary <string, object>
            {
                {
                    "$set", new Dictionary <string, object>
                    {
                        {
                            "Address", new Dictionary <string, object>
                            {
                                { "Street", "My Test Road" },
                                { "Suburb", "Other Suburb" }
                            }
                        },
                        { "RegisteredDate", new DateTime(2018, 3, 10) }
                    }
                }
            });

            var rawResult      = UpdateDefinitionHelper.CreateFromDiff <BsonDocument>(documentA, documentB);
            var renderedResult = RenderDefinition(rawResult);

            Assert.AreEqual(expectedResult, renderedResult);
            Assert.AreEqual(documentB, PerformUpdateAgainstServer(documentA, rawResult));
        }
示例#8
0
        private IEnumerable <WriteModel <TEntity> > BuildWriteModel(IDbEntityCollection <TEntity> entityCollection)
        {
            var idFieldName = EntityMapper.GetIdName();
            var writeModel  = new List <WriteModel <TEntity> >();

            foreach (var entry in entityCollection.GetEntries())
            {
                if (entry.State == DbEntityEntryState.Added)
                {
                    EntityMutation <TEntity> .MutateEntity(entry.Entity, MutatorType.Insert, Database);

                    writeModel.Add(new InsertOneModel <TEntity>(entry.Entity));
                }
                else if (entry.State == DbEntityEntryState.Updated)
                {
                    EntityMutation <TEntity> .MutateEntity(entry.Entity, MutatorType.Update, Database);

                    var idFieldValue = EntityMapper.GetIdValue(entry.Entity);
                    var filter       = Builders <TEntity> .Filter.Eq(idFieldName, idFieldValue);

                    var updateDefintion = UpdateDefinitionHelper.CreateFromDiff <TEntity>(entry.OriginalValues, entry.CurrentValues);

                    //MongoDB doesn't like it if an UpdateDefinition is empty.
                    //This is primarily to work around a mutation that may set an entity to its default state.
                    if (updateDefintion.HasChanges())
                    {
                        writeModel.Add(new UpdateOneModel <TEntity>(filter, updateDefintion));
                    }
                }
                else if (entry.State == DbEntityEntryState.Deleted)
                {
                    var idFieldValue = EntityMapper.GetIdValue(entry.Entity);
                    var filter       = Builders <TEntity> .Filter.Eq(idFieldName, idFieldValue);

                    writeModel.Add(new DeleteOneModel <TEntity>(filter));
                }
            }

            return(writeModel);
        }