/// <summary> /// Creates the base diff. /// </summary> /// <param name="baseline">The base object.</param> /// <param name="properties">The included property names.</param> /// <returns></returns> public static DiffBaseline Create(object baseline, params string[] properties) { Argument.Assert.IsNotNull(baseline, nameof(baseline)); DiffBaseline diff = new DiffBaseline(baseline.GetType()); Type baselineType = baseline.GetType(); if (properties == null) { for (int i = 0; i < baselineType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Length; i++) { AddPropertyToBaseline(baselineType.GetProperties(BindingFlags.Public | BindingFlags.Instance)[i], baseline, baselineType, diff); } } else { for (int i = 0; i < properties.Length; i++) { AddPropertyToBaseline(baselineType.GetProperty(properties[i]), baseline, baselineType, diff); } } return(diff); }
public void DiffWithBaselineFromPropertDiffBaselineClass() { DiffObject baseDiffObject = new DiffObject("Brian", "Developer", "111 Main Street", DateTime.Parse("1/1/1973"), "North"); baseDiffObject.PhoneNumber = PhoneNumber.Parse("222-222-2222"); DiffObject diffObject2 = new DiffObject("Brian", "Developer", "111 Main Street", DateTime.Parse("1/1/1973"), "North"); diffObject2.PhoneNumber = PhoneNumber.Parse("222-222-2222"); SubDiffObject diffObject3 = new SubDiffObject("Brian", "Developer", "111 Main Street", DateTime.Parse("1/1/1973"), "North"); diffObject3.PhoneNumber = PhoneNumber.Parse("222-222-2222"); DiffBaseline baseDiff = DiffBaseline.Create(diffObject2); Assert.AreEqual(NumberOfProperties, baseDiff.Entries.Count); diffObject2.Address = "222 Main Street"; diffObject2.PhoneNumber = PhoneNumber.Parse("444-444-4444"); DiffGenerator <DiffObject> diffGenerator = new DiffGenerator <DiffObject>(); Diff <DiffObject> diff = diffGenerator.Generate(baseDiff, diffObject2); Assert.IsNotNull(diff); Assert.AreEqual(2, diff.Entries.Count); Assert.AreEqual("Address", diff.Entries[0].Name); Assert.AreEqual(baseDiffObject.Address, diff.Entries[0].BaselineValue); Assert.AreEqual(diffObject2.Address, diff.Entries[0].NewValue); Assert.AreEqual("Phone Number", diff.Entries[1].Name); Assert.AreEqual(baseDiffObject.PhoneNumber.ToString(), diff.Entries[1].BaselineValue); Assert.AreEqual(diffObject2.PhoneNumber.ToString(), diff.Entries[1].NewValue); }
public void CreateBaseDiff() { DiffObject baseDiffObject = new DiffObject("Brian", "Developer", "111 Main Street", DateTime.Parse("1/1/1973"), "North"); baseDiffObject.PhoneNumber = PhoneNumber.Parse("222-222-2222"); DiffObject diffObject2 = new DiffObject("Brian", "Developer", "111 Main Street", DateTime.Parse("1/1/1973"), "North"); diffObject2.PhoneNumber = PhoneNumber.Parse("222-222-2222"); SubDiffObject diffObject3 = new SubDiffObject("Brian", "Developer", "111 Main Street", DateTime.Parse("1/1/1973"), "North"); diffObject3.PhoneNumber = PhoneNumber.Parse("222-222-2222"); DiffGenerator <DiffObject> diffGenerator = new DiffGenerator <DiffObject>(); DiffBaseline diff = diffGenerator.GenerateBaseline(baseDiffObject); Assert.AreEqual(NumberOfProperties, diff.Entries.Count); }
/// <summary> /// Executes the specified base property diff. /// </summary> /// <param name="baseline">The base property diff.</param> /// <param name="working">The new object.</param> /// <returns></returns> public Diff <T> Generate(DiffBaseline baseline, T working) { Argument.Assert.IsNotNull(baseline, nameof(baseline)); Argument.Assert.IsNotNull(working, nameof(working)); if (baseline.ReflectedType != working.GetType()) { throw new ArgumentException("The type of 'baseline.ReflectedType' and 'working' must match."); } Diff <T> diff = new Diff <T>(); diff.Name = baseline.ReflectedType.Name; for (int i = 0; i < baseline.Entries.Count; i++) { DiffBaselineEntry entry = baseline.Entries[i]; PropertyInfo propertyInfo = baseline.ReflectedType.GetProperty(entry.Name); DiffVisibleAttribute attr = (DiffVisibleAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(DiffVisibleAttribute)); if (attr == null || attr.Visible) { string newValueString = null; object newValue = baseline.ReflectedType.GetProperty(entry.Name).GetValue(working, null); if (newValue != null) { newValueString = newValue.ToString(); } if (string.Compare(entry.BaselineValue, newValueString, StringComparison.Ordinal) != 0) { diff.AddEntry(attr != null && attr.FriendlyName != null ? attr.FriendlyName : entry.Name, entry.BaselineValue, newValueString); } } } return(diff); }
private static void AddPropertyToBaseline(MemberInfo propertyInfo, object baseline, Type baselineType, DiffBaseline diff) { DiffVisibleAttribute attr = (DiffVisibleAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(DiffVisibleAttribute)); if (attr == null || attr.Visible) { object baselineValue = baselineType.GetProperty(propertyInfo.Name).GetValue(baseline, null); diff.Add(propertyInfo.Name, baselineValue != null ? baselineValue.ToString() : null); } }
/// <summary> /// Creates the base diff. /// </summary> /// <param name="baseline">The base object.</param> /// <param name="properties">The included property names.</param> /// <returns></returns> public DiffBaseline GenerateBaseline(T baseline, params string[] properties) { return(DiffBaseline.Create(baseline, properties)); }
public Diff <DiffObject> CreateDiff(DiffBaseline baseline) { return(diffGenerator.Generate(baseline, this)); }