Abstraction for duplicated code [Answered]

14 Replies
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Susko3
Susko33y ago
This can work if the classes are not static, and if you do
-public static readonly string[] All = { Password, Oidc, Totp, LookupSecret, MagicLink };
+public string[] All { get; } = { Password, Oidc, Totp, LookupSecret, MagicLink };
-public static readonly string[] All = { Password, Oidc, Totp, LookupSecret, MagicLink };
+public string[] All { get; } = { Password, Oidc, Totp, LookupSecret, MagicLink };
but then you'd have to have
RuleFor(x => x.SomeString).OneOf(new IdentityState())
RuleFor(x => x.SomeString).OneOf(new IdentityState())
not sure....
ero
ero3y ago
you need .NET 7 for this
Susko3
Susko33y ago
Explore static virtual members in interfaces
This advanced tutorial demonstrates scenarios for operators and other static members in interfaces.
ero
ero3y ago
interface IInterface
{
abstract static string[] All { get; }
abstract static bool IsValid(string value);
}

class Class : IInterface
{
public static string[] All { get; } = { "foo", "bar", "baz" };
public static bool IsValid(string value) => All.Contains(value);
}
interface IInterface
{
abstract static string[] All { get; }
abstract static bool IsValid(string value);
}

class Class : IInterface
{
public static string[] All { get; } = { "foo", "bar", "baz" };
public static bool IsValid(string value) => All.Contains(value);
}
static classes still can't implement interfaces of course
amio
amio3y ago
Do you need All for something? What? Otherwise maybe something like an IValidateable/<T>
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
ero
ero3y ago
Sure
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
amio
amio3y ago
What you care about is being able to validate stuff. If "All" is only there for that purpose, it's an implementation detail and doesn't need to be exposed at all.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
amio
amio3y ago
It kind of is your whole abstraction. But maybe you should be using something like fluentvalidation instead? Either way, All doesn't need to be part of your "interface"
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord3y ago
✅ This post has been marked as answered!

Did you find this page helpful?