-6

我有一些重复的代码:

var client = new RSClient.RSClient();

var Params = new RsParameter[1];
Params[0] = new RsParameter {Key = "order_id", Value = orderId};
var result = client.GetPreparedReportSimple(login, password, "CREDO_ORDER", Params);

var client = new RSClient.RSClient();

var Params = new RsParameter[2];
Params[0] = new RsParameter {Key = "calculation_id", Value = calculationId};
Params[1] = new RsParameter {Key = "calculation_date", Value = calculationDate};
var result = client.GetPreparedReportSimpleExport(login, password, "CREDO_RSV_ACTIVE", Params, "XLS");

我怎样才能做得更好?

4

3 回答 3

0

您可以重构代码并提取方法。它会是这样的:

public __?___ Method(string login, string password, string something, string[] parameters, string extra = null) {
    var client = new RSClient.RSClient();

    if (extra == null)
        return client.GetPreparedReportSimple(login, password, something, parameters);
    else
        return client.GetPreparedReportSimple(login, password, something, parameters, extra);
}

一旦你拥有它,你可以这样称呼它:

var Params = new [] 
    {
        new RsParameter {Key = "order_id", Value = orderId} 
    };
var result = Method(login, password, "CREDO_ORDER", Params);

....

var Params = new [] 
    {
        new RsParameter {Key = "calculation_id", Value = calculationId},
        new RsParameter {Key = "calculation_date", Value = calculationDate}
    };
var result = Method(login, password, "CREDO_ORDER", params, "XLS");
于 2013-02-27T09:34:18.697 回答
0

第一件事,

 1- move the export and simple display functionality into 2 different methods.
 2- Create a Client Object exactly once by using Singleton Pattern.
于 2013-02-27T09:36:00.573 回答
0

如果您有超过两种情况,您可以执行循环来迭代和跳过重复代码。只需为每次迭代使用不同的数据(字符串等)创建一个 var:

keys= new[]
{
    "order_id",
    "calculation_id",
    //etc
}
于 2013-02-27T09:38:03.783 回答