【发布时间】:2009-12-22 17:33:22
【问题描述】:
我写了这组代码,感觉很差 在质量上。如您所见,在四个 case 语句中的每一个中 我最终重复了很多相同的代码,除了 在每种情况下都有一些变化。不同的项目;会话名称,网格名称 和 ManagerContext 组名称。任何人都可以接受这些乱七八糟的代码吗? 告诉我一个更好的方法吗?
private void LoadGroup(string option)
{
switch (option.ToUpper())
{
case "ALPHA":
VList<T> alphaList = FetchInformation(
ManagerContext.Current.Group1);
if (Session["alphaGroup"] != null)
{
List<T> tempList = (List<T>)Session["alphaGroup"];
alphaList.AddRange(tempList);
}
uxAlphaGrid.DataSource = alphaList;
uxAlphaGrid.DataBind();
break;
case "BRAVO":
VList<T> bravoList = FetchInformation(
ManagerContext.Current.Group2);
if (Session["bravoGroup"] != null)
{
List<T> tempList = (List<T>)Session["bravoGroup"];
bravoList.AddRange(tempList);
}
uxBravoGrid.DataSource = bravoList;
uxBravoGrid.DataBind();
break;
case "CHARLIE":
VList<T> charlieList = FetchInformation(
ManagerContext.Current.Group3);
if (Session["charlieGroup"] != null)
{
List<T> tempList = (List<T>)Session["charlieGroup"];
charlieList.AddRange(tempList);
}
uxCharlieGrid.DataSource = charlieList;
uxCharlieGrid.DataBind();
break;
case "DELTA":
VList<T> deltaList = FetchInformation(
ManagerContext.Current.Group4);
if (Session["deltaGroup"] != null)
{
List<T> tempList = (List<T>)Session["deltaGroup"];
deltaList.AddRange(tempList);
}
uxDeltaGrid.DataSource = deltaList;
uxDeltaGrid.DataBind();
break;
default:
break;
}
}
【问题讨论】:
-
什么是ManagerContext.Current.GroupX? (枚举、值等?)
-
它是 ManagerContext 中的一个属性,它从会话中返回一个 List
。 groupX 与 Session["Xteam"] 匹配。如果它不为空,则将其添加到列表中,否则跳过它。 -
有 Session["deltaTeam"] 好像很奇怪,后来有 Session["deltaGroup"].. 是不是 bug?..
-
我想我在清理它的时候漏掉了一个名字。我会解决的。
-
不要硬编码这些字符串,至少应该将它们分配给变量,并使用变量代替它们。防止此类错误;)
标签: c# design-patterns