1

I have a console app, the central process of which is to get the next line from a DB table, and based on a field value, run one of several bits of code which all return the same type o object. I could have a switch or if statement type arrangement, which does something like (pseudo code):

if exhibitType == "Summary" then AddSummaryExhibit
elseif exhibitType == "Timeline" then AddTimelineExhibit

... etc. Almost like the routing setup in MVC websites. This program does follow an approximate MVC type structure, with each method getting data from a model, rendering HTML based on a .cshtml razor view and appending it to a collection of 'sections' that belong to a HTML document container.

Ideally it'd be nice just to add a new method of name x to my project and then allow x to be added as a new value in the db table without having this manually maintained set of conditionalities. I was thinking maybe delegates or somesuch might apply here, but that's just a thought. Any tips on best practice would be appreciated.

Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
Glinkot
  • 2,924
  • 10
  • 42
  • 67

1 Answers1

0

I believe you are looking for has already been answered here in the thread talking about long switch statements: How to avoid long switch ..need help refactoring

In effect, you are looking for a gang of four pattern. The patterns that come to mind are the strategy pattern, state pattern and chain of responsibility pattern.

These patterns can be quite challenging to think about how they would work for your requirements, but its a huge step in a good direction by identifying a need for modularity and that you aren't getting it from your switch statement.

Community
  • 1
  • 1
Dessus
  • 2,147
  • 1
  • 14
  • 24
  • Thanks Dessus. I've moved them out to separate methods and will look into those patterns. Cheers – Glinkot Dec 20 '11 at 07:33