开发者

How to avoid repeated code?

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?

UPDATE

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 tr开发者_高级运维adeDetails, 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.


Something like this, maybe?

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


FWIW, perhaps something like:

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

However, that plumbing isn't terribly duplicated by itself and ease of future modifications/maintenance should be considered.

Happy coding.


How about something like:

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

Then you can call it like:

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


Create a method that sets the things that you want:

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

Then you can call it with:

SetValues(ddlTradeGrades, "tradeGrade", "descr", dtTradeGrades);


You can do something like this:

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


See Need generic utility C# method for populating ASP.NET DropDownList


May be like this...

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

and then call directly like this

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

or you could still have skinny individual methods

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜