C
C#7mo ago
Saiyanslayer

Blazor Design: Expression Service That Depends on other Objects

For a Blazor project, I have a group of Expression components that calculates values based on text input and referencing each other for values. Users can use a tag like [value1] to reference other components. For example,
var formula = "[value] + [value2]";
var formula = "[value] + [value2]";
I'm considering using a service to handle this
public List<Component> Components {get;set;}
...
public OnComponentInitializing(Component reference){
Component.Add(reference);
}

public OnComponentDisposal(Component reference){
Component.Remove(reference);
}
...

public double GetValue(string tag){
//converts tag into a label
//search list for a component that matches the label
//if found returns the value
}
public List<Component> Components {get;set;}
...
public OnComponentInitializing(Component reference){
Component.Add(reference);
}

public OnComponentDisposal(Component reference){
Component.Remove(reference);
}
...

public double GetValue(string tag){
//converts tag into a label
//search list for a component that matches the label
//if found returns the value
}
What I'm worried about is if I have a directory page to search groups, I could end up loading dozens of this service and it could cause issues. Question: do I have the right approach? Can I use a service to have a group of components to register with it to be easily searched and not have it balloon into a mess? Or should the service be like a singleton and have a single instance and the grouped expressions register and then handle referencing? If not, what would be a cleaner approach? Alternatively, I was thinking of making a Parent reference in each of the expression components that would reference the group. Then the group would have a method to handle the expression.
3 Replies
Joschi
Joschi7mo ago
Could you elaborate a bit on what you are exactly trying to accomplish? What exactly should the user be able to do?
Saiyanslayer
SaiyanslayerOP7mo ago
They should be able to enter in formulas in a text field that can reference other formulas or number fields and use them as parameters so you have a number field that has "400" in it and this component could have "10 * [other component]" = 4000
Joschi
Joschi7mo ago
And they define the names and values of those other components? Maybe a single service that manages a dictionary<string, component> would suffice
Want results from more Discord servers?
Add your server