I am building an application that makes heavy use of Enum
s for custom data. Essentially, an object is stored in the database with about 28 separate attributes. Each attribute is a two-character field that's translated from the SQL straight over to an Enum
.
Unfortunately, I need to also translate these values into two different human-readable values. One for a legend on a data table, and one for a CSS class to style an image on the web application front-end.
To do this, I've set up two custom attributes and applied them to the Enum
where necessary. For example:
Custom Attribute Interface
public interface IAttribute<T> { T Value { get; } }
Example Custom Attribute
public sealed class AbbreviationAttribute: Attribute, IAttribute<string> { private readonly string value; public AbbreviationAttribute(string value) { this.value = value; } public string Value { get { return this.value; } } }
Method to Retrieve Custom Attribute from Enum
public static R GetAttributeValue<T, R>(IConvertible @enum) { R attributeValue = default(R); if (@enum != null) { FieldInfo fi = @enum.GetType().GetField(@enum.ToString()); if (fi != null) { T[] attributes = fi.GetCustomAttributes(typeof(T), false) as T[]; if (attributes != null && attributes.Length > 0) { IAttribute<R> attribute = attributes[0] as IAttribute<R>; if (attribute != null) { attributeValue = attribute.Value; } } } } return attributeValue; }
Example Enum
Using This Pattern
public enum Download { [Abbreviation("check")] [Description("Certified")] C = 1, [Abbreviation("no-formal")] [Description("No formal certification")] NF = 2, [Abbreviation("cert-prob")] [Description("Certified with potential problems")] CP = 3 }
Both Abbreviation
and Description
are custom attributes that implement IAttribute<T>
. My actual Enum
has 11 possible values, and as I mentioned before it's used in 28 separate properties in my custom object. Using custom attributes seemed the best way to map this information back and forth.
Now for the question, is this the best way to accomplish this? I store the Enum
value ("C", "NF", or "CP" in the snippet above) in the database, but I need the values of the Abbreviation and Description in my code. Also, I doubt this will be the final set of custom attributes I'll need.
Before I keep moving forward with this pattern ... is it the right way to do things? I'd rather fix potential problems with this approach now than have to track back and refactor later.
An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden).
Enum names are in global scope, they need to be unique.
This is the same method I use. The one downside is serialization. The custom attributes values do not serialize.
I like the custom attribute method over the database method because it ties the attribute data right to the enum instead of having to use a lookup table or class, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With