C
C#2y ago
oe

❔ best design practice for this?

Hi everyone. I have a ConvertedRequest class that acts as an Entity Framework model for the database. Inside the class, there is an ID of type string (I want it to be alphanumeric 6 digits, not GUID). I have a method to generate this below. My question is what's the best design practice to reference this method? I am technically not gonna make more "Random" value methods, so was thinking static singleton, but the purpose of this project is to learn how to write with good design patterns
readonly Random random = new();
string RandomString(int length = 6)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray()).ToLower();
}
readonly Random random = new();
string RandomString(int length = 6)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray()).ToLower();
}
Including it in the Controller for the API just seems ugly and out of place
2 Replies
Angius
Angius2y ago
Either make it a singleton and inject it with DI, or make it an extension method of Random Or, hell, just a random static method also works Just make sure to use Random.Shared in that case, to avoid newing up the random each time
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server