需要良好的实现来使用具有相同输出格式但不同属性的多个 API

Need good implementation for consuming multiple APIs with same output format but different properties

我正在处理多个 RESTFul API,其中 returns 数据具有以下结构:

{
"total": 4,
"offset": 0,
"limit": 50,
**"data"**: [
          {
            "record_date": "2015-06-19 14:20:08",
            "user_id": "kdave@abc.com",
            "notes": "testing",
            "id": 25,
            "type": 1002
          },....
        ]
}

data”属性随 API 的不同而变化。目前,我需要在不同的命名空间下创建不同的 类 "Data" 以反序列化 API 输出。

对于以更好的架构方式处理这种情况有什么建议吗?

这是 generics 的完美问题:

public class RestResponse<TData>
{
    public int Total { get; set; }
    public int Offset { get; set; }
    public int Limit { get; set; }
    public TData[] Data { get; set; }
}

然后定义你的各种数据类。

编辑:

如果您想使用相同的反序列化代码,请考虑使用通用方法:

public RestResponse<T> DeserializeResponse<T>(...)
{
     var result = new RestResponse<T>();
     ...
     return result;
}

其他地方:

object result;
switch (...)
{
    case A:
        result = DeserializeResponse<ApiDataModelA>(...);
        break;
    ...
    default:
        throw new InvalidOperationException(...);
}