应对滞后的枚举支持

发布于 2025-01-03 19:08:00 字数 1203 浏览 3 评论 0原文

在 EF 5.0(计划很快发布)之前,实体框架将不支持枚举。

实体框架 4.2 枚举支持

http://blogs.msdn.com/b/efdesign/archive/2011/06/29/enumeration-support-in-entity-framework.aspx

http://visualstudiomagazine.com/blogs/data-driver/2012/01/entity-framework-4-3-gets-final-tune-up.aspx

WCF 数据服务(以及 oData 标准) 不支持枚举

我们知道枚举很常见,不幸的是他们从未遇到过 到目前为止。它们在我们下一步要做的事情清单上排名相当靠前 虽然

(请参阅:https://stackoverflow.com/a/3571378/141172

我已经开始了一个新项目,并且将枚举替换为以下内容:

public static class MyEnum
{
    public const int MyValue = 0;
    public const int AnotherValue = 1;
}

我牺牲了枚举提供的保证,即仅分配定义的值,以便利用重要的(此时相当成熟的)基础设施服务。

有没有更好的方法来处理滞后的枚举支持?一旦 EF 和 WCF 数据服务添加了枚举支持,是否可能会出现另一个重要的框架,它会像这两个框架一样缓慢地引入枚举支持?

The Entity Framework will not support enum's until EF 5.0 (scheduled to ship soon).

Entity Framework 4.2 enum support

http://blogs.msdn.com/b/efdesign/archive/2011/06/29/enumeration-support-in-entity-framework.aspx

http://visualstudiomagazine.com/blogs/data-driver/2012/01/entity-framework-4-3-gets-final-tune-up.aspx

WCF Data Services (and the oData standard) to not support enums

We understand that enums are common, unfortunately they never met tha
bar so far. They are rather high on our list of things to do next
though

(See: https://stackoverflow.com/a/3571378/141172)

I have begun a new project and am replacing enum's with something like:

public static class MyEnum
{
    public const int MyValue = 0;
    public const int AnotherValue = 1;
}

I'm sacrificing the guarantee that enum's provide that only defined values will be assigned in order to utilize important (and at this point fairly mature) infrastructure services.

Is there a better way to handle lagging enum support? Once EF and WCF Data Services add enum support, is there likely to be another important framework on the horizon that will introduce enum support as slowly as these two have?

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

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

发布评论

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

评论(2

始于初秋 2025-01-10 19:08:00

您可以使用带有私有构造函数的结构来为每个值定义一个公共静态成员吗​​:

public struct FakeEnum
    {
        public static readonly FakeEnum MyValue = new FakeEnum(0);
        public static readonly FakeEnum AnotherValue = new FakeEnum(1);

        private readonly int _value;

        private FakeEnum(int value)
        {
            _value = value;
        }

        public int Value { get { return _value; } }

        // TODO: Equals and GetHasCode and Operators Oh My!
    }

注意:我实际上还没有尝试过这一点。

Could you use a struct with a private constructor that defines a public static member for each value:

public struct FakeEnum
    {
        public static readonly FakeEnum MyValue = new FakeEnum(0);
        public static readonly FakeEnum AnotherValue = new FakeEnum(1);

        private readonly int _value;

        private FakeEnum(int value)
        {
            _value = value;
        }

        public int Value { get { return _value; } }

        // TODO: Equals and GetHasCode and Operators Oh My!
    }

Note: I haven't actually tried this out.

宫墨修音 2025-01-10 19:08:00

比您的示例更好的是使用枚举来保存常量值并每次都转换为 int 。这样你至少可以在任何可能的地方使用枚举而不是失去一切。

枚举永远不会提供不可避免的类型安全。但是,通过将枚举的使用尽可能扩展到应用程序代码和业务逻辑中,您可以获得越来越多的安全性,以防止微妙的使用错误。

Well better than your example would be using an enum to hold the constant values and casting to int everytime. That way you can at least use the enum whereever it is possible rather than loosing everything.

enums never provide inescapable type-safety. But by extending the usage of enums as far as you can into the applications code and business logic, you get more and more safety against subtle usage errors.

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