Indeed
Indeed
Explore posts from servers
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
Thank you! Looks nice Quick question though, Do you happen to know if there is a way to add a dapper anonymous parameter converter to it so I do not have to map these enums into values before sending them in a query?
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
for reference:
[Intellenum(typeof(string))]
public partial class CustomerType {
public static readonly CustomerType Basic = new ("Basic");
}
[Intellenum(typeof(string))]
public partial class CustomerType {
public static readonly CustomerType Basic = new ("Basic");
}
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
I'll try Intellenum for now, but I am open to suggestions
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
and have also explained my usage and why it might not necessarily work with my current setup
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
I apologize however I do not believe we understand each other, as I have already mentioned this solution
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
It can be better for optimization and suiting one's specific needs without fighting with EFCore
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
Different people, different needs Some people need fine-grained queries for their needs
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
public async Task<Result<IEnumerable<AuthTokenDTO>>> GetTokensByUser(int idUser, AuthTokenStatus[]? statuses = null, AuthProvider[]? providers = null) {
statuses ??= [AuthTokenStatus.ACTIVE];


Result<IEnumerable<AuthTokenDTO>> tokens = await Result.Try(() => {
string query = """
SELECT id, idUser, provider, authToken, createdAt, updatedAt, status
FROM crud.auth
WHERE idUser = @idUser
AND status IN (@statuses)
""";

if (providers is { Length: > 0 }) {
query += " AND provider IN (@providers)";
}

return this._context.GetConnection().QueryAsync<AuthTokenDTO>(query, new {
providers,
statuses,
idUser
});
});

if (tokens.IsFailed) {
return Result.Fail("Failed to fetch auth tokens").WithErrors(tokens.Errors);
}

return tokens;
}
public async Task<Result<IEnumerable<AuthTokenDTO>>> GetTokensByUser(int idUser, AuthTokenStatus[]? statuses = null, AuthProvider[]? providers = null) {
statuses ??= [AuthTokenStatus.ACTIVE];


Result<IEnumerable<AuthTokenDTO>> tokens = await Result.Try(() => {
string query = """
SELECT id, idUser, provider, authToken, createdAt, updatedAt, status
FROM crud.auth
WHERE idUser = @idUser
AND status IN (@statuses)
""";

if (providers is { Length: > 0 }) {
query += " AND provider IN (@providers)";
}

return this._context.GetConnection().QueryAsync<AuthTokenDTO>(query, new {
providers,
statuses,
idUser
});
});

if (tokens.IsFailed) {
return Result.Fail("Failed to fetch auth tokens").WithErrors(tokens.Errors);
}

return tokens;
}
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
the point is I later deserialize them in dapper into values to use in queries
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
most go for static readonly strings
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
you're talking about Enumerables
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
there are many ways to go on about it, which is currently proposed to be the best one
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
etc.
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
some library handling it in compiletime
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
public class CardType
: Enumeration
{
public static CardType Amex = new(1, nameof(Amex));
public static CardType Visa = new(2, nameof(Visa));
public static CardType MasterCard = new(3, nameof(MasterCard));

public CardType(int id, string name)
: base(id, name)
{
}
}
public class CardType
: Enumeration
{
public static CardType Amex = new(1, nameof(Amex));
public static CardType Visa = new(2, nameof(Visa));
public static CardType MasterCard = new(3, nameof(MasterCard));

public CardType(int id, string name)
: base(id, name)
{
}
}
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
some Enumeration abstract class like so
public abstract class Enumeration : IComparable
{
public string Name { get; private set; }

public int Id { get; private set; }

protected Enumeration(int id, string name) => (Id, Name) = (id, name);

public override string ToString() => Name;

public static IEnumerable<T> GetAll<T>() where T : Enumeration =>
typeof(T).GetFields(BindingFlags.Public |
BindingFlags.Static |
BindingFlags.DeclaredOnly)
.Select(f => f.GetValue(null))
.Cast<T>();

public override bool Equals(object obj)
{
if (obj is not Enumeration otherValue)
{
return false;
}

var typeMatches = GetType().Equals(obj.GetType());
var valueMatches = Id.Equals(otherValue.Id);

return typeMatches && valueMatches;
}

public int CompareTo(object other) => Id.CompareTo(((Enumeration)other).Id);

// Other utility methods ...
}
public abstract class Enumeration : IComparable
{
public string Name { get; private set; }

public int Id { get; private set; }

protected Enumeration(int id, string name) => (Id, Name) = (id, name);

public override string ToString() => Name;

public static IEnumerable<T> GetAll<T>() where T : Enumeration =>
typeof(T).GetFields(BindingFlags.Public |
BindingFlags.Static |
BindingFlags.DeclaredOnly)
.Select(f => f.GetValue(null))
.Cast<T>();

public override bool Equals(object obj)
{
if (obj is not Enumeration otherValue)
{
return false;
}

var typeMatches = GetType().Equals(obj.GetType());
var valueMatches = Id.Equals(otherValue.Id);

return typeMatches && valueMatches;
}

public int CompareTo(object other) => Id.CompareTo(((Enumeration)other).Id);

// Other utility methods ...
}
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
What is the currently agreed upon standard
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
My point is how does one go about implementing custom string enums in c#
28 replies
CC#
Created by Indeed on 6/3/2024 in #help
String enums - current consensus
I understand that
28 replies