C#C
C#3y 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();
        }

Including it in the Controller for the API just seems ugly and out of place
Was this page helpful?