4 Replies - 328 Views - Last Post: 20 May 2020 - 11:37 AM Rate Topic: -----

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 317
  • Joined: 25-May 16

Simplify Switch Case from Database

Posted 19 May 2020 - 09:17 AM

Hi everyone, I am trying to reduce cluttered/clunky code and am perplexed about how to handle this situation.

We have a database with certain values and each value is meant to do something different.

When the project was small, it was easy enough to put in a simple switch statement that says "when it's this, then this case hits, etc...."

For example
switch (process.JobCode)
{
 case Constants.JobCode.Job1:
 // do something here
 break;
 case Constants.JobCode.Job2:
 // do job 2 stuff here
 break;
 case Consants.JobCode.Job3:
 // do job 3 stuff here
 break;
 default:
 break;

// and this process is going to get bigger as we add new job codes in the database while they each have a different purpose
}



Is there a way for me to simplify this so that this does not turn into an ever-growing switch-case block?

All of these cases do something different but also do some of the same things as well.

Has anyone run into something similar to this before?

Is This A Good Question/Topic? 0
  • +

Replies To: Simplify Switch Case from Database

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15780
  • View blog
  • Posts: 63,237
  • Joined: 12-June 08

Re: Simplify Switch Case from Database

Posted 19 May 2020 - 09:32 AM

Usually a few different ways, but it's a little hard to estimate which would best work for you.

One option is a lookup table.

An alternative is use polymorphism and chuck things into more detailed objects. Typically a large amount of refactoring so a little rough.

Don't discount that in some cases a switch will be best done with a switch.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7444
  • View blog
  • Posts: 25,075
  • Joined: 05-May 12

Re: Simplify Switch Case from Database

Posted 19 May 2020 - 06:50 PM

In object oriented programming, a switch statement is a code smell that suggest you should be looking at the strategy pattern (and polymorphism). That adage falls apart in the face of dealing with relational databases, unless you decide to add another layer of complexity like a chain of responsibility and/or a command pattern to give each strategy a chance to detect if it is the one to be kicked off. To me, if the set of choices is finite and only changes from build to build, keep the switch statement and skip the complexity of the chain of responsibility or command pattern. On the other hand, if the set of strategies is dynamic or configurable, or if the various owners of each strategy is working at a different release schedule, then the overhead of implementing the chain maybe worth it.

Speaking of configuration, is it possible to just have each key value and the associated method name for the action be stored in a configuration file or a database?
Was This Post Helpful? 1
  • +
  • -

#4 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 317
  • Joined: 25-May 16

Re: Simplify Switch Case from Database

Posted 20 May 2020 - 10:25 AM

That is something we could look into doing.

We also have an ever-growing constructor for dependency injection as well since each of those cases may need to use a different implementation method. I was thinking of finding a clever way to use a delegate but I don't see how that would be possible when things will be different with regards to differences in logic. The only thing that is 100% the same is the logging implementation that is being repeated each and every time (we might be able to create a helper method for that).
Was This Post Helpful? 0
  • +
  • -

#5 DarenR   User is offline

  • D.I.C Lover

Reputation: 705
  • View blog
  • Posts: 4,555
  • Joined: 12-January 10

Re: Simplify Switch Case from Database

Posted 20 May 2020 - 11:37 AM

what are you injecting into a database that you need to create constructors for it? in databases you can have triggers which will update tables accordingly
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1