重构if if statments

时间:2015-08-12 19:24:57

标签: c# asp.net datagridview telerik telerik-grid

我有一个包含14个if语句的方法,而且我必须再做12次完全相同的事情,所以就像160 if语句一样。如何重构以使其更干净?我正在使用telerik radgrid,我将条件格式应用于单元格,但每列不同,并且根据不同列中的值而不同。这是我方法的开始。

fyi:它确实有用。

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        //Is it a GridDataItem
        if (e.Item is GridDataItem)
        {
            //Get the instance of the right type
            GridDataItem dataBoundItem = e.Item as GridDataItem;

            //Check the formatting condition
            if (dataBoundItem["sample_hour"].Text == "4hr YP")
            {
                if (float.Parse(dataBoundItem["ph"].Text) > 5.72 || float.Parse(dataBoundItem["ph"].Text) < 4.75)
                {
                    dataBoundItem["ph"].BackColor = Color.Yellow;
                    dataBoundItem["ph"].ForeColor = Color.Black;
                    dataBoundItem["ph"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["brix"].Text) > 22.36 || float.Parse(dataBoundItem["brix"].Text) < 17.35)
                {
                    dataBoundItem["brix"].BackColor = Color.Yellow;
                    dataBoundItem["brix"].ForeColor = Color.Black;
                    dataBoundItem["brix"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["temp"].Text) > 91 || float.Parse(dataBoundItem["temp"].Text) < 89)
                {
                    dataBoundItem["temp"].BackColor = Color.Yellow;
                    dataBoundItem["temp"].ForeColor = Color.Black;
                    dataBoundItem["temp"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["bud"].Text) > 41.76 || float.Parse(dataBoundItem["bud"].Text) < 3.121)
                {
                    dataBoundItem["bud"].BackColor = Color.Yellow;
                    dataBoundItem["bud"].ForeColor = Color.Black;
                    dataBoundItem["bud"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["cell_count"].Text) > 177.70 || float.Parse(dataBoundItem["cell_count"].Text) < 41.24)
                {
                    dataBoundItem["cell_count"].BackColor = Color.Yellow;
                    dataBoundItem["cell_count"].ForeColor = Color.Black;
                    dataBoundItem["cell_count"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["viability"].Text) > 69.183 || float.Parse(dataBoundItem["viability"].Text) < 5.65)
                {
                    dataBoundItem["viability"].BackColor = Color.Yellow;
                    dataBoundItem["viability"].ForeColor = Color.Black;
                    dataBoundItem["viability"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["dp4"].Text) > 10.892 || float.Parse(dataBoundItem["dp4"].Text) < 2.556)
                {
                    dataBoundItem["dp4"].BackColor = Color.Yellow;
                    dataBoundItem["dp4"].ForeColor = Color.Black;
                    dataBoundItem["dp4"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["dp3"].Text) > 6.052 || float.Parse(dataBoundItem["ph"].Text) < 1.412)
                {
                    dataBoundItem["bud"].BackColor = Color.Yellow;
                    dataBoundItem["bud"].ForeColor = Color.Black;
                    dataBoundItem["bud"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["maltose"].Text) > 8.285 || float.Parse(dataBoundItem["maltose"].Text) < .419)
                {
                    dataBoundItem["maltose"].BackColor = Color.Yellow;
                    dataBoundItem["maltose"].ForeColor = Color.Black;
                    dataBoundItem["maltose"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["glucose"].Text) > 6.695 || float.Parse(dataBoundItem["glucose"].Text) < -0.263)
                {
                    dataBoundItem["glucose"].BackColor = Color.Yellow;
                    dataBoundItem["glucose"].ForeColor = Color.Black;
                    dataBoundItem["glucose"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["lactic_acid"].Text) > .124 || float.Parse(dataBoundItem["lactic_acid"].Text) < .0101)
                {
                    dataBoundItem["lactic_acid"].BackColor = Color.Yellow;
                    dataBoundItem["lactic_acid"].ForeColor = Color.Black;
                    dataBoundItem["lactic_acid"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["glycerol"].Text) > .574 || float.Parse(dataBoundItem["ph"].Text) < .332)
                {
                    dataBoundItem["glycerol"].BackColor = Color.Yellow;
                    dataBoundItem["glycerol"].ForeColor = Color.Black;
                    dataBoundItem["glycerol"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["acetic_acid"].Text) > 0.176|| float.Parse(dataBoundItem["acetic_acid"].Text) < -.0756)
                {
                    dataBoundItem["acetic_acid"].BackColor = Color.Yellow;
                    dataBoundItem["acetic_acid"].ForeColor = Color.Black;
                    dataBoundItem["acetic_acid"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["ethanol"].Text) > 1.159 || float.Parse(dataBoundItem["ethanol"].Text) < .0053)
                {
                    dataBoundItem["ethanol"].BackColor = Color.Yellow;
                    dataBoundItem["ethanol"].ForeColor = Color.Black;
                    dataBoundItem["ethanol"].Font.Bold = true;

                }

            }

        }
    }

3 个答案:

答案 0 :(得分:5)

你所有的if语句都在做同样的事情,你可以将它们重构成一个方法。

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    //Is it a GridDataItem
    if (e.Item is GridDataItem)
    {
        //Get the instance of the right type
        GridDataItem dataBoundItem = e.Item as GridDataItem;

        //Check the formatting condition
        if (dataBoundItem["sample_hour"].Text == "4hr YP")
        {
            SetFormatting(dataBoundItem["ph"], 5.72, 4.75);
            SetFormatting(dataBoundItem["brix"], 22.36, 17.35);

            // etc...
        }

    }
}

private void SetFormatting(TableCell cell, float minValue, float maxValue)
{
    float value = float.Parse(cell.Text);

    if (value > minValue || value < maxValue)
    {
        cell.BackColor = Color.Yellow;
        cell.ForeColor = Color.Black;
        cell.Font.Bold = true;
    }
}

答案 1 :(得分:1)

if中的逻辑是相同的,所以 - 把它移到它自己的方法中:

private void Logic( GridDataItem dataBoundItem, string key, float max, float min )
{
    float f = float.Parse( dataBoundItem[key].Text );
    if( f > max || f < min )
    {
         dataBoundItem[key].BackColor = Color.Yellow;
         dataBoundItem[key].ForeColor = Color.Black;
         dataBoundItem[key].Font.Bold = true;
    }
}

然后像这样使用:

Logic( dataBoundItem, "ph", 5.72, 4.75 );
Logic( dataBoundItem, "brix", 22.36, 17.35 );
...

但是,您仍然将数据与逻辑混合在一起。因此,创建一个为您保存数据的类(即key,max,min等)。将该类的一堆实例添加到数组/列表中,并在每个实例上循环运行逻辑:

class Rule
{
    public string Key;
    public float Min, Max;
}

private Rule[] m_RulesCase1 = new Rule[]
                              {
                                  new Rule() { Key = "ph", Max = 5.72, Min = 4.75 }
                                  new Rule() { Key = "brix", Max = 22.36, Min = 17.35 }
                                  ...
                              };

private void ApplyRule( GridDataItem dataBoundItem, Rule r )
{
    float f = float.Parse( dataBoundItem[r.Key].Text );
    if( f > r.Max || f < r.Min )
    {
         dataBoundItem[r.Key].BackColor = Color.Yellow;
         dataBoundItem[r.Key].ForeColor = Color.Black;
         dataBoundItem[r.Key].Font.Bold = true;
    }
}

private void ApplyRules( GridDataItem dataBoundItem, IEnumerable<Rule> rules )
{
    foreach( var r in rules )
        ApplyRule( dataBoundItem, r );
}

并且,您可以随时进一步从配置文件或其他来源获取规则,避免将其完全包含在代码中。

答案 2 :(得分:0)

您可以创建如下过程:

private void CellFormat(string column,float min,float max,Color backcolor,Color forecolor,bool bold)
{
   if (float.Parse(dataBoundItem[column].Text) > min || float.Parse(dataBoundItem[column].Text) < max5)
   {
                    dataBoundItem[column].BackColor = backcolor;
                    dataBoundItem[column].ForeColor = forecolor;
                    dataBoundItem[column].Font.Bold = bold;
    }

}

会给出什么:

CellFormat("ph"  , 4.75, 5.72, Color.Yellow,Color.Black,true) ;
CellFormat("brix",17.35,22.36, Color.Yellow,Color.Black,true) ;