Easy Enum property for EPiServer
December 26th, 2009 by Adam Najmanowicz | 21 CommentsOne of the most frequently and eagerly used programming constructs of the Microsoft.Net Framework is Enum. There are several interesting features that make it very compelling to use to for all kinds of dropdowns and checklists:
- The bounds factor ? proper use of Enum type guarantee that the selected value will fall within the constraints of the allowed value set.
- The ability to treat Enums as flags (and compound them into flag sets) as well as a one-of selector.
- The ease of use and potentially complete separation of the ?Enum value? from the underlying machine type representation that ensures the most efficient memory usage.
Surprisingly enough EPiServer as it stands right now does not have an easy facility to turn Enums into properties. To give credit where credit is due, the EPiServer framework provides a nice surrogate that mimic that behaviour to a degree. The relevant property types are:
- PropertyAppSettingsMultiple – which ?creates check boxes with options that are defined in the AppSettings section in web.config. The name of the property should match the key for the app setting.?
- PropertyAppSettings – which ?creates a drop down list with options that are defined in the AppSettings section in web.config. The name of the property should match the key for the app setting.?
You quickly realize though that the properties have some limitations that makes their use a bit less compelling:
- The properties are not strongly typed
- The property entry in AppSettings section has to have the name that matches the property name on the page.
- It?s rather poorly documented, Other than relating to this blog entry or Erik?s post documenting it I could not find any other examples on how to use them. (but then again, who needs docs really when we have Reflector)
- You cannot have the very same property duplicated on the page since you can only have a single property of a given name per page. So you need to have multiple entries in AppSettings that match the name of each of those properties on your pages. I know? semantics but still?
- You are working on strings rather than enums (Did i mention it?s not type safe?)
- The values in the AppSettings are stored in a somewhat DLS-y manner (consecutive options are separated from each other with the ?|? character, the name and the value are separated with a ?;?, for example: <add key = "RegionId" value="First Option;Option1|Default Option;Option2|Disabled Option;Option3" /> ) and I have had on an occasion entered a string there that caused the server to crash.
- The values are not translatable, or at least I could not find how to do it and any Reflector digging rendered no results either.