什么是由自己的价值检索我的自定义枚举类的正确方法? [英] What's the correct way of retrieving my custom enumeration classes by their value?

查看:89
本文介绍了什么是由自己的价值检索我的自定义枚举类的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了我的域模型内自己的自定义的伪枚举,让我有一些更详细的值。例如,我的课是如下:

I've created my own custom pseudo enumerations within my domain model to allow me to have some more verbose values. For example, my class is as follows:

public abstract class Enumeration<X, Y> : IComparable where X : IComparable
{

    public Enumeration(X value, Y displayName) { }

    public Y DisplayName { get { return _displayName; } }
    public X Value { get { return _value; } }

}

和继承这将是一个类:

public class JobType : Enumeration<string, string>
{
    public static JobType ChangeOver = new JobType("XY01", "Changeover");
    public static JobType Withdrawal = new JobType("XY02", "Withdrawal");
    public static JobType Installation = new JobType("XY03", "Installation");

    private JobType(string jobTypeId, string description)
        : base(jobTypeId, description) { }
}

我已经得到的问题是,我希望能够从我的资源库数据库返回的值来解决这些值。所以,我的方法,如后我:

The problem I've got is that I want to be able to resolve these values from the value returned from the database in my repository. So I'm after a method such as:

public static JobType Resolve(string jobTypeId) { return matchingJobType; }



我开始写一个解决方法,为每个枚举类,但一定是比一个更好的办法?重复同样的方法,在每个switch语句

I started writing a resolve method for each enumeration class, but there's got to be a better way than duplicating the same method with a switch statement in each?

我想过加入词典< X,枚举< X,Y>>高速缓存; 属性的基类,并从基类的构造函数中添加类进去。这也将具有确保唯一值的好处。我有这样的问题是,当我从字典中获得枚举,它的类型枚举< X,Y> ,我想它作为一个<$ C $ 。C> JobType

I thought about adding a Dictionary<X, Enumeration<X, Y>> Cache; property to the base class, and adding the class into it from the constructor of the base class. This would also have the benefit of ensuring unique values. The problem I have with this is that when I get the enumeration from the Dictionary, it's of the type Enumeration<X, Y> and I want it as a JobType.

因此,这意味着其要么添加第三个泛型类型的枚举类和有:

So this means having to either add a third generic type to the Enumeration class and having:

public static T Resolve(X value); // With the additional type
public static T Resolve<T>(X value); // Or without an additional type



我显然不喜欢写<$的想法C $ C> JobType.Resolve< JobType>(富); ,和我想要的仅仅是 JobType.Resolve(富); ,但它应该用尽可能少的代码尽可能完成。即可这一切仅仅是从基类,而不必包含一个额外的泛型类型如何处理?

I obviously don't like the idea of having to write JobType.Resolve<JobType>(foo);, and what I want is just JobType.Resolve(foo);, but it should be done with as little code as possible. I.e. can all this just be handled from the base class without having to include an additional generic type?

推荐答案

这看起来像一个情况好奇地重复模板模式。如果你想获得基本类型方法返回派生类无需进行转换,那么你可以传递派生类型分为基本型,制约派生类型从基本类型衍生,如:

This looks like a situation for the curiously recurring template pattern. If you want to get the base type method to return the derived class without casting, then you can pass the derived type into the base type, constraining the derived type to to be derived from the base type, e.g.

public abstract class Enumeration<TEnum, X, Y> : IComparable 
    where TEnum : Enumeration<TEnum, X, Y>
    where X : IComparable
{
    public static TEnum Resolve(X value) { /* your lookup here */ }

    // other members same as before; elided for clarity
}

您会再定义你的具体类如下:

You'd then define your concrete classes as follows.

public class JobType : Enumeration<JobType, string, string>
{
    // other members same as before; elided for clarity
}

现在你的类型将匹配起来,在此基础上,不需要铸造类解析方法。

Now your types will match up and no casting required on the base class Resolve method.

JobType type = JobType.Resolve("XY01");

您如何存储在基类中的值映射是由你。这听起来像你已经知道如何反正做到这一点,只需要一点点帮助,获得类型相匹配。

How you store the value to instance mapping in the base class is up to you. It sounds like you already know how to do this anyway, and just needed a bit of help with getting the types to match up.

这篇关于什么是由自己的价值检索我的自定义枚举类的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆