Wednesday, November 10, 2010

Visual Studio 2010: ASP.NET ControlBuilder MakeGeneric Workaround

Working with enumerations is very code friendly and we use them often especially when we are referencing a lookup table.  The ORM that we use (LLBLGen Pro) released its latest version recently and they make it even easier for us to map an enumeration to an entity field.

Sometimes these lookup tables need to be presented to the end user for whatever reason - be it a drop down, radio button list or a check box list.  When working in VS 2008 I stumbled onto this blog that outlines a straight forward way to create a generic drop down that exposes the the selected value as the specified generic Enum. Using that approach, we centralized all of the enum parsing that goes on for the gets and sets when working with the selected items. We took that implementation and created a generic dropdown and checkbox list.  It worked great in our environment at the time which was Visual Studio 2008.

Here is the checkbox list control that we were using:
public class EnumCheckBoxList<T> : EnumCheckBoxList where T : struct
{
    public List<T> SelectedEnumValues
    {
        get
        {
            List<T> items = new List<T>(base.Items.Count);
            foreach (ListItem item in base.Items)
            {
                if (item.Selected)
                    items.Add((T)Enum.Parse(typeof(T), item.Value));
            }
            return items;
        }
        set
        {
            base.ClearSelection();

            foreach (T item in value)
                base.Items.FindByValue(Enum.GetName(typeof(T), item)).Selected = true;
        }
    }

    /// <summary>
    /// Only valid for use on bit flagged enumerations
    /// </summary>
    public T? SelectedFlaggedEnumValues
    {
        get
        {
            ulong values = 0;
            foreach (ListItem item in base.Items)
            {
                if (item.Selected)
                    values = values | Convert.ToUInt64((T)Enum.Parse(typeof(T), item.Value));
            }
            return values == 0 ? null : (T?)Enum.Parse(typeof(T), values.ToString());
        }
        set
        {
            if (value != null)
            {
                Type enumType = typeof(T);
                foreach (Enum e in Enum.GetValues(enumType))
                {
                    ulong singleEnumFromFlagResult = 0;
                    if ((singleEnumFromFlagResult = (Convert.ToUInt64(e) & Convert.ToUInt64(value.Value))) > 0)
                    {
                        Enum singleEnumFromFlag = (Enum)Enum.Parse(enumType, singleEnumFromFlagResult.ToString());
                        base.Items.FindByValue(Enum.GetName(enumType, singleEnumFromFlag)).Selected = true;
                    }
                }
            }
        }
    }
}

[ControlBuilder(typeof(EnumCheckBoxListControlBuilder))]
public partial class EnumCheckBoxList : CheckBoxList
{
    public string EnumTypeName { get; set; }
}

public class EnumCheckBoxListControlBuilder : ControlBuilder
{
    public override void Init(TemplateParser parser, ControlBuilder parentBuilder, Type type, string tagName, string id,
                              System.Collections.IDictionary attribs)
    {

        string enumTypeName = (string)attribs["EnumTypeName"];
        Type enumType = Type.GetType(enumTypeName);
        if (enumType == null)
            throw new Exception(string.Format("Type for enum {0} can not be created", enumTypeName));

        Type dropDownType = typeof(EnumCheckBoxList<>).MakeGenericType(enumType);
        base.Init(parser, parentBuilder, dropDownType, tagName, id, attribs);
    }
}

Today our new development is done primarily in Visual Studio 2010.  We needed the same functionality for the generic dropdown and checkbox list for our Nucleus project but upon bringing over our implementation we discovered that the ControlBuilder wasn't updating the .designer file - so our controls weren't being converted to their generic implementations. After a little Googling, I found a bug entered in Microsoft Connect that described the issue we are having with VS 2010 and it seems that there isn't any workaround currently available for our situation using the ControlBuilder implementation.

The generic control was nice because programmers wouldn't have to go back and forth suppling plumbing to parse string values into enumerations and vise versa. We took the same idea and instead created a few extension methods.  The extension methods shown below have a get and set defined for a list of enumeration values and also a get and set for enumerations that are decorated with the FlagsAttribute.

/// <summary>
/// Get the selected enumeration values in the listbox
/// </summary>
/// <typeparam name="T">The type of enumeration represented in the listbox</typeparam>
/// <param name="list">The listbox</param>
/// <returns>Returns the list of selected enumeration values in the listbox</returns>
public static List<T> GetSelectedEnumValues<T>(this ListControl list) where T : struct
{
    List<T> items = new List<T>(list.Items.Count);
    foreach (ListItem item in list.Items)
    {
        if (item.Selected)
            items.Add((T)Enum.Parse(typeof(T), item.Value));
    }
    return items;
}

/// <summary>
/// Select the values in the given list of enums in the listbox.
/// </summary>
/// <typeparam name="T">The type of enumeration represented in the listbox</typeparam>
/// <param name="list">The listbox</param>
/// <param name="valuesToSelect">The enum values to select in the listbox</param>
public static void SetSelectedEnumValues<T>(this ListControl list, List<T> valuesToSelect) where T : struct
{
    list.ClearSelection();

    foreach (T item in valuesToSelect)
        list.Items.FindByValue(Enum.GetName(typeof(T), item)).Selected = true;
}

/// <summary>
/// Get the selected enumeration values in the listbox. This method should be called if the enumeration is decorated with the Flags attribute.
/// </summary>
/// <typeparam name="T">The type of enumeration represented in the listbox. This enumeration must be decorated with the Flags attribute</typeparam>
/// <param name="list">The listbox</param>
/// <returns>Returns the selected enumeration values in the listbox as a flagged enumeration. If no values are selected, null is returned</returns>
public static T? GetSelectedFlaggedEnumValues<T>(this ListControl list) where T : struct
{
    ulong values = 0;
    foreach (ListItem item in list.Items)
    {
        if (item.Selected)
            values = values | Convert.ToUInt64((T)Enum.Parse(typeof(T), item.Value));
    }
    return values == 0 ? null : (T?)Enum.Parse(typeof(T), values.ToString());
}

/// <summary>
/// Select the values in the given flag enumeration in the listbox.
/// </summary>
/// <typeparam name="T">The type of enumeration represented in the listbox. This enumeration must be decorated with the Flags attribute</typeparam>
/// <param name="list">The listbox</param>
/// <param name="valuesToSelect">The enum values (represented as Flags) to select in the listbox</param>
public static void SetSelectedFlaggedEnumValues<T>(this ListControl list, T valuesToSelect) where T : struct
{
    list.ClearSelection();

    Type enumType = typeof(T);
    foreach (Enum e in Enum.GetValues(enumType))
    {
        ulong singleEnumFromFlagResult = 0;
        if ((singleEnumFromFlagResult = (Convert.ToUInt64(e) & Convert.ToUInt64(valuesToSelect))) > 0)
        {
            Enum singleEnumFromFlag = (Enum)Enum.Parse(enumType, singleEnumFromFlagResult.ToString());
            list.Items.FindByValue(Enum.GetName(enumType, singleEnumFromFlag)).Selected = true;
        }
    }
}

Hopefully Microsoft responds to the Connect bug with a fix in an upcoming service pack.  In the mean time these extension methods will do the trick.

Links

Friday, November 5, 2010

First Sprint in Review

Wow it is hard to believe that it has been two weeks and we have finished our first Sprint.  Here is how it came out.

Committed Tasks
We completed all but a couple of the tasks we committed in the Sprint.  We under estimated the few learning curves that were introduced by our new environment which caused delays early in the Sprint.

Review (Demo)
Everyone demonstrated the features that they worked on during the Sprint.  We all knew what to expect because we were all involved in the planning process.

Retrospective
We noticed a few things that we would change in the next Sprint.

  1. Use the Work Remaining field on the TFS Tasks to help us better gauge progress and future planning.
  2. When UI elements are "done", gather the team for a quick demo.
    • We want to collect input early to provide a better polished product at the end of the Sprint.

The formal structure and Daily Scrum is welcomed by the team.  If you are not already using an agile process like Scrum, give it a try - you will be surprised with the results.