From ede078a4b1ed1067eb80eb476f638e3898293ea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Mon, 15 Aug 2022 17:42:32 +0300 Subject: [PATCH] Added extension method: HasSameExtraProperties --- .../Data/ExtraPropertyDictionaryExtensions.cs | 27 ++++++++++++++++++- .../Abp/Data/HasExtraPropertiesExtensions.cs | 11 ++++++++ .../HasExtraPropertiesExtensions_Tests.cs | 21 +++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/ExtraPropertyDictionaryExtensions.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/ExtraPropertyDictionaryExtensions.cs index b97b0056e9..d5de504afc 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/ExtraPropertyDictionaryExtensions.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/ExtraPropertyDictionaryExtensions.cs @@ -1,4 +1,5 @@ using System; +using JetBrains.Annotations; namespace Volo.Abp.Data; @@ -26,4 +27,28 @@ public static class ExtraPropertyDictionaryExtensions extraPropertyDictionary[key] = Enum.Parse(enumType, extraPropertyDictionary[key].ToString(), ignoreCase: true); return extraPropertyDictionary[key]; } -} + + public static bool HasSameItems( + [NotNull] this ExtraPropertyDictionary dictionary, + [NotNull] ExtraPropertyDictionary otherDictionary) + { + Check.NotNull(dictionary, nameof(dictionary)); + Check.NotNull(otherDictionary, nameof(otherDictionary)); + + if (dictionary.Count != otherDictionary.Count) + { + return false; + } + + foreach (var key in dictionary.Keys) + { + if (!otherDictionary.ContainsKey(key) || + dictionary[key]?.ToString() != otherDictionary[key]?.ToString()) + { + return false; + } + } + + return true; + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs index 0bd64d55f1..db543bb67f 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Linq; +using JetBrains.Annotations; using Volo.Abp.ObjectExtending; using Volo.Abp.Reflection; @@ -121,4 +122,14 @@ public static class HasExtraPropertiesExtensions source.RemoveProperty(property.Name); } } + + public static bool HasSameExtraProperties( + [NotNull] this IHasExtraProperties source, + [NotNull] IHasExtraProperties other) + { + Check.NotNull(source, nameof(source)); + Check.NotNull(other, nameof(other)); + + return source.ExtraProperties.HasSameItems(other.ExtraProperties); + } } diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/HasExtraPropertiesExtensions_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/HasExtraPropertiesExtensions_Tests.cs index 95e222e2c7..4ad99fee1e 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/HasExtraPropertiesExtensions_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/HasExtraPropertiesExtensions_Tests.cs @@ -45,4 +45,25 @@ public class HasExtraPropertiesExtensions_Tests city.SetProperty("DateTime?", DateTime.MinValue); city.GetProperty("DateTime?").ShouldBe(DateTime.MinValue); } + + [Fact] + public void HasSameExtraProperties_Tests() + { + var adana = new City(Guid.NewGuid(), "Adana"); + adana.SetProperty("IsHot", true); + + var antalya = new City(Guid.NewGuid(), "Antalya"); + antalya.SetProperty("IsHot", true); + + adana.HasSameExtraProperties(antalya).ShouldBeTrue(); + + adana.SetProperty("IsHot", false); + adana.HasSameExtraProperties(antalya).ShouldBeFalse(); + + adana.SetProperty("IsHot", true); + adana.HasSameExtraProperties(antalya).ShouldBeTrue(); + + adana.SetProperty("Population", 3_000_000); + adana.HasSameExtraProperties(antalya).ShouldBeFalse(); + } }