views:

163

answers:

4

Let's say my program is always passed a string of characters that represents a data table in Wiki syntax. Something like:

  {||Client:||ABC|-|Contact:||Joe Smith|-|Current revision:||1.0||}

I don't want each one of my specific Wiki table objects (e.g., ClientContactTable, CustomerOrderTable, etc.) to know how to parse out | and - and }. So I'd like to write 1 chunk of code that parses the string into some "middle" tier data object that I can pass to the constructor of each specific Wiki table object.

My question is: what should that middle tier object be?

A: 

I'd go with a map from strings to strings.

It's not particularly OO, but it'll do the job, unless there's something about the job I don't know.

Jonas Kölker
A: 

I would use a Dictionary or Hash table.

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

In case the msdn docs are hard to read (always were for me), I'll give a quick overview. Basically, the dictionary takes an object (a string?) as a key, and another object for data.

Dictionary<string,string> d = new Dictionary<string,string>();
d["key1"] = "value";
d["anotherkey"] = "anothervalue";
+3  A: 

You want to implement System.Runtime.IFormatter to create a generic WikiTable serialization formatter. To help you out with this, you can inherit from the System.Runtime.Formatter base class.

Once you've done that, you would use it like this:

Stream msg = GetWikiClientContactTableMessage();
WikiTableFormatter wtf = new WikiTableFormatter(); // nice name ;)
ClientContactTable result = (ClientContactTable)wtf.Deserialize(msg);

Additionally, this code will probably be a little easier to write if you do it more like the XmlSerializer and make the caller pass the expected output type to the constructor. Then you can use reflection to get the properties you're expecting if you need to.

Joel Coehoorn
+1 also for wtf as a variable name. Nice Joel :)
Pure.Krome
A: 

As others have said, if you want the first name to act as a key, use an associative map (string->string). That should be the cleanest way; it means your parsing code can include a lot of the input checking and error handling that would otherwise be duplicated for each object class.

If you absolutely must include multiple entries with the same name (as in, multiple Contact's for the same Client), you can fall back to a list of (string,string) pairs.

comingstorm