我有一些重复的代码:
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");
我怎样才能更好地做到这一点?
答案 0 :(得分: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");
答案 1 :(得分:0)
首先,
1- move the export and simple display functionality into 2 different methods.
2- Create a Client Object exactly once by using Singleton Pattern.
答案 2 :(得分:0)
如果你有两个案例,你可以循环迭代并跳过重复代码。只需为每次迭代用不同的数据(字符串等)创建一个var:
keys= new[]
{
"order_id",
"calculation_id",
//etc
}