diff --git a/src/YuckQi.Domain.Services/YuckQi.Domain.Services.csproj b/src/YuckQi.Domain.Services/YuckQi.Domain.Services.csproj index f35c857..5bc311c 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.4.0 + 6.4.1 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 2b3c7ef..8ba0233 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.4.0 + 6.4.1 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 index 2a47fd0..16a63fd 100644 --- a/src/YuckQi.Domain/Extensions/ObjectExtensions.cs +++ b/src/YuckQi.Domain/Extensions/ObjectExtensions.cs @@ -12,10 +12,24 @@ public static class ObjectExtensions public static Dictionary GetDifferencesByProperty(this T a, T b) { - var properties = PropertiesByType.GetOrAdd(typeof(T), type => type.GetProperties()); + var properties = GetObjectProperties(typeof(T)); 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); + var result = values.Where(t => ! Equals(t.Value.Item1, t.Value.Item2)).ToDictionary(t => t.Key, t => t.Value); - return differences; + return result; + } + + public static HashSet GetNonNullProperties(this T a) + { + var properties = GetObjectProperties(typeof(T)); + var values = properties.Select(t => new KeyValuePair(t, t.GetValue(a))); + var result = values.Where(t => t.Value != null).Select(t => t.Key).ToHashSet(); + + return result; + } + + private static IEnumerable GetObjectProperties(Type type) + { + return PropertiesByType.GetOrAdd(type, t => t.GetProperties()); } } diff --git a/src/YuckQi.Domain/YuckQi.Domain.csproj b/src/YuckQi.Domain/YuckQi.Domain.csproj index 359cfdf..cbad02a 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.4.0 + 6.4.1 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 index ea35392..25b0310 100644 --- a/test/YuckQi.Domain.UnitTests/Extensions/ObjectExtensionTests.cs +++ b/test/YuckQi.Domain.UnitTests/Extensions/ObjectExtensionTests.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using NUnit.Framework; using YuckQi.Domain.Extensions; @@ -14,11 +15,21 @@ public void Objects_WithDifferentStringValues_HaveOneDifference() { var a = new { a = 123, b = "test" }; var b = new { a = 123, b = "hello" }; - var differences = a.GetDifferencesByProperty(b); + var result = 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")); + Assert.That(result, Has.Exactly(1).Items); + Assert.That(result.Single().Key.Name, Is.EqualTo(nameof(a.b))); + Assert.That(result.Single().Value.Item1, Is.EqualTo("test")); + Assert.That(result.Single().Value.Item2, Is.EqualTo("hello")); + } + + [Test] + public void Object_WithSomeNonNullProperties_HasExpectedValue() + { + var a = new { a = new Int32?(), b = 1234, c = "hello", d = new Int64?(9999) }; + var result = a.GetNonNullProperties(); + + Assert.That(result, Has.Exactly(3).Items); + Assert.That(result.Select(t => t.Name), Does.Not.Contain("a")); } }