枚举的设计指南

发布于 2024-09-06 22:43:58 字数 327 浏览 3 评论 0原文

在金融 Winform 项目之一中,应用程序代码必须处理货币等值。在表示业务实体的对象模型中,有些字段需要保存货币值,例如 USD、EUR 等。该字段的值集很可能仅限于一种标准货币,并且很少需要保存货币值。扩展到新货币。该应用程序从 GUI 控件获取货币输入,该控件从另一个标准源获取这些值并显示为下拉列表,尽管用户也可以将值直接输入到控件中。应用程序的逻辑并不具体取决于货币的值,并且不会直接对这些字段执行需要引用字段中包含的实际值的特殊处理。

现在的问题是:是否建议使用枚举来枚举美元、欧元等值,以便可以根据枚举验证接收到代码中的货币值?如果不是,是否建议保留货币值从下拉列表中输入的事实,以便在代码中不需要额外的验证?

In one of the financial Winform projects , the application code will have to work with values such as currencies. In the object model that represents business entities , there are fields that need to hold currency values such as USD , EUR , etc. The set of values for this field will most probably be limited to one of the standard currencies and rarely will it need to be expanded for new currencies. The application gets the currency inputs from a GUI control that brings these values from another standard source and displays as a dropdown, although it is possible that user inputs a value directly into the control. The application's logic does not specifically depend on the values of the currency and no special process is performed on these fields directly that demands referring to the actual values contained in the fields.

Now for the question : Is it advisable to use an enumeration , that enumerates values such as USD , EUR and the likes, so that currency values received into the code can be validated against the enumeration ? If not , is it advisable to leave it to the fact that the currency values are input from a dropdown , so that no extra validations be needed in code ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心作怪 2024-09-13 22:43:58

我建议您为货币创建一个简单的类或事件结构,并为当前定义的货币创建一些静态只读成员:

class Currency
{
     public static reaonly IEnumerable<Currency> Currencies = new List<Currency>
     {
         new Currency { Name = "USD", CurrencySign = "$" },
         new Currency { Name = "EUR", CurrencySign = "€" }
     }

     public string Name {get; private set;}

     public string CurrencySign {get; private set;}

     public override ToString() { return Name; }
}

它允许您轻松扩展货币列表并从静态成员的其他位置提取它,例如数据库或 Web 服务,而无需重新设计您的申请。

I recommend you create a simple class or event struct for currency, and also create few static readonly members for currently defined currencies:

class Currency
{
     public static reaonly IEnumerable<Currency> Currencies = new List<Currency>
     {
         new Currency { Name = "USD", CurrencySign = "$" },
         new Currency { Name = "EUR", CurrencySign = "€" }
     }

     public string Name {get; private set;}

     public string CurrencySign {get; private set;}

     public override ToString() { return Name; }
}

It allows you to easy extend currency list and extract it from other places then static member, like database or web service without redesing your application.

冷情 2024-09-13 22:43:58

如果代码不处理货币值,并且如果以后可以添加新的货币值,那么我会将货币值编码为(数据)字符串而不是(硬编码)枚举值。

If the code doesn't process the currency value, and if new currency value can be added later, then I'd encode currency values as (data) strings instead of as (hard-coded) enum values.

再可℃爱ぅ一点好了 2024-09-13 22:43:58

由于您有一组有限且固定的可能值,因此枚举或预先填充的下拉列表比允许用户输入自己的值更好。 (如果您的货币少于 256 种,则枚举可能更有效,因为它可以存储在短短 1 个字节中)。另一种方法(如果您设想有一天可能需要添加另一种货币)是从 XML 配置文件或数据库表填充 drpo-down,以便它是数据驱动的且易于扩展(而不是需要重建应用程序以支持新货币)

如果您打算混淆代码,请小心枚举类型 - 一旦混淆,您就无法将枚举值转换为文本(因为它们将被“加扰”),因此您不能再使用使用反射在枚举值和文本之间进行转换(用于填充 UI 控件和/或序列化为文本格式)

As you have a limited and fixed set of possible values, an enumeration or pre-populated drop down are better than allowing the user to enter their own values. (An enumeration is possibly more efficient as it can be stored in as little as one byte if you have fewer than 256 currencies). Another approach (if you envisage that it might be possible one day that another currency needs to be added) would be to populate a drpo-down from an XML configuration file or database table so that it is data-driven and easily extended (rather than requiring a rebuild of the application to support the new currency)

Be careful with enumerated types if you plan to Obfuscate your code - once obfscated, you cannot convert the enum values to text (as they will be "scrambled") so you can no longer use reflection to convert between enumerated values and text (for filling UI controls and/or serialisation to text formats)

只是我以为 2024-09-13 22:43:58

IMO 您应该使用允许的值创建一个枚举并验证输入。原因是因为您可能有一个带有下拉列表的界面,但没有什么可以阻止其他界面使用简单的文本条目。

IMO you should create an enumeration with the allowed values and validate the input. The reason is because you may have one interface with a dropdown but there is nothing preventing other interfaces to use simple text entries for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文