c# - Refactoring Large Switch Statement -
I saw the following question
I am doing a code refactor that has a big switch statement ASPX Page_load event handler
code is something like
string switch value = gate switch value (); Switch (switch value) {case "switch 1": stopwatch. Restart (); HandlerForSwitchValue1 (); stopwatch.Stop (); LogDetails (); break; Case "Foo": Stopwatch. Restart (); HandlerForFoo (); stopwatch.Stop (); LogDetails (); break; ... ... ...} I am planning to create another category called 'handler' which will include string and action reps.
IDictionary & lt; String, Action & gt; Actionmaps = new dictionary & lt; String, Action & gt; (); My question is, where do I place the dictionary? If I populate it in the 'Handlers' class, then how do I use the private handler methods defined in the ASPX code?
The second option is to populate the code behind the dictionary. Something like this
handler Add ("SwitchValue 1", HandlerForSwitchWall 1); Add method or whatever I name it will add item to 'handler class'
Now I will have 100 lines that show something like this Will give What do I get from this again?
Handlers. Add ("SwitchVuel 2", HandlerForSwitchWall 2); Until the mapping changes over runtime, you start a field with a static reading dictionary as a class field and the constructor of that class. If you want to make the mapping dynamically running, you can instead start a stable property in a class that support store for that property can also be started in the class constructor.
Comments
Post a Comment