0

I have a C# solution that reads records from a db and processes them into another db. The records to be processed are essentially one long string that represents the data and then another string "code" that determines how that big data string should be parsed. There are dozens and dozens of different kinds of records; I have them represented as distinct concrete classes that all inherit from an abstract class.

I can generate the code to make classes for the different records easily enough. However, is there a better (i.e. less verbose, more dynamic) way to map the "code" to the class that represents it? Right now I'm just using a huge switch, something like:

BaseRecord record;
switch(code) {
    case "A":
        record = new A(stringData);
    case "B":
        record = new B(stringData);
    .
    .
    .
    case "Z":
        record = new Z(stringData);

where A, B, ..., Z all derive from BaseRecord.

Other than reflection, I can't think of a way to map string representations of class names to the classes they represent.

3
  • Other than reflection... : are you trying to avoid using introspection, and if so, why?
    – coredump
    Commented Jul 4, 2015 at 13:00
  • 3
    If the data really needs to be stored in the manner you describe (which is the first thing I'd question), then you clearly need the moral equivalent of a switch() statement somewhere in your code, so you might as well "be honest" and use an actual switch(). As long as it's done once in the DAL, where it belongs, and no other code has to know about this "code"->class mapping, it shouldn't be a problem.
    – Ixrec
    Commented Jul 4, 2015 at 13:28
  • @coredump Just trying to see what my options are. switch and reflection were the only two I could come up with.
    – Joseph
    Commented Jul 4, 2015 at 18:32

1 Answer 1

2

Well, it is possible to avoid the switch statement: You can create an abstract factory class, and have some classes inherit from it (ideally via a template argument) to produce objects of the different derived types. Then you can put all the concrete factories you need in a map and use the strings to index into that map to produce a factory for the desired dynamic type.

However, while possible, this is hardly worth doing. As far as I can see, your switch statement is mind-numbingly simple (albeit long). The abstract factory would introduce a lot of additional complexity, so the switch seems to be more compliant to the KISS principle.

Generally speaking, there's nothing bad about a switch statement, not even one with a lot of cases (as long as the cases are simple and self contained). The problem with switch statements is only when they crop up everywhere, which indicates that you should be using inheritance instead. A single switch to perform deserialization seems fine to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.