diff --git a/src/YuckQi.Domain.Services/YuckQi.Domain.Services.csproj b/src/YuckQi.Domain.Services/YuckQi.Domain.Services.csproj index 0189c88..f35c857 100644 --- a/src/YuckQi.Domain.Services/YuckQi.Domain.Services.csproj +++ b/src/YuckQi.Domain.Services/YuckQi.Domain.Services.csproj @@ -4,7 +4,7 @@ net6.0 true Kevin J Lambert - 6.2.0 + 6.4.0 LICENSE A .NET library for bootstrapping a domain services project. enable diff --git a/src/YuckQi.Domain.Validation/YuckQi.Domain.Validation.csproj b/src/YuckQi.Domain.Validation/YuckQi.Domain.Validation.csproj index 0497bae..2b3c7ef 100644 --- a/src/YuckQi.Domain.Validation/YuckQi.Domain.Validation.csproj +++ b/src/YuckQi.Domain.Validation/YuckQi.Domain.Validation.csproj @@ -4,7 +4,7 @@ net6.0 true Kevin J Lambert - 6.2.0 + 6.4.0 LICENSE A .NET library providing domain validation fundamentals. enable diff --git a/src/YuckQi.Domain/Extensions/ObjectExtensions.cs b/src/YuckQi.Domain/Extensions/ObjectExtensions.cs new file mode 100644 index 0000000..2a47fd0 --- /dev/null +++ b/src/YuckQi.Domain/Extensions/ObjectExtensions.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace YuckQi.Domain.Extensions; + +public static class ObjectExtensions +{ + private static readonly ConcurrentDictionary> PropertiesByType = new (); + + public static Dictionary GetDifferencesByProperty(this T a, T b) + { + var properties = PropertiesByType.GetOrAdd(typeof(T), type => type.GetProperties()); + var values = properties.Select(t => new KeyValuePair(t, (t.GetValue(a), t.GetValue(b)))); + var differences = values.Where(t => ! Equals(t.Value.Item1, t.Value.Item2)).ToDictionary(t => t.Key, t => t.Value); + + return differences; + } +} diff --git a/src/YuckQi.Domain/YuckQi.Domain.csproj b/src/YuckQi.Domain/YuckQi.Domain.csproj index 81d575f..359cfdf 100644 --- a/src/YuckQi.Domain/YuckQi.Domain.csproj +++ b/src/YuckQi.Domain/YuckQi.Domain.csproj @@ -4,7 +4,7 @@ net6.0 true Kevin J Lambert - 6.2.0 + 6.4.0 LICENSE A .NET library for bootstrapping a domain model project. enable diff --git a/test/YuckQi.Domain.UnitTests/Extensions/ObjectExtensionTests.cs b/test/YuckQi.Domain.UnitTests/Extensions/ObjectExtensionTests.cs new file mode 100644 index 0000000..ea35392 --- /dev/null +++ b/test/YuckQi.Domain.UnitTests/Extensions/ObjectExtensionTests.cs @@ -0,0 +1,24 @@ +using System.Linq; +using NUnit.Framework; +using YuckQi.Domain.Extensions; + +namespace YuckQi.Domain.UnitTests.Extensions; + +public class ObjectExtensionTests +{ + [SetUp] + public void Setup() { } + + [Test] + public void Objects_WithDifferentStringValues_HaveOneDifference() + { + var a = new { a = 123, b = "test" }; + var b = new { a = 123, b = "hello" }; + var differences = a.GetDifferencesByProperty(b); + + Assert.That(differences, Has.Exactly(1).Items); + Assert.That(differences.Single().Key.Name, Is.EqualTo(nameof(a.b))); + Assert.That(differences.Single().Value.Item1, Is.EqualTo("test")); + Assert.That(differences.Single().Value.Item2, Is.EqualTo("hello")); + } +}