Skip to content

Commit 0da94ca

Browse files
committed
refactor: make record
1 parent 3dd452b commit 0da94ca

5 files changed

Lines changed: 12 additions & 23 deletions

File tree

src/LinkDotNet.Blog.Domain/Enumeration.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,22 @@
66
namespace LinkDotNet.Blog.Domain;
77

88
#pragma warning disable CA1724
9-
public abstract class Enumeration<TEnumeration>
10-
where TEnumeration : Enumeration<TEnumeration>
11-
#pragma warning restore
9+
public abstract record Enumeration<TEnumeration>(string Key)
10+
#pragma warning restore CA1724
11+
where TEnumeration : Enumeration<TEnumeration>
1212
{
13-
protected Enumeration(string key)
14-
{
15-
ArgumentException.ThrowIfNullOrWhiteSpace(key);
16-
Key = key;
17-
}
18-
1913
public static FrozenSet<TEnumeration> All { get; } = GetEnumerations();
2014

21-
public string Key { get; }
22-
23-
public static bool operator ==(Enumeration<TEnumeration>? a, Enumeration<TEnumeration>? b)
24-
=> a is not null && b is not null && a.Key.Equals(b.Key, StringComparison.Ordinal);
15+
public static bool operator ==(Enumeration<TEnumeration>? a, string? b)
16+
=> a is not null && b is not null && a.Key.Equals(b, StringComparison.Ordinal);
2517

26-
public static bool operator !=(Enumeration<TEnumeration>? a, Enumeration<TEnumeration>? b) => !(a == b);
18+
public static bool operator !=(Enumeration<TEnumeration>? a, string? b) => !(a == b);
2719

2820
public static TEnumeration Create(string key)
2921
=> All.SingleOrDefault(p => p.Key == key)
3022
?? throw new InvalidOperationException($"{key} is not a valid value for {typeof(TEnumeration).Name}");
3123

32-
public override int GetHashCode() => Key.GetHashCode(StringComparison.Ordinal);
33-
34-
public override bool Equals(object? obj) => obj?.GetType() == typeof(TEnumeration) && ((TEnumeration)obj).Key == Key;
35-
36-
public override string ToString() => Key;
24+
public sealed override string ToString() => Key;
3725

3826
private static FrozenSet<TEnumeration> GetEnumerations()
3927
{
@@ -46,3 +34,4 @@ private static FrozenSet<TEnumeration> GetEnumerations()
4634
.ToFrozenSet();
4735
}
4836
}
37+

src/LinkDotNet.Blog.Domain/ProficiencyLevel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace LinkDotNet.Blog.Domain;
22

3-
public sealed class ProficiencyLevel : Enumeration<ProficiencyLevel>
3+
public sealed record ProficiencyLevel : Enumeration<ProficiencyLevel>
44
{
55
public static readonly ProficiencyLevel Familiar = new(nameof(Familiar));
66
public static readonly ProficiencyLevel Proficient = new(nameof(Proficient));

src/LinkDotNet.Blog.Infrastructure/Persistence/PersistenceProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace LinkDotNet.Blog.Infrastructure.Persistence;
44

5-
public sealed class PersistenceProvider : Enumeration<PersistenceProvider>
5+
public sealed record PersistenceProvider : Enumeration<PersistenceProvider>
66
{
77
public static readonly PersistenceProvider SqlServer = new(nameof(SqlServer));
88
public static readonly PersistenceProvider Sqlite = new(nameof(Sqlite));

src/LinkDotNet.Blog.Web/Features/Services/FileUpload/AuthenticationMode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace LinkDotNet.Blog.Web.Features.Services.FileUpload;
44

5-
public class AuthenticationMode : Enumeration<AuthenticationMode>
5+
public sealed record AuthenticationMode : Enumeration<AuthenticationMode>
66
{
77
public static readonly AuthenticationMode Default = new(nameof(Default));
88

tests/LinkDotNet.Blog.UnitTests/Domain/TestEnumeration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace LinkDotNet.Blog.UnitTests.Domain;
44

5-
public sealed class TestEnumeration : Enumeration<TestEnumeration>
5+
public sealed record TestEnumeration : Enumeration<TestEnumeration>
66
{
77
public static readonly TestEnumeration One = new TestEnumeration(nameof(One));
88

0 commit comments

Comments
 (0)