Example #1
0
        public static MergeComparer <MergeTestObject> CreateMerger()
        {
            var merger = MergeComparer <MergeTestObject> .Create(o => o.ID, true);

            merger.MatchAlgorithm.AddMatchEvaluator(Property1Score, o => o.Property1);
            merger.MatchAlgorithm.AddMatchEvaluator(Property2Score, o => o.Property2);
            merger.MatchAlgorithm.AddMatchEvaluator(Property3Score, o => o.Property3, new SoftStringEqualityComparer(StringComparisonOptions.CaseInsensitive));

            return(merger);
        }
Example #2
0
        public void MergeMissing_MergesMissingValue()
        {
            var comparer = MergeComparer <MergeTestObject> .Create(o => o.ID, true);

            var instance1 = new MergeTestObject()
            {
                Property3 = "foo"
            };

            var instance2 = new MergeTestObject()
            {
                Property3 = null
            };

            comparer.CompareAlgorithm.MergeMissing(instance1, instance2);

            Assert.AreEqual(instance1.Property3, instance2.Property3);
            Assert.AreEqual("foo", instance1.Property3);
            Assert.AreEqual("foo", instance2.Property3);
        }
Example #3
0
        public void Compare_NonSimplePropertiesAreIgnored()
        {
            var comparer = MergeComparer <MergeTestObjectWithComplexMember> .Create(o => o.ID, true);

            var instance1 = new MergeTestObjectWithComplexMember()
            {
                ComplexMember1 = new MergeTestObject(),
                ComplexMember2 = new List <object>(new[] { "foo" })
            };

            var instance2 = new MergeTestObjectWithComplexMember()
            {
                ComplexMember1 = new MergeTestObject(),
                ComplexMember2 = new List <object>(new[] { "bar" })
            };

            var conflicts = comparer.CompareAlgorithm.Compare(instance1, instance2);

            Assert.AreEqual(0, conflicts.Count());
        }
Example #4
0
        public void MergeMissing_DoesNotOverwriteExistingValue()
        {
            var comparer = MergeComparer <MergeTestObject> .Create(o => o.ID, true);

            var instance1 = new MergeTestObject()
            {
                Property1 = 1,
                Property3 = "foo"
            };

            var instance2 = new MergeTestObject()
            {
                Property3 = "bar"
            };

            comparer.CompareAlgorithm.MergeMissing(instance1, instance2);

            Assert.AreNotEqual(instance1.Property3, instance2.Property3);
            Assert.AreEqual("foo", instance1.Property3);
            Assert.AreEqual("bar", instance2.Property3);
        }
Example #5
0
        static void Main(string[] args)
        {
            // create two objects with the same key
            var key = Guid.NewGuid();

            var fromDatabase = new TestObject()
            {
                Id        = key,
                Property1 = "foo",
                Property2 = 1
            };

            var fromView = new TestObject()
            {
                Id        = key,
                Property1 = "bar",
                Property2 = 1
            };

            // create object that matches TestObject instances on Id things (usually want to make this into a singleton)
            var mergeComparer = MergeComparer <TestObject> .Create(o => o.Id, false);

            mergeComparer.MatchAlgorithm.AddMatchEvaluator(100, o => o.Id);

            // compare the two objects, using the user-supplied input as a source
            var results = mergeComparer.Compare(new[] { fromView }, new[] { fromDatabase });

            // there should only be one result, since we match on Id
            var result = results.Single();

            foreach (var conflict in result.Conflicts)
            {
                // destination is the old value in the database
                Console.WriteLine("User changing {0} from {1} to {2}", conflict.PropertyName, conflict.DestinationValue, conflict.SourceValue);
            }

            WaitForUser();
        }
Example #6
0
        public void MergeAll_OverwritesAllValues()
        {
            var comparer = MergeComparer <MergeTestObject> .Create(o => o.ID, true);

            var instance1 = new MergeTestObject()
            {
                Property1 = 1,
                Property2 = 3,
                Property3 = "foo"
            };

            var instance2 = new MergeTestObject()
            {
                Property2 = 0,
                Property3 = "bar"
            };

            comparer.CompareAlgorithm.MergeAll(instance1, instance2);

            Assert.AreEqual(instance1.Property1, instance2.Property1);
            Assert.AreEqual(instance1.Property2, instance2.Property2);
            Assert.AreEqual(instance1.Property3, instance2.Property3);
        }