-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ServiceEntry] Add hashable class that contains a service type and se…
…rvice key, to store in a dictionary and access by both type and key.
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} | ||
} |