Skip to content

Commit

Permalink
added a method to get properties that are not null
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuck committed Sep 19, 2023
1 parent 07fc1cf commit f9a2b49
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/YuckQi.Domain.Services/YuckQi.Domain.Services.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>6.4.0</Version>
<Version>6.4.1</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library for bootstrapping a domain services project.</Description>
<Nullable>enable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>6.4.0</Version>
<Version>6.4.1</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library providing domain validation fundamentals.</Description>
<Nullable>enable</Nullable>
Expand Down
20 changes: 17 additions & 3 deletions src/YuckQi.Domain/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,24 @@ public static class ObjectExtensions

public static Dictionary<PropertyInfo, (Object?, Object?)> GetDifferencesByProperty<T>(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<PropertyInfo, (Object?, Object?)>(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<PropertyInfo> GetNonNullProperties<T>(this T a)
{
var properties = GetObjectProperties(typeof(T));
var values = properties.Select(t => new KeyValuePair<PropertyInfo, Object?>(t, t.GetValue(a)));
var result = values.Where(t => t.Value != null).Select(t => t.Key).ToHashSet();

return result;
}

private static IEnumerable<PropertyInfo> GetObjectProperties(Type type)
{
return PropertiesByType.GetOrAdd(type, t => t.GetProperties());
}
}
2 changes: 1 addition & 1 deletion src/YuckQi.Domain/YuckQi.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>6.4.0</Version>
<Version>6.4.1</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library for bootstrapping a domain model project.</Description>
<Nullable>enable</Nullable>
Expand Down
23 changes: 17 additions & 6 deletions test/YuckQi.Domain.UnitTests/Extensions/ObjectExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using NUnit.Framework;
using YuckQi.Domain.Extensions;

Expand All @@ -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"));
}
}

0 comments on commit f9a2b49

Please sign in to comment.