Repurposing Enumerations
Introduction
Enumerations provide the ability to name and then group integral values. Using an enumeration a programmer can provide a set of named values for a variable, property, or parameter. .NET has made enumerations even friendlier. Base functionality in the .NET Enum class lets you retrieve the names of an enumeration member, retrieve name values as an array, and bind them to controls as a data source. This functionality, coupled with the use of a TypeConverter class, allows a programmer to use enumerations in novel ways.
This article highlights a case where TypeConverter classes were used to repurpose enumerations in order to provide a friendly façade to a third-party API that expects parameterized string values.
Problem Description
Enumerations are useful because they provide the ability to put a friendly name to any integral value except char. Additionally, they are a design time construct so you can use dot “.” notation and Intellisense to interact with them. Such as:
// define it
public enum MyEnumeration
{
FriendlyName1=0,
FriendlyName2
}
// use it
protected MyEnumeration localEnum=MyEnumeration.FriendlyName1; |
However, there is not a similar construct for putting a friendly name to a set of constant string values. A typical implementation would be to define a set of static properties or public read-only string values on a class. Although a valid approach, it has a couple of limitations:
1. An enumeration is a specialized typed value. A static property or constant string value is not. For example:
// example of specialized value using enum
// valid values are FriendlyName1 and FriendlyName2
public SomeMethod(MyEnumeration enumParameter)
{
.. .. ..
}
// can’t do this with strings, any string value will do.
public SomeMethod(string stringParameter)
{
.. .. ..
} |
2. If there is a limited set of valid string values, then you will need to write some special validation code. Additionally, the meaning behind the string values may or may not be easy to remember.
In order to make the points real, let’s discuss a real world situation. Through my company Cybral, I implement various .NET XML web service solutions. At one particular client, I am interacting with a third-party API to create new work orders in a billing system. This billing system requires a set of parameterized string values to define the type of work order being created. To make it really simple, let’s say there are just four valid values:
1. "CSRVN" means change services and send a technician.
2. "CSRVY” means change services but do not send a technician.
3. "INSTN" means install services and send a technician.
4. "INSTY" means install services but do not send a technician.
Reading through these codes, you soon realize that they are painful to use. Now multiply this problem by 100 because there are a large number of these situations.
Now, we could create four different functions on a class that expose these different tasks, using a friendly name for each method. For example, ChangeServiceNoTechnician () or ChangeServiceOfferingWithTechnician(). However, what if there are other parameter values that can be combined with these codes? Soon we would have so many methods that we would be creating more problems then we solve.
Enumerations are the answer. But remember these are string values, so how do we generically handle converting these enumerated values to values the underlying API can understand? If the converted value is an integral value, then the default Enum class can handle everything. If we must translate them to other string values, we will have to create a converter class of some sort. Fortunately for us, Microsoft has thought of this too and has provided the TypeConverter.