Skip to content

Commit

Permalink
object extensions for changed properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuck committed Sep 18, 2023
1 parent e88e34f commit 07fc1cf
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 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.2.0</Version>
<Version>6.4.0</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.2.0</Version>
<Version>6.4.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library providing domain validation fundamentals.</Description>
<Nullable>enable</Nullable>
Expand Down
21 changes: 21 additions & 0 deletions src/YuckQi.Domain/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<Type, IEnumerable<PropertyInfo>> PropertiesByType = new ();

public static Dictionary<PropertyInfo, (Object?, Object?)> GetDifferencesByProperty<T>(this T a, T b)
{
var properties = PropertiesByType.GetOrAdd(typeof(T), type => type.GetProperties());
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);

return differences;
}
}
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.2.0</Version>
<Version>6.4.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library for bootstrapping a domain model project.</Description>
<Nullable>enable</Nullable>
Expand Down
24 changes: 24 additions & 0 deletions test/YuckQi.Domain.UnitTests/Extensions/ObjectExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit 07fc1cf

Please sign in to comment.