简体   繁体   English

在C#中表示“组合框”

[英]Representing “comboboxes” in C#

I have to do a form in which has several comboboxes. 我必须做一个表格,里面有几个组合框。 Comboboxes have values represented as Key-Description pairs. 组合框的值表示为“键描述”对。

Each Combobox has its values in a separate table Key<string> - Value<string> 每个组合框在单独的表中都有其值Key<string> Value<string>

How to represent it in a class? 如何在课堂上表现出来?
Suppose we have Project ( Type and Technology ) as "enums", each of them in Type and Technology tables in Database (so its number depends on how many rows in the DB) 假设我们将Project类型技术 )作为“枚举”,它们每个都在数据库的Type和Technology表中(因此其数量取决于数据库中的行数)

class Project {
    public int Id {get; set;}
    public KeyValuePair<string, string> Type {get; set;}
    public KeyValuePair<string, string> Technology {get; set;}
}

or better 或更好

class ProjectType { public string Key; public string Value; }
class ProjectTechnology { public string Key; public string Value; }

class Project {
    public int Id {get; set;}
    public ProjectType Type {get; set;}
    public ProjectTechnolgy Technology {get; set;}
}

From the point of view of the separation of concerns probably we should use the second variant, but why to complicate if we could do simple like in the first ? 从关注点分离的角度来看,我们可能应该使用第二种变体,但是如果我们可以像第一种变体那样简单地做,那么为什么要复杂化呢?

Suppose we have Project (Type and Technology) as "enums", each of them in Type and Technology tables in Database 假设我们将项目(类型和技术)作为“枚举”,每个都在数据库的类型和技术表中

Then why not to use enums? 那为什么不使用枚举呢?

public enum ProjectType
    {
        [Description("Friendly name for type 1")]
        Type1,
        [Description("Friendly name for type 2")]
        Type2,
        [Description("Friendly name for type 3")]
        Type3
    }

And with an extension method like the following you can get the description of a given enum value: 并使用如下扩展方法,可以获取给定枚举值的描述​​:

public static string GetEnumDescription<TEnum>(this TEnum item)
        => item.GetType()
               .GetField(item.ToString())
               .GetCustomAttributes(typeof(DescriptionAttribute), false)
               .Cast<DescriptionAttribute>()
               .FirstOrDefault()?.Description ?? string.Empty;

(I got this code frome here , where also is shown how to map enums to tables in the case your are using EF as ORM) (我从这里获得了此代码,还展示了如何在将EF用作ORM的情况下将枚举映射到表)

The Description attribute can be used as the display string inside your combobox. Description属性可以用作组合框内的显示字符串。 This way works if, as you say, the values are fixed, if they are not or they may grow or change then I would say the second approach (creating separate classes) is better following SOLID best practices, in this case I would say that the second approach violates the O and the S in solid, the Open-closed Principle and the Single Responsibility Principle (more on SOLID ). 如您所说,如果值是固定的,如果值不是固定的或者它们可能增长或更改,则这种方法有效,那么我想说第二种方法(创建单独的类)最好遵循SOLID最佳做法,在这种情况下,第二种方法在整体上违反了OS开放式封闭原则单一责任原则 (更多关于SOLID )。

Let's say Types and Technologies may grow or change, and you follow the first approach. 假设类型和技术可能会增长或改变,而您会遵循第一种方法。 Then you violate the S since now your Project class may change if the definition of Type or Technology changes. 然后您违反了S,因为现在如果Type或Technology的定义更改,您的Project类可能会更改。 Supouse that in the future you realize that you must support mutiple languages (eng, esp, ita, etc), then you violate the O since the definition of Type and Technology can not be anymore represented by a KeyValuePair and you will have to figure out how to achieve this and, worst, you will have to modify Project class which should take no care about this. 假设将来您意识到自己必须支持多种语言(例如eng,esp,ita等),则您违反了O,因为Type和Technology的定义不再可以用KeyValuePair表示,因此您必须弄清楚如何实现这一目标,最糟糕的是,您将不得不修改Project类,而无需对此进行任何考虑。

But if they are fixed values then the enum approach is better since it enforces to use always known values, there is no chance that you can assign to a Project a Type or a Technology that does not exist. 但是,如果它们是固定值,则枚举方法会更好,因为它强制使用始终已知的值,因此您不可能将不存在的类型或技术分配给项目。

This is a matter of opinion and coding style, but here's my take: 这是意见和编码风格的问题,但这是我的看法:

The first option (simple KeyValuePair) is less code, but that doesn't neccesarily make it simpler. 第一个选项(简单的KeyValuePair)是更少的代码,但这并不一定使它更简单。 The second option (creating classes to represent ProjectType and ProjectTechnology) is better, because it is much easier to read. 第二个选项(创建代表ProjectType和ProjectTechnology的类)更好,因为它更容易阅读。

A further improvement (my opinion again) would be not to name the properties of these two classes "Key" and "Value". 进一步的改进(我再次认为)将不会将这两个类的属性命名为“键”和“值”。 Surely these properties represent something more specific than a key and a value? 当然,这些属性表示的内容比键和值更具体吗? Name it accordingly, making it easier for others to understand what they represent. 相应地命名,使其他人更容易理解他们代表的内容。

Hope that's the kind of answer you needed. 希望这是您需要的答案。

PS it's fairly clear what you meant to write, but you forgot to remove the 'int' in PS,您写的意思很清楚,但是您忘了删除其中的“ int”

public int KeyValuePair<string, string> Type {get; set;}
public int KeyValuePair<string, string> Technology {get; set;}

and

 public int ProjectType Type { get; set;}
 public int ProjectTechnolgy Technology { get; set;}

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

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