Databases Reference
In-Depth Information
The GetEnumDictionary method is responsible for calling the BuildEnumDictionary
method to extract metadata the first time a particular enum type is processed by the appli-
cation, caching the results to improve performance. This is a common and important
pattern when working with metadata. You want to cache the extracted metadata because
it requires multiple sequential loops and reflection calls to collect. You definitely want to
avoid doing this unnecessarily as it will have a significant negative impact on overall
performance of the web application. In a worst-case scenario, where you have a page with
a GridView control that displays multiple enumeration columns and a large number of
rows, the performance hit might be noticeable even with a single user accessing the
system. By storing the extracted metadata in a static ConcurrentDictionary object, the
UnleashedEnumeraterFieldTemplate ensures that any given enum time is processed only
in the application's lifetime, minimizing any performance impact of reflection.
LISTING 11.1 UnleashedEnumerationFieldTemplate Base Class
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Web.DynamicData;
using System.Web.UI.WebControls;
namespace Unleashed.DynamicData
{
public class UnleashedEnumerationFieldTemplate : UnleashedFieldTemplate
{
private static readonly ConcurrentDictionary<Type, IOrderedDictionary>
enumCache = new ConcurrentDictionary<Type, IOrderedDictionary>();
public override string FieldValueString
{
get
{
object fieldValue = this.FieldValue;
Type enumType = this.Column.GetEnumType();
if (fieldValue != null && enumType != null)
{
fieldValue = Enum.ToObject(enumType, fieldValue);
IOrderedDictionary enumDictionary = GetEnumDictionary(enumType);
var enumName = (string)enumDictionary[fieldValue];
if (!string.IsNullOrEmpty(enumName))
return enumName;
Search WWH ::




Custom Search