Marcin On ASP.NET

Keeping my eye on the dot

May, 2008

Posts
  • Marcin On ASP.NET

    Dynamic Data samples: Extending the FilterRepeater

    • 2 Comments

    6/25 Update: This sample has become part of the Dynamic Data Futures project on codeplex. You can still use this code but you should check out the Futures project as it contains a lot more. The key conecpts are the same, though some names or APIs might have changed.

    Note: This post is part of a series, see the list of other Dynamic Data samples.

    The FilterRepeater is a control in Dynamic Data that is responsible for automatically emitting a list of filter controls that can be used to filter data rows displayed for a table. The filters work by providing a list of where parameters to the data source for each supported column type. By default Dynamic Data supports foreign key and boolean columns and renders them using a DropDownList. This and upcoming posts will talk about ways in which you can add filtering for other column types and how to modify the filter UI.

    All of the code referenced here is part of the AdvancedFilterRepeaterSample solution.To run the sample open the solution in Visual Studio 2008 SP1 Beta or later and run AdvancedFilterRepeaterSite (note: it might not be the default project for the solution, so right click on Default.aspx inside the project and choose View in Browser). Navigate to the list page for the Products table where you can experiment with some new filtering options. For example, type "con" into the Categories filter. You should see an AJAX autocomplete drop-down instead of the default drop-down list.

    Introduction to FilterRepeater

    FilterRepeater is a specialized Repeater control that will automatically bind to a collection of filterable columns for the given request's table: if a request comes in for /Products/List.aspx, it will bind to the columns in the Products table. The table is chosen based on the route that the request matched, or it can be overridden by setting the TableName and ContextTypeName properties.

    To work correctly FilterRepeater expects its ItemTemplate to have correct content. Specifically, it requires a control with a known ID ("DynamicFilter" by default) that derives from FilterUserControlBase. When databinding occurs FilterRepeater initializes an instance of the filter control with information about the column to filter.

    Below is a snippet of the code that ships in the default Dynamic Data templates:

    <asp:FilterRepeater ID="FilterRepeater" runat="server">
    <
    ItemTemplate>
    <
    asp:Label runat="server" Text='<%# Eval("DisplayName") %>' />
    <asp:DynamicFilter runat="server" ID="DynamicFilter"
    OnSelectedIndexChanged="OnFilterSelectedIndexChanged" />
    </
    ItemTemplate>
    <
    FooterTemplate><br /><br /></FooterTemplate>
    </
    asp:FilterRepeater>

    FilterRepeater version 2.0 (well, more like 1.1 really)

    The DynamicDataExtensions project contains a simple extension of FilterRepeater called AdvancedFilterRepeater. It is not really that advanced (I started with that name and never bothered to change it) as it overrides only one method: GetFilteredColumns. This method is what is used as the databinding source. Below is a slightly simplified source of how it is implemented:

    public class AdvancedFilterRepeater : FilterRepeater {
    protected override IEnumerable<MetaColumn> GetFilteredColumns() {
    return Table.Columns.Where(c => IsFilterableColumn(c)).OrderBy(column => column, new FilterOrderComparer());
    }

    protected bool IsFilterableColumn(MetaColumn column) {
    if (column.IsCustomProperty) return false;

    var filterAttribute = column.Attributes.OfType<FilterAttribute>().FirstOrDefault();
    if (filterAttribute != null) return filterAttribute.Enabled;

    if (column is MetaForeignKeyColumn) return true;

    if (column.ColumnType == typeof(bool)) return true;

    return false;
    }

    private class FilterOrderComparer : IComparer<MetaColumn> {
    // implementation omitted for brevity
    }
    }

    The Table property is automatically populated with the right MetaTable instance based on the table resolution rules mentioned in the previous section. The rest is a simple LINQ extension method query and some boolean logic to choose the filterable columns. The only way this deviates from the default process is the addition and handling of FilterAttribute, which is a new attribute written for the purpose of this sample. It combines the roles of UIHintAttribute and ScaffoldColumnAttribute from Dynamic Data field templates and should be applied to columns in an analogous manner. Here's the full signature:

    public sealed class FilterAttribute : Attribute {
    public string FilterControl { get; set; }
    public int Order { get; set; }
    public bool Enabled { get; set; }
    }

    The FilterControl property lets you specify which user control to use as the filter while the Enabled property lets you omit a given column's filter in the AdvancedFilterRepeater. The Order property let's you specify an ordering weight.

    Specifying custom filter controls

    The reason why the implementation of AdvancedFilterRepeater is so simple is because a lot of work is done in two other components:

    • FilterFactory is the equivalent of FieldTemplateFactory for filters. It uses the information in a column's FilterAttribute to determine which filter user control to instantiate for the column. It takes the value of the attribute's FilterControl property and looks for a user control located at ~\DynamicData\Filters\{FilterControl}.ascx. If a filter attribute is not specified ~\DynamicData\Filters\Default.ascx is used, which is identical to the FilterUserControl.ascx that is part of the default Dynamic Data template.
    • DelegatingFilter is a control that derives from FilterUserControlBase (which means that it is itself a filter) and delegates the filtering behavior to another filter control. It uses the FilterFactory class to determine which control to use.

    In order to take advantage of AdvancedFilterRepeater and DelegatingFilter you need to replace the existing FilterRepeater in your List.aspx page template with the following code:

    <asp:AdvancedFilterRepeater id="AdvancedFilterRepeater" runat="server">
    <
    HeaderTemplate>
    <
    table>
    </
    HeaderTemplate>
    <
    ItemTemplate>
    <
    tr>
    <
    td valign="top"><%# Eval("DisplayName") %>:</td>
    <
    td><asp:DelegatingFilter runat="server" ID="DynamicFilter"
    OnSelectionChanged="OnFilterSelectionChanged" />
    </
    tr>
    </
    ItemTemplate>
    <
    FooterTemplate>
    </
    table>
    </
    FooterTemplate>
    </
    asp:AdvancedFilterRepeater>

    This template is pretty similar to the default one seen earlier in this post except it wraps each filtered column in a table row for cleaner formatting (yes, using tables for layout is wrong but this post is not about layouts). And of course DynamicFilter has been replaced with DelegatingFilter.

    To take advantage of the custom filter controls included in the sample such as autocomplete filter or cascading filter you need to annotate your data model with FilterAttribute. Here's a snippet of how it is done in the sample:

    [MetadataType(typeof(Product_MD))]
    public partial class Product { }

    public class Product_MD {
    // Display the Category and Supplier filters using the Autocomplete.ascx filter control
    [Filter(FilterControl = "Autocomplete")]
    public object Category { get; set; }
    [Filter(FilterControl = "Autocomplete")]
    public object Supplier { get; set; }

    // Display the Discontinued filter using the BooleanRadio.ascx filter control
    // Make sure the Discontinued filter is displayed first
    [Filter(FilterControl = "BooleanRadio", Order = 1)]
    public object Discontinued { get; set; }

    // Display the UnitsInStock filter using Integer.ascx filter control
    [Filter(FilterControl = "Integer")]
    public object UnitsInStock { get; set; }
    }

    When you run the application and view the list page for the Products table the appropriate filters get inserted automatically. Future posts will discuss how the more advanced filters are implemented.

  • Marcin On ASP.NET

    Dynamic Data samples: Custom metadata providers

    • 6 Comments

    6/25 Update: This sample has become part of the Dynamic Data Futures project on codeplex. You can still use this code but you should check out the Futures project as it contains a lot more. The key conecpts are the same, though some names or APIs might have changed.

    Note: This post is part of a series, see the list of other Dynamic Data samples.

    A while back I posted some sample code for adding custom metadata providers. Since then much has changed in the Dynamic Data runtime and it is time to post an updated version. This sample illustrates how to write a custom metadata provider that allows you to programmatically add metadata attributes to an in-memory store before you register your model with the Dynamic Data runtime in Global.asax.

    This sample should work with the latest preview release of Dynamic Data.

    Running the sample

    Download the metadata provider sample solution, extract to your preferred location, and open in Visual Studio 2008 SP1. The solution contains two projects: a sample website and a small library project that contains the metadata provider code.

    Run the included website and go the Products list page. Once there hit Edit on any of the rows. You will be taken to a details view for the row you are editing. Change the UnitsInStock field to a negative value and move out of the field: a range validation error appears. Go back to the same field, clear it, and move out again: a required validation error appears.

    How it works

    The website scaffolds a small subset of the Northwind sample database. The UnitsInStock column on the Product table is decorated with two attributes: RequiredAttribute and RangeAttribute. However, the unique thing here is that each attribute is coming from a different source.

    The RequiredAttribute is declared in the default Dynamic Data way using a metadata proxy class:

    [MetadataType(typeof(Product_MD))]
    public partial class Product {

    private class Product_MD {
    [Required(ErrorMessage="This field is required [from MetadataType]")]
    public object UnitsInStock { get; set; }
    }
    }

    The RangeAttribute is added to the InMemoryMetadataManager class in Global.asax:

    InMemoryMetadataManager.AddColumnAttributes<Product>(p => p.UnitsInStock,
    new RangeAttribute(0, 1000) {
    ErrorMessage = "This field must be between {1} and {2} [from InMemeroyMetadataManager]." }
    );

    The AddColumnAttributes function shown here has a way to strongly type the property references (as opposed to writing something like AddColumnAttributes("UnitsInStock", ...)) using a simple lambda expression. This provides for some nice IntelliSense support that would not be available if you were referring to properties using simple strings.

    In order to have the Dynamic Data runtime pick up the metadata attributes added to InMemoryMetadataManager you need to modify your model registration call in Global.asax:

    model.RegisterContext(typeof(NorthwindDataContext), new ContextConfiguration() {
    ScaffoldAllTables = true,
    MetadataProviderFactory =
    (type => new InMemoryMetadataTypeDescriptionProvider(type, new AssociatedMetadataTypeTypeDescriptionProvider(type)))
    });

    The MetadataProviderFactory property of ContextConfiguration lets you specify a metadata provider factory method that is used by the Dynamic Data runtime to obtain an instance of a TypeDescriptionProvider that acts as the source of metadata attributes for a given table (type). The sample above also illustrates TypeDescriptionProvider chaining, which is what supports the fact that the table receives metadata from two sources: the InMemory provider adds its metadata to the results returned by the AssociatedMetadataType provider and returns a combined collection.

    Truly dynamic metadata

    At this point it is tempting to consider a dynamic metadata provider that would let you add, remove, and modify attributes throughout the lifetime of the application and not just during the application's startup phase. For example, you could imagine having some sort of administrative interface that would let you turn on and off whether a field is required or modify its description, even after the model has already been registered.

    While it would seem that such a scenario would require minimal changes to the InMemoryMetadataManager class there is one detail about Dynamic Data that makes this a lot more complicated: the current Dynamic Data runtime fetches the metadata upon model registration and then caches it inernally for the lifetime of the app domain. This means that any changes that you make to the metadata through InMemoryMetadataManager after the model has been registered will be ignored.

    This is a limitation of Dynamic Data that we will address in future versions. For the time being the only way to get around this would be to unload the app domain. However, this is means that you will need to find a place (such as a database) to store the new metadata while the app restarts.

    What about the XML metadata?

    My old custom metadata provider sample had an implementation of an XML-based metadata provider but the current one does not. The reason for this is that since the December CTP we have added support for even more attributes and some of the attributes have become a lot more complex. Because much of the old sample had to do with deserializing CLR attributes from an XML representation instead of anything specific to Dynamic Data I decided not to develop the old XML provider any further. However, if somebody ever wrote the appropriate XML parsing code it could easily be used to populate the InMemoryMetadataManager with the right values, essentially resulting in an XML metadata provider. Just remember about the domain unloading mentioned earlier.

  • Marcin On ASP.NET

    Dynamic Data samples

    • 2 Comments

    In breaks between writing the Dynamic Data runtime I also write samples illustrating the various ways Dynamic Data can be extended and customized, often in response to questions on the Dynamic Data forum. In a series of upcoming posts I will share these samples with you as well as discuss how the various extensions work with the Dynamic Data runtime. This post is meant to serve as a list of the samples:

    The list will be updated with links as samples become available.

  • Marcin On ASP.NET

    5/20 Dynamic Data Preview posted to Code Gallery

    • 0 Comments

    We have posted the latest Dynamic Data Preview bits to Code Gallery. You can get them here. Note that you will need Visual Studio 2008 SP1 Beta and/or .NET Framework SP1 Beta installed to run everything.

    See this ASP.NET forum post about what has changed.

    In other news, I will soon be posting a collection of samples that illustrate a number of extension hooks. Stay tuned ...

Page 1 of 1 (4 items)