From 46b2d8a4aa696f19d49dddbd57927198aff0d70f Mon Sep 17 00:00:00 2001 From: Claus Date: Mon, 14 Aug 2017 13:34:47 +0200 Subject: [PATCH] implement equality comparison to prevent duplicates in when used in dictionaries. --- src/Umbraco.Core/Services/IdkMap.cs | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/Umbraco.Core/Services/IdkMap.cs b/src/Umbraco.Core/Services/IdkMap.cs index af3e4ff50b..021f8288cb 100644 --- a/src/Umbraco.Core/Services/IdkMap.cs +++ b/src/Umbraco.Core/Services/IdkMap.cs @@ -181,12 +181,54 @@ namespace Umbraco.Core.Services { public int Id { get; set; } public UmbracoObjectTypes UmbracoObjectType { get; set; } + + protected bool Equals(Id2KeyCompositeKey other) + { + return Id == other.Id && UmbracoObjectType == other.UmbracoObjectType; + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((Id2KeyCompositeKey) obj); + } + + public override int GetHashCode() + { + unchecked + { + return (Id * 397) ^ (int) UmbracoObjectType; + } + } } internal class Key2IdCompositeKey { public Guid Key { get; set; } public UmbracoObjectTypes UmbracoObjectType { get; set; } + + protected bool Equals(Key2IdCompositeKey other) + { + return Key.Equals(other.Key) && UmbracoObjectType == other.UmbracoObjectType; + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((Key2IdCompositeKey) obj); + } + + public override int GetHashCode() + { + unchecked + { + return (Key.GetHashCode() * 397) ^ (int) UmbracoObjectType; + } + } } } } \ No newline at end of file