来自相似对象的智能封装

Smart Encapsulation from similar objects

我有几个 Soap 请求都以相同的方式开始: 第一个请求:

var client = new Conn.Team.z_proj_team_spQueryPortClient();
client.Endpoint.Address = EndPoint;
var query = new Conn.Team.z_proj_team_spQuery
{
    Filter = new Conn.Team.z_proj_team_spFilter
    {
        param_code = projectCode
    }
};
var auth = new Conn.Team.Auth { Username = _username, Password = _password };
var records = client.Query(auth, query).Records;

第二个请求:

var client = new Conn.Task.z_proj_Task_spQueryPortClient();
client.Endpoint.Address = EndPoint;
var query = new Conn.Task.z_proj_Task_spQuery
{
    Filter = new Conn.Task.z_proj_Task_spFilter
    {
        param_code = projectCode
    }
};
var auth = new Conn.Task.Auth { Username = _username, Password = _password };
var records = client.Query(auth, query).Records;

如您所见,代码 看起来 几乎相同,但它们是完全不同的对象(不共享基数 class)但是谁使用相同的 属性-名称。

因为我有大约10个这样的调用,有没有好的方法来包装这些函数?您甚至会建议这样做,还是 Reflection simple 会使其成为 "more evil"?

(顺便说一句:)我不确定什么标签适合我的问题。随意将它们更改为更好的

通过以下 "reflection/dynamic" 方式解决了这个问题。如果您有更好的解决方案,请随时post。

private object Connect(Type clientType, Type queryType, Type filterType, Type authType, string code)
{
    dynamic client = Activator.CreateInstance(clientType);
    client.Endpoint.Address = EndPoint;

    dynamic query = Activator.CreateInstance(queryType);
    dynamic filter = Activator.CreateInstance(filterType);
    filter.param_code = code;
    query.Filter = filter;

    dynamic auth = Activator.CreateInstance(authType);
    auth.Username = _username;
    auth.Password = _password;

    return client.Query(auth, query).Records;
}