如何避免重复代码?

时间:2011-05-19 17:47:39

标签: c# asp.net code-reuse

我还是很喜欢编程,我注意到我正在重复代码:

protected void FillTradeSetups()
{
    DBUtil DB = new DBUtil();
    DataTable dtTradeSetups;

    dtTradeSetups = DB.GetTradeSetups();
    ddlSetups.DataValueField = "tradeSetupId";
    ddlSetups.DataSource = dtTradeSetups;
    ddlSetups.DataBind();
}

protected void FillTimeFrames()
{
    DBUtil DB = new DBUtil();
    DataTable dtTimeFrames;

    dtTimeFrames = DB.GetTimeFrames();
    ddlTimeFrames.DataValueField = "tfCode";
    ddlTimeFrames.DataSource = dtTimeFrames;
    ddlTimeFrames.DataBind();
} 

protected void FillTradeGrades()
{
    DBUtil DB = new DBUtil();
    DataTable dtTradeGrades;

    dtTradeGrades = DB.GetTradeGrades();
    ddlTradeGrades.DataValueField = "tradeGrade";
    ddlTradeGrades.DataTextField = "descr";
    ddlTradeGrades.DataSource = dtTradeGrades;
    ddlTradeGrades.DataBind();
}

protected void FillExecutionGrades()
{
    DBUtil DB = new DBUtil();
    DataTable dtExecutionGrades;

    dtExecutionGrades = DB.GetExecutionGrades();
    ddlExecutionGrades.DataValueField = "executionGrade";
    ddlExecutionGrades.DataTextField = "descr";
    ddlExecutionGrades.DataSource = dtExecutionGrades;
    ddlExecutionGrades.DataBind();
} 

我怎么能更聪明一点?你能帮我重新编写代码,这样就不会重复了吗?

更新

哇,谢谢你的回复,我想我会发布我正在考虑实施的内容。我还创建了另一个小工作者来删除其他一些丑陋的重复代码。你怎么看待这个?

void FillDropDownList(DropDownList ddl, DataTable dt, string dataValueField, string dataTextField, string defValue)
{
    ddl.DataValueField = dataValueField;
    ddl.DataSource = dt;
    if (!string.IsNullOrEmpty(dataTextField))
    {
        ddl.DataTextField = dataTextField;
    }

    ddl.DataBind();
    ddl.SelectedValue = defValue;
}

private string GetTradeItem(DataTable tradeDetails, string attribute)
{
    return tradeDetails.Rows[0][attribute].ToString();
}

然后用类似的东西来调用它:

int tradeId = int.Parse(Request.QueryString["tradeId"]);
DBUtil DB = new DBUtil();
DataTable tradeDetails = DB.GetTrade(tradeId);
FillDropDownList(ddlTradeGrades, DB.GetTradeGrades(), "tradeGrade", "descr", GetTradeItem(tradeDetails, "tradeGrade"));

当丑陋变成更优雅的东西时,编码感觉很棒。

7 个答案:

答案 0 :(得分:8)

这样的事,也许?

void SetupDataSource(DropDownList ddl, DataTable dt, string dataValueFieldm, string dataTextField)
{
    if (ddl != null)
    {
        ddl.DataValueField = dataValueField;
        ddl.DataSource = dt;
        if (!string.IsNullOrEmpty(dataTextField)) 
        {
            ddl.DataTextField  = dataTextField;
        }

        ddl.DataBind();
    }
    else
    {
       throw new ArgumentNullException("ddl");
    }
}

答案 1 :(得分:7)

FWIW,或许类似于:

void BindControl(DropDownList ddl, string valueField, string textField, DataTable data) {
    ddl.DataValueField = valueField;
    ddl.DataTextField = textField ?? valueField; // textField can be null
    ddl.DataSource = data;
    ddl.DataBind();
}

DBUtil DB = new DBUtil();
BindControl(ddlTradeGrades, "tradeGrade", "descr", DB.GetTradeGrades());
...

然而,这种管道本身并不是非常重复,应该考虑将来修改/维护的容易程度。

快乐的编码。

答案 2 :(得分:2)

创建一个设置所需内容的方法:

void SetValues(DropDownList ddl, string datavalue, string text, object ds)
{
    ddl.DataValueField = dataValue;
    ddl.DataTextField = text;
    ddl.DataSource = ds;
    ddl.DataBind();
}

然后你可以用:

来调用它
SetValues(ddlTradeGrades, "tradeGrade", "descr", dtTradeGrades);

答案 3 :(得分:2)

如下:

void FillData(DataTable dataSource, DropDownList ddl, string dataValueField, string dataTextField)
{
    ddl.DataSource = dt;
    ddl.DataValueField = dataValueField;
    ddl.DataTextField = dataTextField;
    ddl.DataBind();
}

然后你可以这样称呼它:

FillData(DB.GetTradeSetups(), ddlSetups, "tradeSetupId", string.Empty)

答案 4 :(得分:1)

您可以这样做:

   private void BindMyLists()
    {
        DBUtil DB = new DBUtil();
        BindDropDownList(this.ddlExecutionGrades, DB.GetExecutionGrades(), "executionGrade", "descr");
        BindDropDownList(this.ddlTradeGrades, DB.GetTradeGrades(), "tradeGrade", "descr");
        //etc
    }
    protected void BindDropDownList(DropDownList dropDownList, DataTable dataTable, string dataValueField, string dataTextField)
    {
        dropDownList.DataValueField = dataValueField;
        dropDownList.DataTextField = dataTextField;
        dropDownList.DataSource = dataTable;
        dropDownList.DataBind();
    }

答案 5 :(得分:1)

答案 6 :(得分:1)

可能是这样......

protected void FillDdl(DropDownList ddl, string dataValueField, Func<DataTable> dataTableMethod)
{
    FillDdl(ddl, dataValueField, null, dataTableMethod);
}

protected void FillDdl(DropDownList ddl, string dataValueField, string dataTextField, Func<DataTable> dataTableMethod)
{
    DataTable dt = dataTableMethod();

    ddl.DataSource = dt;

    ddl.DataValueField = dataValueField;
    ddl.DataTextField = dataTextField ?? dataValueField;

    ddl.DataBind();
}

然后像这样直接打电话

DBUtil DB = new DBUtil();

FillDdl(ddlSetups, "tradeSetupId", DB.GetTradeSetups);
FillDdl(ddlTimeFrames, "tfCode", DB.GetTimeFrames);
FillDdl(ddlTradeGrades, "tradeGrade", "descr", DB.GetTradeGrades);
FillDdl(ddlExecutionGrades, "executionGrade", "descr", DB.GetExecutionGrades);

或者你仍然可以使用瘦的单个方法

protected void FillTradeSetups()
{
    DBUtil DB = new DBUtil();
    FillDdl(ddlSetups, "tradeSetupId", DB.GetTradeSetups);
}

protected void FillTimeFrames()
{
    DBUtil DB = new DBUtil();
    FillDdl(ddlTimeFrames, "tfCode", DB.GetTimeFrames);
} 

protected void FillTradeGrades()
{
    DBUtil DB = new DBUtil();
    FillDdl(ddlTradeGrades, "tradeGrade", "descr", DB.GetTradeGrades);
}

protected void FillExecutionGrades()
{
    DBUtil DB = new DBUtil();
    FillDdl(ddlExecutionGrades, "executionGrade", "descr", DB.GetExecutionGrades);
}