ASP .NET Query Projections – Part 3

In this post we are quickly going to discuss the need for a consistent way for our binding controls to let our selector class know exactly what to project. I will illustrate the problem by comparing the telerik controls RadComboBox and RadGrid.

Lets quickly look at where the data bindings are defined in each control:

RadComboBox
<telerik:RadComboBox ID="ComboBox" runat="server" DataSourceID="Source" DataTextField="Name" DataValueField="TopicId" />
RadGrid
<telerik:RadGrid ID="Grid" runat="server" DataSourceID="Source2"
                            AutoGenerateColumns="False">
    <MasterTableView DataKeyNames="TopicId">
        <Columns>
            <telerik:GridBoundColumn HeaderText="Id" UniqueName="Id" DataField="SchoolId" />
            <telerik:GridBoundColumn HeaderText="Name" UniqueName="Name" DataField="Name" />
        </Columns>
    </MasterTableView>
</telerik:Grid>

As you can see in the RadComboBox data bound fields exist in the DataTextField and DataValueField properties while the RadGrid data bound fields exist in the DataKeyNames and bound columns. In the end I thought it would be best to extend these controls and make them implement a common interface:

public interface IProjectionBinder
{
    IEnumerable<string> DataBoundFields(string args);
}

The method parameter is important because it is possible to need to pick up a specific set of fields (For example in the grid it is possible for a column to bind to a separate data source that we may need to project – in this case we would need to pass in a parameter to identify the column).

Note: It may be possible that you cannot include every field that the control uses – for example in templates for the RadGrid. In such cases you will be able to add them into the AdditionalFields property of the Selector control.

For completion I’ll include the implementation for the RadComboBox control:

public class ComboBox : RadComboBox, IProjectionBinder
{
    #region IProjectionBinder Members

    public IEnumerable<string> DataBoundFields(string args)
    {
        if (!string.IsNullOrEmpty(args))
        {
            throw new ArgumentException("ComboBox does not support Projection arguments", "args");
        }

        if (string.IsNullOrEmpty(DataTextField) || string.IsNullOrEmpty(DataValueField))
        {
            throw new InvalidOperationException("Both DataTextField and DataValueField need to be defined for projection");
        }

        List<string> fields = new List<string>();

        fields.Add(DataTextField);
        fields.Add(DataValueField);

        return fields;
    }

    #endregion
}

At this point we now have hooks in the ObjectDataSource and a common way to get data bound fields of a control. In the next post I will put it all together via the Selector control.

Print | posted on Sunday, 22 August 2010 6:57 PM

Feedback

No comments posted yet.
Title  
Name
Email (never displayed)
Url
Comments   
Please add 5 and 7 and type the answer here: