Coping with Lagging Enum Support

巧了我就是萌 提交于 2019-12-12 16:03:06

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9185116/coping-with-lagging-enum-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!