C
C#2y ago
pyrodistic

Enum or SQL Table?

Hello, I'm doing a CRUD system for animals using MCV (.NET 5). So I thought to have AnimalClass, AnimalSpecies, and AnimalSex as properties. AnimalSpecies is going to be a SQL Table since I might want to allow the user to add species, it's going to have to depend on AnimalClass as a FK so I think the easiest way is to make AnimalClass a table as well (though the values are not supposed to change). The 3 values on the AnimalSex are fixed aswell, so I'm thinking it might make more sense as a enum? Does this make sense? I've never used enum, how would I go about using the enum in this case? Thanks in advance.
13 Replies
Binto86
Binto862y ago
Imo if it doesn't change, just use enum Im not sure what do u mean by
how would I go about using enum in this case?
pyrodistic
pyrodistic2y ago
Sorry, I've never used enum before, so I might need to read into it. But should I declare the enum on the class itself? Or on the ViewModel and then save the value as a char/string/int for example? Since I want the enum to be on a drop down list when creating or editing I'm not sure what the best way to go about it.
Binto86
Binto862y ago
You want to parse the enum values to string and vice versa
Mayor McCheese
Animal gender wouldn’t necessarily be fixed, so may need to support multiple values
Binto86
Binto862y ago
You want to display the enum as strings in the dropdown and have the actual value of the enum bind to the option on the dropdown
pyrodistic
pyrodistic2y ago
AFAIK there can only be M/F or other (as in both or none - sex, not gender). Ok, I was overcomplicating, that makes sense. Thank you!
Binto86
Binto862y ago
Np
Mayor McCheese
Generally true, but some animals can change gender ( still the same amount ), but few are more complex than that Regardless you can stick with the mfn for this without issue
pyrodistic
pyrodistic2y ago
Since this is for an hypothetical pet clinic those animals are rarely seen. But that's absolutely true.
Mayor McCheese
But if you’re using display values on a site too I’d skip traditional enums unless it’s a school assignment, enumeration classes might be better https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types
pyrodistic
pyrodistic2y ago
That's seems like a good option! Gonna read into that - thank you!
Mayor McCheese
Nb: it’s going to simply some things, but make ef serialization harder
Binto86
Binto862y ago
Btw @pyrodistic don't forget to close the thread