After Thought

Smart Enums

with 2 comments

Our team was introduced to Smart Enums by J.P. Boodhoo in one of his Nothin’ but .NET classes in Austin. An example of a simple Smart Enum is shown below.

public class PlasticType
{
	public static PlasticType PET = new PlasticType("pet");
	public static PlasticType HDPE = new PlasticType("hdpe");
	public static PlasticType PVC = new PlasticType("pvc");
	public static PlasticType LDPE = new PlasticType("ldpe");
	public static PlasticType PP = new PlasticType("pp");
	public static PlasticType PS = new PlasticType("ps");
	public static PlasticType Other = new PlasticType("other");

	private PlasticType(string key)
	{
		Key = key;
	}
	public string Key { get; set; }
}

PlasticType is a Smart Enum that is just a collection of static classes that can be used instead of a regular Enum in our code.

The example above describes different types of plastic  (source: Wikipedia) that are in use today. The numbers were devised by Plastic Bottle Institute of the Society of the Plastics Industry to make recycling easier (On a separate note, please recycle plastic after use. Let us make the world a better place to live for the next generation). You may ask what are PET, HDPE or PVC. To make the output more readable if the PlasticTypes were to be printed, let us add Description property as shown below.

public class PlasticType
{
	public static PlasticType PET = new PlasticType("pet", "Polyethylene Terephthalate");
	public static PlasticType HDPE = new PlasticType("hdpe", "High-density Polyethylene");
	public static PlasticType PVC = new PlasticType("pvc", "Polyvinyl Chloride");
	public static PlasticType LDPE = new PlasticType("ldpe", "Low-density Polyethylene");
	public static PlasticType PP = new PlasticType("pp", "Polypropylene");
	public static PlasticType PS = new PlasticType("ps", "Polystyrene");
	public static PlasticType Other = new PlasticType("other", "Others");

	private PlasticType(string key, string description)
	{
		Key = key;
		Description = description;
	}

	public string Key { get; set; }
	public string Description { get; set; }	
}

Smart Enum is already looking better than our regular Enum with the addition of Description property. We (myself and Clinton Sheppard) decided to take it a step further to make the usage of PlasticType easier in our code. So we added a GetFor(key) method as shown below.

public class PlasticType
{
	public static PlasticType PET = new PlasticType("pet", "Polyethylene Terephthalate");
	public static PlasticType HDPE = new PlasticType("hdpe", "High-density Polyethylene");
	public static PlasticType PVC = new PlasticType("pvc", "Polyvinyl Chloride");
	public static PlasticType LDPE = new PlasticType("ldpe", "Low-density Polyethylene");
	public static PlasticType PP = new PlasticType("pp", "Polypropylene");
	public static PlasticType PS = new PlasticType("ps", "Polystyrene");
	public static PlasticType Other = new PlasticType("other", "Others");

	private PlasticType(string key, string description)
	{
		Key = key;
		Description = description;
        NamedConstants.Add(key, this);
	}

	public static PlasticType GetFor(string key)
	{
		if (key == null)
		{
			return null;
		}

		PlasticType plasticType;
		NamedConstants.TryGetValue(key, out plasticType);
		return plasticType;
	}

	private static readonly Dictionary<string, PlasticType> NamedConstants = new Dictionary<string, PlasticType>();

	public string Key { get; set; }
	public string Description { get; set; }
}

GetFor(key) method converts a given key to the corresponding Smart Enum that can be used within our code. This limits the usage of strings only within the Smart Enum making renaming easier across the project. We have created a NamedConstant class (our name for Smart Enum) that is part of MvbaCore project that provides other helper methods like Equals, Values (enumerates over all the available PlasticTypes). Inheriting PlasticType from NamedConstant class would add all the helper methods automatically as shown below.

public class PlasticType : NamedConstant&lt;PlasticType&gt;
{
	public static PlasticType PET = new PlasticType("pet", "Polyethylene Terephthalate");
	public static PlasticType HDPE = new PlasticType("hdpe", "High-density Polyethylene");
	public static PlasticType PVC = new PlasticType("pvc", "Polyvinyl Chloride");
	public static PlasticType LDPE = new PlasticType("ldpe", "Low-density Polyethylene");
	public static PlasticType PP = new PlasticType("pp", "Polypropylene");
	public static PlasticType PS = new PlasticType("ps", "Polystyrene");
	public static PlasticType Other = new PlasticType("other", "Others");

	private PlasticType(string key, string description)
	{
		Key = key;
		Description = description;
		NamedConstants.Add(key, this);
	}

	public string Description { get; set; }
}

NamedConstant<T> makes creating Smart Enums easier, so we can focus on solving a business problem rather than building helper methods around it.

We can use Smart Enums not only to compare types like any other regular Enum but also add behavior to them making it smarter as described in this post.

You can create a Smart Enum as described above without a key or description. It depends on the problem you are trying to solve.

About these ads

Written by shashankshetty

July 18, 2010 at 9:51 pm

Posted in C#, Uncategorized

Tagged with

2 Responses

Subscribe to comments with RSS.

  1. [...] conditional statements (if…else) in our code. Continuing our example of PlasticType from the Smart Enums post, let us say we have to build a controller action that has to decide the recycling program [...]

  2. [...] release of Constant, a component that enables developers to create smart enums in C# and is implemented using .NET portable class [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: