如何避免重复的代码? [英] How to avoid repeated code?

查看:134
本文介绍了如何避免重复的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是很新的编程和我注意到,我不停地重复代码:

I'm still quite new to programming and I noticed that I'm repeating code:

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();
} 



我怎么能对这个有点聪明吗?你能帮因此,它不是重复了这么多我重新写代码?

How can I be a bit smarter about this? Can you help me re-write the code so that it's not repeating so much?

更新

哇,谢谢你的答复,我想我会发布什么,我想实现的。我还创建了自己的另一个小工人,除去其他一些丑陋的重复代码。你觉得这个怎么样?

Wow, thanks for the replies, I thought I'd post what I'm thinking of implementing. I also created myself another little worker to remove some other ugly duplicated code. What do you think of this?

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();
}



,然后用类似称呼它:

and then call it with something like:

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"));



编码感觉好极了,当丑陋的东西转成更优雅。

Coding feels great when something ugly turns into something more elegant.

推荐答案

这样的事情,也许?

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");
    }
}

这篇关于如何避免重复的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆