views:

66

answers:

2

Consider the factory method below which takes some meta data and creates a column of the relevant type.

Its all good until I encounter a column which relies on some additional data (ColumnType.DropDownList). This needs some additional data (a list of values) for display purposes.

I dont want to provide this data at a meta data level so providing it when an object is created in the factory seems sensible. But I'm struggling for an elegant way to get the list into the factory method (SEE COMMENT: cant BE HARDCODED HERE!!!)

Any ideas? I'm open to any!

     public static DetailEditorColumn<int> Create(ColumnMetaData metaData)
    {
        if (metaData.ColumnType == ColumnType.Rank)
        {
            return GridColumnBuilder<int>.GetRankColumn(metaData.DisplayOrder, metaData);
        }

        if (metaData.ColumnType == ColumnType.Decision)
        {
            return GridColumnBuilder<int>.GetDecisionColumn(metaData.DisplayOrder, metaData);
        }

        if (metaData.ColumnType == ColumnType.Date)
        {
            return GridColumnBuilder<int>.GetDateColumn(metaData.DisplayOrder, metaData);
        }

        if (metaData.ColumnType == ColumnType.DropDownList)
        {
            // TODO where the humf should this get plugged in!
            // cant BE HARDCODED HERE!!!
            DropDownList lookupList = new DropDownList()
            {
                new DropDownListOption() { Id = 1, Value = "Entry 1", ParenTId = null }, 
                new DropDownListOption() { Id = 2, Value = "Entry 1", ParenTId = null }, 
                new DropDownListOption() { Id = 3, Value = "Entry 1", ParenTId = null }
            };

            return GridColumnBuilder<int>.GetDropDownListColumn(metaData.DisplayOrder, metaData, lookupList);
        }

        throw new Exception("Column Type Not Supported " + metaData);
    }
+2  A: 

Overload the function with one that takes an additional parameter.

public static DetailEditorColumn<int> Create(ColumnMetaData metaData, List<T> lookupList)
{
    //what is coded above
    if (metaData.ColumnType == ColumnType.DropDownList)
    {
        if (lookupList == null)
            //handle error
        else
            return GridColumnBuilder<int>.GetDropDownListColumn(metaData.DisplayOrder, metaData, lookupList);
    }
}

public static DetailEditorColumn<int> Create(ColumnMetaData metaData)
{
    return ClassName.Create(metaData, null);
}
Samuel
+1  A: 

If you want much more elegant solution, i would definetly suggest using the Visitor Pattern here to let every concrete ColumnMetaData (such as Rank, Date, DropDown etc) decide what to do when it comes to create an editor with the builder. It will also help you to meet some non-functional requirements such as extensibility and manageability.

orka