From 2ccd581fa7d9e7af91b4dcd5f138ffa3ecaa28c2 Mon Sep 17 00:00:00 2001 From: Aleksbgbg Date: Wed, 19 Jun 2019 12:25:39 +0100 Subject: [PATCH] [ServiceEntry] Add hashable class that contains a service type and service key, to store in a dictionary and access by both type and key. --- Wingman.Tests/Container/ServiceEntryTests.cs | 61 ++++++++++++++++++++ Wingman/Container/ServiceEntry.cs | 33 +++++++++++ 2 files changed, 94 insertions(+) create mode 100644 Wingman.Tests/Container/ServiceEntryTests.cs create mode 100644 Wingman/Container/ServiceEntry.cs diff --git a/Wingman.Tests/Container/ServiceEntryTests.cs b/Wingman.Tests/Container/ServiceEntryTests.cs new file mode 100644 index 0000000..7536d8c --- /dev/null +++ b/Wingman.Tests/Container/ServiceEntryTests.cs @@ -0,0 +1,61 @@ +namespace Wingman.Tests.Container +{ + using Wingman.Container; + + using Xunit; + + public class ServiceEntryTests + { + [Fact] + public void TestIdenticalEntriesIdenticalHashCode() + { + ServiceEntry serviceEntry0 = new ServiceEntry(typeof(IService), "SomeKey"); + ServiceEntry serviceEntry1 = new ServiceEntry(typeof(IService), "SomeKey"); + + int hashCode0 = serviceEntry0.GetHashCode(); + int hashCode1 = serviceEntry1.GetHashCode(); + + Assert.Equal(hashCode0, hashCode1); + } + + [Fact] + public void TestNullServiceMatches() + { + ServiceEntry serviceEntry0 = new ServiceEntry(typeof(IService), null); + ServiceEntry serviceEntry1 = new ServiceEntry(typeof(IService), null); + + int hashCode0 = serviceEntry0.GetHashCode(); + int hashCode1 = serviceEntry1.GetHashCode(); + + Assert.Equal(hashCode0, hashCode1); + } + + [Fact] + public void TestNullKeyMatches() + { + ServiceEntry serviceEntry0 = new ServiceEntry(null, "SomeKey"); + ServiceEntry serviceEntry1 = new ServiceEntry(null, "SomeKey"); + + int hashCode0 = serviceEntry0.GetHashCode(); + int hashCode1 = serviceEntry1.GetHashCode(); + + Assert.Equal(hashCode0, hashCode1); + } + + [Fact] + public void TestDifferentEntriesDifferentHashCode() + { + ServiceEntry serviceEntry0 = new ServiceEntry(typeof(IService), "SomeKey"); + ServiceEntry serviceEntry1 = new ServiceEntry(typeof(IService1), "SomeKey"); + + int hashCode0 = serviceEntry0.GetHashCode(); + int hashCode1 = serviceEntry1.GetHashCode(); + + Assert.NotEqual(hashCode0, hashCode1); + } + + private interface IService { } + + private interface IService1 { } + } +} \ No newline at end of file diff --git a/Wingman/Container/ServiceEntry.cs b/Wingman/Container/ServiceEntry.cs new file mode 100644 index 0000000..46d5685 --- /dev/null +++ b/Wingman/Container/ServiceEntry.cs @@ -0,0 +1,33 @@ +namespace Wingman.Container +{ + using System; + + internal class ServiceEntry + { + private readonly Type _serviceType; + + private readonly string _key; + + internal ServiceEntry(Type serviceType, string key) + { + _serviceType = serviceType; + _key = key; + } + + public override int GetHashCode() + { + unchecked + { + const int prime1 = 17; + const int prime2 = 23; + + int hash = prime1; + + hash = (hash * prime2) + (_serviceType?.GetHashCode() ?? 0); + hash = (hash * prime2) + (_key?.GetHashCode() ?? 0); + + return hash; + } + } + } +} \ No newline at end of file