因此,为了让您了解我正在使用的内容,我有两个下拉框。第一个下拉框包含4个应用程序列表。第二个下拉框随着我在第一个下拉框中的选择动态变化。我在第二个下拉框中有每个选择的图表。我总共有16个图表。每次我更改第二个选择时,图表都会更改,以便一次只显示一个图表。我正在使用if else语句,它已成为难以跟上的方法。我也有标签,我必须与每个图表切换,使其失控。这是我的一个if语句的一个小例子。
else if (ddlApplication.SelectedItem.Text == "Rapp" && ddlTests.SelectedItem.Text == "Total Test Runs")
{
string query = string.Format("select TestName,Count (TestName) AS Counts from VExecutionGlobalHistory where TestTypeID = 2 group by TestName", ddlTests.SelectedItem.Value);
DataTable dt = GetData(query);
//Loop and add each datatable row to the Pie Chart Values
foreach (DataRow row in dt.Rows)
{
SpecificTestsRapp.PieChartValues.Add(new AjaxControlToolkit.PieChartValue
{
Category = row["TestName"].ToString(),
Data = Convert.ToDecimal(row["Counts"])
});
}
string SpecificTestsRappS = null;
string sql2 = "select Count (TestName) AS Counts from VExecutionGlobalHistory where TestTypeID = 2 ";
string connString2 = ";Initial Catalog=Performance;User ID=;Password=";
using (SqlConnection conn = new SqlConnection(connString2))
{
conn.Open();
using (SqlCommand command = new SqlCommand(sql2, conn))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
SpecificTestsRappS = reader[0].ToString();
lblTotalTestsRapp.Text = SpecificTestsRappS;
break;
}
}
conn.Close();
}
SpecificTestsRapp.ChartTitle = "Total Tests Run";
TotalTestsWeb6.Visible = false;
HoursWeb6.Visible = false;
TotalValidationsWeb6.Visible = false;
CostComparisonWeb6.Visible = false;
SpecificTestsWeb6.Visible = false;
TotalTestsRapp.Visible = false;
TotalValidationsRapp.Visible = false;
SpecificTestsRapp.Visible = true;
HoursRapp.Visible = false;
IONChart.Visible = false;
CostComparisonRapp.Visible = false;
txtTotalHoursRapp.Visible = false;
lblTotalHoursRapp.Visible = false;
txtTotalHoursRappA.Visible = false;
lblTotalHoursRappA.Visible = false;
txtTotalCostRappM.Visible = false;
lblTotalCostRappM.Visible = false;
txtTotalCostRappA.Visible = false;
lblTotalCostRappA.Visible = false;
txtTotalTestsRapp.Visible = true;
lblTotalTestsRapp.Visible = true;
lblTotalValidationsRapp.Visible = false;
txtTotalValidationsRapp.Visible = false;
lblTotalValidations.Visible = false;
txtTotalValidations.Visible = false;
lblTotalTests.Visible = false;
txtTotalTests.Visible = false;
txtTotalHours.Visible = false;
lblTotalHours.Visible = false;
txtTotalHoursA.Visible = false;
lblTotalHoursA.Visible = false;
txtTotalCostA.Visible = false;
lblTotalCostA.Visible = false;
txtTotalCostM.Visible = false;
lblTotalCostM.Visible = false;
Label1.Visible = false;
Label2.Visible = false;
}
从技术上来说,当我有16个图表时,我将需要其中的16个。必须有一种更有效的方法来做到这一点。有什么建议吗?
答案 0 :(得分:1)
你可以做以下几行:
在您的用户界面中:
var chartStartegyFactory = new ChartStrategyFactory();
var chartStategy = chartStartegyFactory.Create(ddlApplication.SelectedItem.Text, ddlTests.SelectedItem.Text);
var chart = chartStategy.CreateChart();
lblTotalTestsRapp.Text = chart.ChartData;
SpecificTestsRapp.ChartTitle = chart.ChartTitle;
TotalTestsWeb6.Visible = chart.TotalTestsWeb6Visible;
// continue assigning properties in your UI
在您的业务层中:
public class ChartStrategyFactory
{
public IChartStrategy Create(string application, string test)
{
if (application == "Rapp" && test == "Total Test Runs")
return new RappTotalTestsRunChartStrategy();
// add strategies for other charts
throw new NotSupportedException();
}
}
public interface IChartStrategy
{
Chart CreateChart();
}
public class Chart
{
public string ChartTitle { get; set; }
public string ChartData { get; set; }
public bool TotalTestsWeb6Visible { get; set; }
// create all properties you need
}
public class RappTotalTestsRunChartStrategy : IChartStrategy
{
public Chart CreateChart(){
Chart chart = new Chart();
chart.ChartData = GetDataFromDatabase();
chart.ChartTitle = "Your Chart Title";
chart.TotalTestsWeb6Visible = false;
// continue assigning properties
return chart;
}
}
基本上,您的图表创建代码将封装在每个Startegy中,您将获得可扩展性(在开发新图表时,只需修改ChartStrategyFactory即可。