Parsing Columns Like A Ninja

Written By John Sonmez

How many times have you written this code? :

Or some code like it.  It is pretty common to parse a line and then take each column and store it in your object as data.

One of the annoying problems is that if you have optional columns you have to check to see if they are there before you can parse them.

You’re also repeating your code to strip the quotes off, or whatever other preprocessing you are doing, all over the place.

I know you can use a data driven approach to specify column to property mappings, but I wanted a really low tech, simple solution.

I finally came up with one using one of my favorite C# constructs.

Action<> is superb for solving these kinds of problems

See if this code makes you feel any better:

It may not seem like much.  It is not really a reduction in code, but we have done a few important things here.

  • Removed the explicit handling of optional columns, since we are now only populating columns that exist.  (Adding a new optional column is as easy as adding one more line to the list.)
  • Removed the responsibility from the code of explicitly tracking the column numbers.  Column number mapping now is implicit by the order of the columns in the list.
  • Removed the hidden code duplication of having calls to StripQuotes repeated for each column.
  • Separated the mapping of properties to columns from the assignment of them.

That last point deserves a little more explanation.  Why do we care if we have separated the mapping of properties to columns from the assignment?

The answer is not obvious until you try and use this same code to handle a different set of columns, or columns in a different order.

By separating out the mapping, we can pass the assignment code a different set of mappings, and it will still work.

This allows us to reuse the logic we have in the assignment of the columns to properties instead of rewriting it for other column to property mappings or orderings.