简体   繁体   English

有关如何清理此API的建议

[英]Suggestions for how to clean up this API

For a fun project I'm trying to implement the BitTorrent spec, and right now I'm working on the BEncoding portion of it. 对于一个有趣的项目,我正在尝试实现BitTorrent规范,现在我正在研究它的BEncoding部分。

The encoding basically can encode from int/string/dictionary -> string for transmission. 编码基本上可以从int / string / dictionary - > string进行编码以进行传输。 I've got all of the different encodings written/tested/working as overloaded Encode(...) methods and I've got the individual decode methods written/tested/working as DecodeString(...), DecodeInt(...) etc. 我已经将所有不同的编码编写/测试/工作为重载的Encode(...)方法,并且我已经将各个解码方法编写/测试/工作为DecodeString(...),DecodeInt(... )等

I can't figure out a way to have 1 Decode method for all decodings, in order to keep the API for the encoding/decoding as clean as possible (2 public methods, tops, for the time being). 我无法找到一种方法为所有解码都有一个Decode方法,以便尽可能保持编码/解码的API尽可能干净(目前有2种公共方法,tops)。

Note that I have a method that can get the type of result that the decoded string will have. 请注意,我有一个方法可以获得解码后的字符串将具有的结果类型。

Client code, right now would have to look something like this every time they want to decode a message: 客户端代码,现在每次要解码消息时都必须看起来像这样:

string s = ...; // Encoded string
Type t = Encoder.GetDecodedType(s);
if (t == typeof(int))
    process(Encoder.DecodeInt(s));
else if (t == typeof(string))
    process(Encoder.DecodeString(s));
else if (t == typeof(Dictionary<string, string>))
    process(Encoder.DecodeStringDictionary(s));
else if (t == typeof(Dictionary<string, int>))
    process(Encoder.DecodeIntDictionary(s)):

and I'd like to be able to clean that up to be more like: 而且我希望能够清理它更像是:

string s = ...; // Encoded string
process(Encoder.Decode(s));

where, in both cases the process(...) would likely be overloaded functions at the client end taking the 4 types of decoded values. 其中,在两种情况下,进程(...)可能是客户端的重载函数,采用4种类型的解码值。

You could let the DLR do this for you. 你可以让DLR为你做这件事。

public static void Process(int i) { ... }
public static void Process(string s) { ... }
public static void Process(Dictionary<string, string> dic) { ... }
public static void Process(Dictionary<string, int> dic) { ... }

[...]

public dynamic Decode(string input)     // or 'object' if you prefer
{
    var t = GetDecodedType(input);
    if (t == typeof(int))
        return DecodeInt(input);
    else if (t == ...)
        // ...
}

[...]

string s = ...; // Encoded string
Process(Encoder.Decode(s));            // if you used 'dynamic' above
Process((dynamic)Encoder.Decode(s));   // if you used 'object' above

If you are writing a library/framework ... this will be the most invaluable resource in your endeavor :-) I have the hardcopy and read it cover to cover: 如果您正在编写一个库/框架......这将是您努力的最宝贵的资源:-)我有硬拷贝并阅读它的封面以涵盖:
Design Guidelines for Developing Class Libraries from Microsoft 从Microsoft 开发类库的设计指南

I would say that you should follow the Liskov Substitution Principle here and create a method for each data type. 我会说你应该遵循Liskov Substitution Principle 并为每种数据类型创建一个方法。 That way you don't keep adding to the misery of utilizing typeof when you start passing custom objects. 这样,当你开始传递自定义对象时,你不会继续增加使用typeof的痛苦。 After reading over your question again, you already know the type being passed to it, so that further supports the need to remove the typeof operation 在再次阅读了您的问题后,您已经知道传递给它的类型,因此进一步支持删除typeof操作的需要

I'm confused. 我糊涂了。 why not simply perform the GetDecodedType logic in the public Decode method and determine the type and then make the varying calls once determined? 为什么不简单地在公共Decode方法中执行GetDecodedType逻辑并确定类型,然后在确定后进行变化调用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM