避免连续、相似条件块的方法
想知道是否有更好的方法来处理多个相似的条件语句和操作,例如下面的示例片段。
private void AddCommonDictionaryItemsForAllAttributes(MyCustomType dc, string statusFlag)
{
if (dc.xmlAttributes == null) {
dc.xmlAttributes = new Dictionary<string, string>();
}
dc.xmlAttributes.Add(Constant.CD_1, statusFlag);
dc.xmlAttributes.Add(Constant.CD_2, statusFlag);
dc.xmlAttributes.Add(Constant.CD_3, statusFlag);
if (dc.primaryZone != null) {
dc.xmlAttributes.Add(Constant.CD_4, statusFlag);
}
if (dc.Mgr1 != null) {
dc.xmlAttributes.Add(Constant.CD_10, statusFlag);
}
if (dc.Mgr2 != null) {
dc.xmlAttributes.Add(Constant.CD_11, statusFlag);
}
if (dc.Mgr3 != null) {
dc.xmlAttributes.Add(Constant.CD_5, statusFlag);
}
if (dc.Producer != null) {
dc.xmlAttributes.Add(Constant.CD_6, statusFlag);
}
if (dc.CountTest > 0) {
dc.xmlAttributes.Add(Constant.CD_7, statusFlag);
}
if (dc.List1 != null && dc.List1.Count > 0) {
dc.xmlAttributes.Add(Constant.CD_8, statusFlag);
}
if (dc.List2 != null && dc.List2.Count > 0) {
dc.xmlAttributes.Add(Constant.CD_9, statusFlag);
}
}
在我看来,if 条件和添加到字典操作似乎是多余的,因此寻找更有效和优雅的方法来编码。
谢谢!
更新:我正在使用.NET 3.5
Want to know if there is a better approach to handle multiple, similar conditional statements and actions such as in the example snippet below.
private void AddCommonDictionaryItemsForAllAttributes(MyCustomType dc, string statusFlag)
{
if (dc.xmlAttributes == null) {
dc.xmlAttributes = new Dictionary<string, string>();
}
dc.xmlAttributes.Add(Constant.CD_1, statusFlag);
dc.xmlAttributes.Add(Constant.CD_2, statusFlag);
dc.xmlAttributes.Add(Constant.CD_3, statusFlag);
if (dc.primaryZone != null) {
dc.xmlAttributes.Add(Constant.CD_4, statusFlag);
}
if (dc.Mgr1 != null) {
dc.xmlAttributes.Add(Constant.CD_10, statusFlag);
}
if (dc.Mgr2 != null) {
dc.xmlAttributes.Add(Constant.CD_11, statusFlag);
}
if (dc.Mgr3 != null) {
dc.xmlAttributes.Add(Constant.CD_5, statusFlag);
}
if (dc.Producer != null) {
dc.xmlAttributes.Add(Constant.CD_6, statusFlag);
}
if (dc.CountTest > 0) {
dc.xmlAttributes.Add(Constant.CD_7, statusFlag);
}
if (dc.List1 != null && dc.List1.Count > 0) {
dc.xmlAttributes.Add(Constant.CD_8, statusFlag);
}
if (dc.List2 != null && dc.List2.Count > 0) {
dc.xmlAttributes.Add(Constant.CD_9, statusFlag);
}
}
The if conditions and the addition to the dictionary operation seems to me as redundant so looking out for more efficient and elegant ways to code this.
Thanks!
Update: I am using .NET 3.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以创建一个帮助程序类型,它提供要在
MyCustomType
实例上执行的测试,以及要在xmlAttributes
字典中使用的键:然后您可以创建一个集合这些规则,并列举它们:
You could create a helper type, which provides a test to be performed on an instance of
MyCustomType
, and the key to use in thexmlAttributes
dictionary:You can then create a set of these rules, and enumerate them:
一种选择是拥有某种条件列表以及这些条件表示的常量。例如:
然后,您可以迭代列表,只要谓词为 true,就在字典中设置与
status
相关的内容:请注意,您可以静态设置列表一次,然后在任何地方重用它,如下所示列表本身不会改变。
One option would be to have some sort of list of conditions and the constants represented by those conditions. For example:
Then you could just iterate over the list, setting the relevant to
status
in the dictionary whenever the predicate was true:Note that you can set the list up statically once and then reuse it everywhere, as the list itself doesn't change.
您可以在 for 循环中执行此操作,因为您可以转换为枚举。
You can do it in for loop, because you can cast into to enum.
考虑将属性集合封装在 YourCustomClass 中。这将保护您的属性免遭意外更改,并将属性填充逻辑移向其所属的数据。
优点:
因此,即使使用默认实现,用法也将如下所示:
所有脏工作都将在 dc 内完成(顺便说一句,我建议使用枚举 CD 而不是常量,但这取决于您):
之后您可以轻松重构实现:
客户端仍然只需调用 dc.SetStatus(statusFlag);
也许封装此状态设置逻辑后,您只需将状态保存在 YourCustomClass 字段中。
Consider encapsulating attributes collection inside YourCustomClass. This will protect your attributes from accidental changing, and it will move attributes filling logic toward the data it belongs to.
Benefits:
So, even with your default implementation usage will look like this:
And all the dirty job will be done inside dc (BTW I advice to use enum CD instead of constants, but it's up to you):
After that you can refactor implementation easily:
And client still just calls to dc.SetStatus(statusFlag);
Maybe after encapsulating this status-set logic, you will just save status in field of YourCustomClass.
嗯。这并不是多余的,除非你想考虑像java反射这样的东西。考虑辅助方法:
代码将变为:
等等。也许这作为自定义类型中的方法更有意义:
setXmlStatusAttributes(statusfFlag)
Meh. It's not really redundant, unless you want to consider something like java reflection. Consider helper methods:
The code then becomes:
and so on. Perhaps this would make more sense as a method inside your custom type:
setXmlStatusAttributes(statusfFlag)
有两种方法:
1.使用开关盒
2.使用三元运算符
都将使您的代码看起来干净,但是在您的情况下 switch case 不起作用。
There are two ways:
1. Use switch case
2. Use Ternary operator
both will make your code to look clean, however in your case switch case doesn't work.