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; } }
}
And a class that inherits it would be:
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; }
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?
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
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?