✅ Designing a configurable object in EF Core
Hi everyone!
My goal is to have an object for a web project where you can configure the fields it has:
This would let me design a "Patient" object:
I could add properties:
What would be a good approach in EF Core to this? Table-Per-Hierarchy? How can I map the TValue generic properly?
15 Replies
Honestly, for the kind of "whatever lmao" data I recommend just using a JSON column
yeah, you could do EAV too but json is probably better
would a complex type be worth exploring?
but i assumed it'd be messy since it's a generic
and if i use json, I'll have to add a column for what the generic is? that way, we can convert from json?
since deserializing needs a type
it doesn't, you can deserialize json to a document/node
strong typing/generics and runtime configurable structures aren't particularly compatible
you mean to trasnlate the Json column into a JsonNode and grab the info from it?
I'm concerned because the types could be a bit complex, like a checkbox list type or one that contains multiple classes
a dropdown menu type that contains a string array and an int index for the current selection
you'll have to build support for representing those dynamically and safely in your application
kk, will that make me regret doing it in the future, or is it manable? Maybe not the best way, but still works for small scale?
i mean, it depends
ie < 25 users
if it is really a business requirement for it to be this flexible you kind of have no choice
ok thanks. Is this overall how you'd approach it? The business need is bascially being able to make custom TODO lists
the list header values are essentially set when the list is made, but the the to-do tasks have to handle diverse data types
im not sure if I'm being clear enough
yeah, you just have to find a method of abstraction that balances between providing that flexibility and being a maintenance nightmare
for few users i'd probably lean towards being less flexible and adding support for new user data types as needed
EF Core isn't really designed to work with a dynamic schema so your options are mostly things that don't involve user defined types changing the database structure
right, which basically limit me to a json column
from what I'm seeing, I'll need to have the json value have the info of what the json value (either enum or the class type) to construct the class again.
for example, to convert json to a Checkbox[] value I'm thinking I'd have a field that returns the type Checbox[] that gets stored in the json string
yeah, i'd have some way to define that particular type of checkbox set and then refer to its definition along with the value for any particular instance
i wouldn't store the entire definition in each place that set of checkboxes is used, that would be redundant
unless you don't plan for them to be reused
i think i may need to keep them separate. If you change one instance of the object, doesn't affect the others.