C
C#3mo ago
Saiyanslayer

✅ 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:
public class Structure {
public int Id;
public string Name;
public Property[] Properties; // each value to show/edit
public Structure[] Structures; // in the future, can also contain other Structure objects
}

public class Property<TValue> {
public int Id;
public string Name;
public T Value;
}
public class Structure {
public int Id;
public string Name;
public Property[] Properties; // each value to show/edit
public Structure[] Structures; // in the future, can also contain other Structure objects
}

public class Property<TValue> {
public int Id;
public string Name;
public T Value;
}
This would let me design a "Patient" object:
new Structure(Id:"1", Name:"Patient")
new Structure(Id:"1", Name:"Patient")
I could add properties:
var properties = {
new Property<string> {Id = 1, Name = "Patient Name", Value = "John Doe"},
new Property<string> {Id = 2, Name = "Patient Id", Value = "007"},
new Property<DateTime> {Id = 3, Name = "Patient Birthday", Value = "2024-09-22"}
}
var properties = {
new Property<string> {Id = 1, Name = "Patient Name", Value = "John Doe"},
new Property<string> {Id = 2, Name = "Patient Id", Value = "007"},
new Property<DateTime> {Id = 3, Name = "Patient Birthday", Value = "2024-09-22"}
}
What would be a good approach in EF Core to this? Table-Per-Hierarchy? How can I map the TValue generic properly?
15 Replies
Angius
Angius3mo ago
Honestly, for the kind of "whatever lmao" data I recommend just using a JSON column
Jimmacle
Jimmacle3mo ago
yeah, you could do EAV too but json is probably better
Saiyanslayer
SaiyanslayerOP3mo ago
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
Jimmacle
Jimmacle3mo ago
it doesn't, you can deserialize json to a document/node strong typing/generics and runtime configurable structures aren't particularly compatible
Saiyanslayer
SaiyanslayerOP3mo ago
you mean to trasnlate the Json column into a JsonNode and grab the info from it?
var node = JsonNode.Parse(value);
var node = JsonNode.Parse(value);
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
Jimmacle
Jimmacle3mo ago
you'll have to build support for representing those dynamically and safely in your application
Saiyanslayer
SaiyanslayerOP3mo ago
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?
Jimmacle
Jimmacle3mo ago
i mean, it depends
Saiyanslayer
SaiyanslayerOP3mo ago
ie < 25 users
Jimmacle
Jimmacle3mo ago
if it is really a business requirement for it to be this flexible you kind of have no choice
Saiyanslayer
SaiyanslayerOP3mo ago
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
Jimmacle
Jimmacle3mo ago
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
Saiyanslayer
SaiyanslayerOP3mo ago
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
Jimmacle
Jimmacle3mo ago
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
Saiyanslayer
SaiyanslayerOP3mo ago
i think i may need to keep them separate. If you change one instance of the object, doesn't affect the others.
Want results from more Discord servers?
Add your server