C
C#2y ago
.tree

❔ Method call based on config file

Based on a value in a config file, I want my program to call one of 3 methods who have different parameters but overall do the same thing. What's the best way to handle this? Overloading the method and adding an additional parameter?
9 Replies
Angius
Angius2y ago
Honestly, probably just a switch or if-else
.tree
.tree2y ago
So I would just rewrite this method for the other config values regularly and call them depending on which value is used?
Angius
Angius2y ago
Based on some value in the config file, you said?
.tree
.tree2y ago
Yes
Angius
Angius2y ago
So
switch (cfg.Whatever) {
case "one":
MethodOne(1, 2, "false");
break;
case "two":
MethodTwo("hello", true, 9.19);
break;
case "three":
MethodThree(new Person(), 67, 'a');
break;
default:
throw new OutOfRangeException();
}
switch (cfg.Whatever) {
case "one":
MethodOne(1, 2, "false");
break;
case "two":
MethodTwo("hello", true, 9.19);
break;
case "three":
MethodThree(new Person(), 67, 'a');
break;
default:
throw new OutOfRangeException();
}
.tree
.tree2y ago
Seems like a good approach I suppose, thanks
atakancracker
atakancracker2y ago
And you can use IOptionsMonitor to manage changes in config file during runtime
atakancracker
atakancracker2y ago
In case you need that, I was excited when first see this and use in my app https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-7.0
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.