简体   繁体   English

避免使用if-else或switch-case语句来决定

[英]Avoid if-else or switch-case statements for deciding

I'm developing a generic search form, that search controls in that form are depends on type of <T> properties, for example if T is Order , 我正在开发一个通用的搜索表单,该表单中的搜索控件取决于<T>属性的类型,例如T是否为Order

public class Order
{
   public string OrderNumber {get; set;} // search control is 1 TextBox
   public decimal OrderWeight {get; set;} // search controls are 2 TextBox (for accepting a range)
}

the search form will be something like this 搜索表单将是这样的

在此处输入图片说明

i used this statements in my form for deciding that what are appropriate controls for each T property: 我在表单中使用了以下语句来确定每个T属性的适当控件:

if (propertyType.Name == "System.String")
   InsertOneTextBox(paramInfo);
else 
   if(propertyType.Name == "System.Int32" || propertyType.Name == "System.Decimal") 
      InsertTwoTextBoxs(paramInfo);
   else
    if(propertyType.Name == "System.DateTime") 
      InsertTwoDateTimePickers(paramInfo);
    else
       if(propertyType.Name == someotherconditions)    
          InsertOneComboBox(paramInfo);
   ....  

is there any best practice for avoid using if else s or switch case for deciding that what are appropriate controls set for each property type? 有什么最佳实践可以避免使用if elseswitch case来确定为每种属性类型设置了哪些适当的控件?

You can build some kind of map: 您可以构建某种地图:

Upd . 更新

According to your comment: 根据您的评论:

    // somewhere this class is defined in your code
    class ParamInfo {}

    private readonly Dictionary<Type, Action<ParamInfo>> typeToControlsInsertActionMap;

    public MyForm()
    {
        typeToControlsInsertActionMap = new Dictionary<Type, Action<ParamInfo>>
        {
            { typeof(string), InsertOneTextBox },
            { typeof(int), InsertTwoTextBoxs },
            { typeof(decimal), InsertTwoTextBoxs },

            // etc.
        };
    }

    private void InsertOneTextBox(ParamInfo paramInfo) {}
    private void InsertTwoTextBoxs(ParamInfo paramInfo) {}        

Here Action<ParamInfo> is a delegate, which inserts appropriate controls, depending on property type: 这里Action<ParamInfo>是一个委托,它根据属性类型插入适当的控件:

var paramInfo = // ...
var propertyType = // ...    

typeToControlsInsertActionMap[propertyType](paramInfo);

Note, that you shouldn't check type name in your case. 请注意,您不应该在这种情况下检查类型名称。 Use typeof operator instead. 改用typeof运算符。

Create a class using TinyType and make your input string strong typed. 使用TinyType创建一个类,并使输入字符串成为强类型。 Based on these inputs create 4 strategies ( Talking about Strategy Pattern) all driving from same interface. 基于这些输入,创建4个策略(谈论策略模式),所有策略均来自同一界面。 Create a Factory class and inject in your class where you need these strategies. 创建一个Factory类,并在您需要这些策略的类中进行注入。 Now inject this factory in your class let your stringtyped input and factory decide what kind of insertion you want to do ( one text box/ 2 text box etc.) 现在将这个工厂注入您的类中,让您的字符串输入和工厂决定您要进行哪种插入(一个文本框/ 2个文本框等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM