Skip to content

Commit 34c3fba

Browse files
authored
Merge pull request #334 from MikelThief/master
Align EnumValueObject implementations
2 parents ebaffc4 + 18ed38a commit 34c3fba

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

CSharpFunctionalExtensions.Tests/ValueObjectTests/EnumValueObjectTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ public void GivenNullOrEmptyKey_WhenCreating_ThenException(string key)
8282
[InlineData("Four", false)]
8383
[InlineData(nameof(TestEnumValueObject.Two), true)]
8484
[InlineData(nameof(TestEnumValueObject.One), true)]
85-
public void GivenPossibleKey_WhenCheckingIfKeyIsEnumValueObject_ThenShouldReturnTrueIfKeyRecognized(string possibleKey, bool isIn)
85+
public void GivenPossibleKey_WhenCheckingIfKeyIsEnumValueObject_ThenShouldReturnTrueIfKeyRecognized(string possibleId, bool isIn)
8686
{
8787
// Act
88-
var isEnumValueObject = TestEnumValueObject.Is(possibleKey);
88+
var isEnumValueObject = TestEnumValueObject.Is(possibleId);
8989

9090
// Assert
9191
isEnumValueObject.Should().Be(isIn);

CSharpFunctionalExtensions/ValueObject/EnumValueObject.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,20 @@ public static Maybe<TEnumeration> FromName(string name)
6565
? EnumerationsByName[name]
6666
: null;
6767
}
68-
68+
69+
70+
#if NET40
71+
public static ICollection<TEnumeration> All = EnumerationsById.Values.OfType<TEnumeration>().ToList();
72+
#else
73+
public static IReadOnlyCollection<TEnumeration> All = EnumerationsById.Values.OfType<TEnumeration>().ToList();
74+
#endif
75+
76+
public static bool Is(string possibleName) => All.Select(e => e.Name).Contains(possibleName);
77+
78+
public static bool Is(TId possibleId) => All.Select(e => e.Id).Contains(possibleId);
79+
80+
public override string ToString() => Name;
81+
6982
protected override IEnumerable<object> GetEqualityComponents()
7083
{
7184
yield return Id;
@@ -98,7 +111,11 @@ protected EnumValueObject(string id)
98111
Id = id;
99112
}
100113

101-
public static IEnumerable<TEnumeration> All = Enumerations.Values;
114+
#if NET40
115+
public static ICollection<TEnumeration> All = Enumerations.Values.OfType<TEnumeration>().ToList();
116+
#else
117+
public static IReadOnlyCollection<TEnumeration> All = Enumerations.Values.OfType<TEnumeration>().ToList();
118+
#endif
102119

103120
public virtual string Id { get; protected set; }
104121

@@ -139,7 +156,7 @@ public static Maybe<TEnumeration> FromId(string id)
139156
: null;
140157
}
141158

142-
public static bool Is(string possibleKey) => All.Select(e => e.Id).Contains(possibleKey);
159+
public static bool Is(string possibleId) => All.Select(e => e.Id).Contains(possibleId);
143160

144161
public override string ToString() => Id;
145162

0 commit comments

Comments
 (0)