Monday, January 19, 2009
Tag-Data Pairs
Recently, someone asked me if Tag-Data pairs was a good idea. That's kind of like asking if elephants are good ideas: Sure, if you need a tree pushed over. Not so much if you need something that can fit through your front door.
As with any design question, what parameters are limiting the idea?
In general, yes... Tag-Data pairs can be a quick human-readable format that handle non-object-data efficiently. But if you need data that is hierarchical or object-oriented (as most data is), then no. If you need something more than a handful of fields, or if you need these fields to be grouped or bundled in a hierarchy, then use XML. There are a ton of tools built into the .NET platform for handling XML data.
However, in my previous job, XML was much too bloated for their Nazi-style bandwidth usage. They had to pay per byte of transfer, so each character transferred was precious. In those cases, we actually did use tag-data pairs.
It was quick, lightweight, simple and human-readable. The most annoying part of a TDP packet was that the data inside was not incredibly easy to get at. For example, if you needed to know the person's last name, you had to parse the TDPs into pairs, then loop through to find the tag that matched "LastName".
In order to fix this, we needed a lightweight solution that would convert the TDP strings into something more usable... and back again once the values were modified.
Enter the TDP class. In the end, the implementation of it was almost brainless, but the elegance was in its simplicity.
Managing the data became nearly trivial:
As with any design question, what parameters are limiting the idea?
In general, yes... Tag-Data pairs can be a quick human-readable format that handle non-object-data efficiently. But if you need data that is hierarchical or object-oriented (as most data is), then no. If you need something more than a handful of fields, or if you need these fields to be grouped or bundled in a hierarchy, then use XML. There are a ton of tools built into the .NET platform for handling XML data.
However, in my previous job, XML was much too bloated for their Nazi-style bandwidth usage. They had to pay per byte of transfer, so each character transferred was precious. In those cases, we actually did use tag-data pairs.
It was quick, lightweight, simple and human-readable. The most annoying part of a TDP packet was that the data inside was not incredibly easy to get at. For example, if you needed to know the person's last name, you had to parse the TDPs into pairs, then loop through to find the tag that matched "LastName".
In order to fix this, we needed a lightweight solution that would convert the TDP strings into something more usable... and back again once the values were modified.
Enter the TDP class. In the end, the implementation of it was almost brainless, but the elegance was in its simplicity.
public class TDP : Hashtable
{
public string Sep1 { get; set; }
public string Sep2 { get; set; }
public TDP()
{
Sep1 = "=";
Sep2 = "|";
}
public TDP (string initialString)
{
Sep1 = "=";
Sep2 = "|";
Parse (initialString);
}
public string ParseString
{
get
{
return ConvertToString();
}
set
{
Parse(value);
}
}
private void Parse(string value)
{
this.Clear();
string[] Pairs = value.Split(Sep2.ToCharArray());
foreach (string pair in Pairs)
{
string[] arr = pair.Split(Sep1.ToCharArray());
this.Add(arr[0], arr[1]);
}
}
private string ConvertToString()
{
StringBuilder sb = new StringBuilder();
foreach (string key in this.Keys)
{
sb.Append(key + Sep1 + this[key].ToString() + Sep2);
}
return sb.ToString();
}
public override string ToString()
{
return ParseString;
}
}
Managing the data became nearly trivial:
class Program
{
static void Main(string[] args)
{
TDP tdp = new TDP();
tdp.ParseString = "Cheryl=Beautiful wife|Jerry=Cheryl's Husband|Time=3:00pm|";
Console.WriteLine(tdp["Jerry"]);
tdp["Jerry"] = "Awesome programmer";
tdp["New tag"] = "Some new tag";
Console.WriteLine(tdp.ToString());
}
}
Labels: C-Sharp
Subscribe to Posts [Atom]
Post a Comment