Let's say my program is always passed a string of characters that represents a data table in Wiki syntax. Something like:
假设我的程序总是传递一串字符,表示Wiki语法中的数据表。就像是:
{||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.
我不希望每个特定的Wiki表对象(例如,ClientContactTable,CustomerOrderTable等)知道如何解析|和 - 和}。因此,我想编写一大块代码,将字符串解析为一些“中间”层数据对象,我可以将其传递给每个特定Wiki表对象的构造函数。
My question is: what should that middle tier object be?
我的问题是:中间层对象应该是什么?
4 个解决方案
#1
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.
您希望实现System.Runtime.IFormatter以创建通用的WikiTable序列化格式化程序。为了帮助您解决这个问题,您可以继承System.Runtime.Formatter基类。
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.
此外,如果您更喜欢XmlSerializer并且使调用者将预期的输出类型传递给构造函数,则此代码可能会更容易编写。然后,如果需要,您可以使用反射来获取您期望的属性。
#2
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.
这不是特别的OO,但它会完成这项工作,除非有一些我不知道的工作。
#3
I would use a Dictionary or Hash table.
我会使用Dictionary或Hash表。
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.
如果msdn文档难以阅读(总是适合我),我会给出一个快速概述。基本上,字典将对象(字符串?)作为键,将另一个对象作为数据。
Dictionary<string,string> d = new Dictionary<string,string>();
d["key1"] = "value";
d["anotherkey"] = "anothervalue";
#4
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.
正如其他人所说,如果你想让名字作为一个键,使用一个关联映射(string-> string)。那应该是最干净的方式;这意味着您的解析代码可以包含许多输入检查和错误处理,否则将为每个对象类重复。
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.
如果绝对必须包含多个具有相同名称的条目(如同一个客户端的多个联系人),则可以回退到(字符串,字符串)对的列表。
#1
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.
您希望实现System.Runtime.IFormatter以创建通用的WikiTable序列化格式化程序。为了帮助您解决这个问题,您可以继承System.Runtime.Formatter基类。
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.
此外,如果您更喜欢XmlSerializer并且使调用者将预期的输出类型传递给构造函数,则此代码可能会更容易编写。然后,如果需要,您可以使用反射来获取您期望的属性。
#2
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.
这不是特别的OO,但它会完成这项工作,除非有一些我不知道的工作。
#3
I would use a Dictionary or Hash table.
我会使用Dictionary或Hash表。
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.
如果msdn文档难以阅读(总是适合我),我会给出一个快速概述。基本上,字典将对象(字符串?)作为键,将另一个对象作为数据。
Dictionary<string,string> d = new Dictionary<string,string>();
d["key1"] = "value";
d["anotherkey"] = "anothervalue";
#4
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.
正如其他人所说,如果你想让名字作为一个键,使用一个关联映射(string-> string)。那应该是最干净的方式;这意味着您的解析代码可以包含许多输入检查和错误处理,否则将为每个对象类重复。
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.
如果绝对必须包含多个具有相同名称的条目(如同一个客户端的多个联系人),则可以回退到(字符串,字符串)对的列表。