- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这组代码我写过,感觉挺差的在质量上。如您所见,在四个 case 语句的每一个中我最终重复了很多相同的代码,除了每种情况都有一些变化。变化的项目; session 名称、网格名称和 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;
}
}
最佳答案
您应该能够将案例的各个部分提取到参数化辅助函数中:
function helper(grp, grpname, dg) {
VList<T> theList = FetchInformation(grp);
if (Session[grpname] != null)
{
List<T> tempList = (List<T>)Session[grpname];
theList.AddRange(tempList);
}
dg.DataSource = theList;
dg.DataBind();
}
private void LoadGroup(string option)
{
switch (option.ToUpper())
{
case "ALPHA":
helper(ManagerContext.Current.Group1, "alphaGroup", uxAlphaGrid);
break;
case "BRAVO":
helper(ManagerContext.Current.Group2, "bravoGroup", uxBravoGrid);
break;
case "CHARLIE":
helper(ManagerContext.Current.Group3, "charlieGroup", uxCharlieGrid);
break;
case "DELTA":
helper(ManagerContext.Current.Group4, "deltaGroup", uxDeltaGrid);
break;
default:
break;
}
}
这是一种选择,我敢肯定还有进一步的重构。
对于更深入的重构,我会考虑使用一组选项对象、可能的委托(delegate)或类似对象的表驱动。其工作方式是该选项将成为一个对象而不是一个字符串,并且该选项将具有配置它的属性和调用适当委托(delegate)的方法。这实际上取决于您要维护的抽象级别。有时从常规控件继承并在子类中提供配置信息以便它们知道如何加载自己是值得的。
这里没有足够的空间来真正深入到重构的深度。
关于c# - 反DRY模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1947951/
我是 DRY principle 的坚定拥护者: Every piece of knowledge must have a single, unambiguous, authoritative rep
假设我有一个稍微复杂的 for 循环,用于不同的情况。有没有一种方法可以提取该 forloop 并仍然保持代码可读性? 例如: private function bar(){ for(i=0;
JSData 是替代还是补充 Sequelize.js? 我试图了解这两个库如何一起玩以及是否应该一起玩。 从我的阅读来看,JSData 主要处理访问数据,而 Sequelize.js 将创建和修改
我有 aws-eks 集群,下面是我替换现有配置的命令。 kubectl create configmap flink-config --from-file=./config -o yaml --dr
我正在设计一个可重用的类库,其中包含 2 个名为 core.xml.dll 和 core.string.dll 的程序集(其中包括)。 xml 程序集引用字符串程序集,以便使用一些字符串辅助方法。 但
我正在尝试找到一种更 DRY 的方式来使用 docker-compose env。 docker-compose-base.yml base: image: reactjs_web v
我有一些看起来像这样的类(class): struct A { void *stuff; int x; int foo() const; } 我有一些采用这种类型参数的函数,例如 int
我目前正在 Silverlight 中构建一些自定义控件。我希望这些控件能够响应验证错误。我想要做的是在我的控件周围设置红色边框,就像默认的 Silverlight 控件一样。 我的理解是我需要将它添
我正在处理一些服务器端代码,在将它们传递给客户端之前包装所有异常,因此所有面向客户端的方法都有以下代码 try{ DoSomething(); } catch (ExceptionA e) {
“纵深防御”原则指出,应该在多个地方实现约束,这样如果一条数据绕过或漏过一层,就会在下一层被捕获。一个很好的例子是在网络应用程序中——你将验证放在客户端 javascript 中,放在服务器端代码中(
使用这种方法在 MVC 中查看模型:http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/06/29/how-we-do-mvc-view
我想将 jquery 脚本优化为多个元素(单个页面上有许多幻灯片)。我可以添加一些功能,但这并不干燥(不要重复自己)。 Pr
我有一些代码(不是我的),其中包含带有声明的范围映射的指令。我的愿望是在其他地方使用该指令并传递将在模板中使用的附加变量。 我要传递的变量是theVar。 我发现唯一可行的解决方案看起来很噩梦:
我将数据存储在数组中,并从该数组中使用循环创建表。对于一个表,我需要两列,另外 30 列(取决于数组项)。这将有三列。 var prodej = [ /*First column, second,
有没有更好的方法来声明来自同一组(例如“com.android.support”)具有相同版本(例如“23.4.0”)的多个软件包(例如“appcompat-v7”)? 实际申报: ... def a
我有一个看起来像这样的对象: var myObj = { _fooCon: function(f) { if (typeof f != 'function') throw
这就是我所拥有的。 Map data = new HashMap<>(); // assume this has been populated public int getLastestVersion
我在尝试维护 DRY 概念时遇到了 AngularJS 的问题 [不要重复自己]。我希望我做错了什么,有人可以指出我的错误。 一家公司销售卡车和汽车。 这两个项目具有相似和不同的属性。添加到其中一种的
我最终在几个类中得到了几个遵循相同模式的方法(每个类中总是有一堆这些方法): private void updateFoo() { String newFoo = fooTextField.g
假设我有一个字典config,其中包含键username和password。我想创建一个新字典,仅包含 config 中的username 和 password 键值对。一种方法是: new_dict
我是一名优秀的程序员,十分优秀!