Josh Heyse

Thoughts Defragmented

Archive for the 'LINQ' Category

Dynamic Data Filtering – Table Text Search

Posted by jheyse on 17th October 2008

I’m pretty happy with the number of people who have been downloading and trying out Dynamic Data Filtering.  On average there are about 20 downloads a day with around 50 page views.  There has been a lot of good feedback on both the ASP.NET forums and on the CodePlex project site 5over the last week or so.  So much actually that I am having trouble keeping up with answering the questions coming in. 

One question did catch my eye though.  It was a member of the ASP.NET forum asking about being able to search on all of the columns within a given table.  [View the Post]  Dynamic Data doesn’t support this functionality out of the box, but it is flexible enough to write something yourself to do it.  The main things that makes this possible is that a single FilterTemplate can return one ore more DynamicFilterParameters which produce lambda predicates.

In this case, you can accomplish this logic of searching multiple columns in a table by saying where ProductName LIKE [Value] or ProductNumber LIKE [Value] etc…  To accomplish this in Dynamic Data Filtering you would utilize the OrExpressionParameter to create a collection of LikeExpressionParameters to do the predicate for each individual column.

I spent a little time creating a simple example which performs this logic.  I also implemented a Columns property which allows you to specify using a comma separated list the columns you want searched.  If Columns is left null or empty all columns are searched.  The aspx for the user control only contains a single asp:TextBox with the Id=TextBox1.

public partial class TableTextSearch : Catalyst.Web.DynamicData.FilterTemplateUserControlBase
{
    public string Columns { get; set; }
 
    private MetaTable Table
    {
        get
        {
            IDynamicDataSource source = this.FindDataSourceControl();
            if (source != null)
                return source.GetTable();
            return null;
        }
    }
 
    public override IEnumerable<Parameter> GetWhereParameters(System.Web.DynamicData.IDynamicDataSource dataSource)
    {
        OrExpressionParameter parameter = new OrExpressionParameter();
 
        string[] cols = null;
        if (!string.IsNullOrEmpty(Columns))
        {
            cols = Columns.Split(',');
            for (int i = 0; i < cols.Length; i++)
                cols[i] = cols[i].Trim().ToUpper();
        }
 
        var columns = (from c in Table.Columns
                       where cols == null || cols.Length == 0 || cols.Contains(c.Name)
                       group c by c.TypeCode).ToDictionary(g => g.Key, g => g);
 
        foreach (var column in columns[TypeCode.String])
        {
            parameter.Parameters.Add(new LikeExpressionParameter()
                    {
                        Type = TypeCode.String,
                        Name = column.Name,
                        Value = TextBox1.Text,
                        Like = LikeExpressionParameter.LikeType.Contains
                    });
        }
        yield return parameter;
    }
 
    public override void LoadQueryStringParameters(System.Collections.Specialized.NameValueCollection parameters)
    {
        TextBox1.Text = parameters["TableTextSearch"];
    }
 
    public override System.Collections.Specialized.NameValueCollection SaveQueryStringParameters()
    {
        return new NameValueCollection() { { "TableTextSearch", TextBox1.Text } };
    }
 
    public override void Clear()
    {
        TextBox1.Text = string.Empty;
    }
}

To implement filters like this one you must use the DynamicFilterForm as opposed to the DynamicFilterRepeater.  The reason is that the TableTextSearch filter control is associated with any column/property in particular but the entire table instead.  It may make sense to allow the FilterAttribute to be annotated at the class level to address this.

<asp:DynamicFilterForm ID="DynamicFilterForm1" runat="server" DataSourceID="GridDataSource">
    <FilterTemplate>
        <div>
            Search
        </div>
        <div>
            Keyword: 
            <dd:TableTextSearch runat="server" ID="TableTextSearch" />
            <asp:LinkButton ID="LinkButton4" runat="server" CommandName="Search">Search</asp:LinkButton>&nbsp;&nbsp;
            <asp:LinkButton ID="LinkButton5" runat="server" CommandName="Clear">Clear</asp:LinkButton>
        </div>
    </FilterTemplate>
</asp:DynamicFilterForm>

To build upon this example it would be nice to be able to search the DisplayText of foreign columns and potentially non text values.  For example if the user entered a string which can be converted to a date, search all DateTime columns.

Posted in ASP.NET, Dynamic Data, LINQ | No Comments »

Dynamic Data Filtering 1.10 on CodePlex

Posted by jheyse on 30th September 2008

After almost a month of sporadic work on Dynamic Data Filtering I am happy to announce that I am releasing the next version.  This version includes several new features and a bug fix. It is downloadable from CodePlex

New Features

Dynamic Filter Repeater

Based on suggestions I have added a Dynamic Filter Repeater.  This control uses the FilterAttributes annontated in your MetaModel to automatically populate the list of filters for a given page.  This is most useful if you modify your PageTemplates/List.aspx & PageTemplates/ListDetails.aspx pages.  Filtering is OPT-IN!

Visual Studio Integration

Dynamic Data Filtering now has a design time experience.  I have added design time rendering to the DynamicFilterForm and new DynamicFilterRepeater.  The first time you drag either control on to the form the designer will automatically create the DynamicData/Filters folder and populate the folder with the default templates provided.  You will also see a designer action, "Upgrade DataSource", which will convert a LinqDataSource to a DynamicLinqDataSource.

I have also added item templates to the Add New Item… window which will add a filter to the DynamicData/Filters folder for you.  Currently I have templates for the standard equals operator and range.  I believe these are the two most common filters used, let me know if you’d like the others and I will add them in the next release.

Thank you David and Joe from the ASP.NET team for their help with the designer support.
Thank you Steve for the icon work.

Installer

Dynamic Data is now distributable via an MSI installer.  This installer will install to %Program Files%Dynamic Data Filtering by default.  Included in this folder are the binaries, samples and the default templates.  The installer also automatically adds the VS integrations mentioned above.  This was my first experience with WIX and I must say I am impressed and have found my new installer development technology.

Thank you James who helped convert the C# examples and templates to VB.

Bug Fixes

It was identified that multiple queries were being performed against the database on pages that had a DynamicFilterForm on them.  It was found that this was caused by the BaseDataSource.PerformSelect() method.  This method has been overridden to prevent this behavior in both the DynamicFilterForm and DynamicFilterRepeater.

Posted in ASP.NET, Dynamic Data, LINQ | No Comments »

A Richer DynamicDataFilter – Part 5 ColumnContains

Posted by jheyse on 23rd May 2008

I’m not really sure what to call this post, so it’s just the name of the control I wrote.  A reader left me the following message on a previous post during this series on the extended DynamicDataFilter:

Hi

I was wondering if you could guide me on how to do the following:

On every List page, I need a dropdownlist populated with  all the column names from the Table. Adjacent to that should be a textbox where the user can enter some text. And then a ‘Search’ button.

When clicked on this button, the following should happen:

Perform search on the Table where dropdownlist.selectedcolumnname CONTAINS textbox.text.

Repopulate Gridview with the search results.

I’d appreciate any help or atleast an idea on how to go about this. I’m new to Dynamic Data

Abby, unfortunately the functionality you are looking for isn’t currently supported in DynamicData, the main issue is that the LinqDataSource does not understand CONTAINS the where parameters you supply are all equals.  But, the DynamicData Filtering I have written does support this. (My code isn’t production quality, and should only be used in production at your own risk.)  Here is a quick screen shot:

ColumnContains

To allow for this I created a ColumnContains control which has a DrowDownList of the columns available in the DataSource’s Table and a TextBox which accepts your CONTAINS value.

<table>
    <tr>
        <td>
            <asp:DropDownList ID="ddlColumn" runat="server">
            </asp:DropDownList>
        </td>
        <td>
            <asp:TextBox ID="tbText" runat="server"></asp:TextBox>
        </td>
    </tr>
</table>

The code behind for the control is responsible for binding the list of string based columns to the DropDownList and creates the LikeExpressionParameter which is responsible for the LIKE query.

public partial class ColumnContains : Catalyst.Web.DynamicData.FilterTemplateUserControlBase
{
    protected void Page_Init(object sender, EventArgs e)
    {
        ddlColumn.DataTextField = "Name";
        ddlColumn.DataSource = Table.Columns.Where(c => c.TypeCode == TypeCode.String);
        ddlColumn.DataBind();
    }

    private MetaTable Table
    {
        get
        {
            IDynamicDataSource source = this.FindDataSourceControl();
            if(source != null)
                return source.GetTable();
            return null;
        }
    }

    public override IEnumerable<Parameter> GetWhereParameters(IDynamicDataSource dataSource)
    {
        yield return new LikeExpressionParameter()
            {
                Name = ddlColumn.SelectedValue,
                Like = LikeExpressionParameter.LikeType.Contains,
                Value = tbText.Text
            };
    }

    public MetaColumn Column
    {
        get
        {
            return Table.GetColumn(ddlColumn.SelectedValue);
        }
        set
        {
            ddlColumn.SelectedIndex = -1;
            if (value != null)
            {
                ListItem li = ddlColumn.Items.FindByValue(value.Name);
                if (li != null)
                    li.Selected = true;
            }
        }
    }

    public string Value
    {
        get
        {
            return tbText.Text;
        }
        set
        {
            tbText.Text = value;
        }
    }

    public override void LoadQueryStringParameters(NameValueCollection parameters)
    {
        string columnName = parameters[string.Format("{0}_column", this.ID)];
        Column = Table.GetColumn(columnName);
        Value = parameters[string.Format("{0}_value", this.ID)];
    }

    public override NameValueCollection SaveQueryStringParameters()
    {
        NameValueCollection collection = new NameValueCollection();
        collection.Add(string.Format("{0}_column", this.ID), Column.Name);
        collection.Add(string.Format("{0}_value", this.ID), Value);
        return collection;
    }

    public override void Clear()
    {
        Column = null;
        Value = string.Empty;
    }
}

Posted in ASP.NET, C#, Dynamic Data, LINQ | 1 Comment »

Safe Binding using Lambdas

Posted by jheyse on 19th May 2008

Friday I was discussing some of the new features in Visual Studio 2008 SP1 with some co-workers and one of them mentioned a new feature where VS 2008 will do symbolic renaming on all WCF Service Artifacts.  This lead to a conversation on how the IDE understands properties and methods differently then their corresponding names in XML configuration files (WCF) and string literals (data binding).  During the conversation I had a small light come on, regarding data binding and string literals. 

With .NET 3.5 it is possible to do compile time checking for data binding using Lambda expressions.  The idea is that instead of using a string to represent the name of the property you would pass in a Lambda Expression referencing that property.  Here is a simple example using a business object property that raises PropertyChanged.

public class Person : INotifyPropertyChanged
{
    private string _name;
 
    public string Name
    {
        get { return _name; }
        set
        {
            if (!object.Equals(_name, value))
            {
                _name = value;
                OnPropertyChanged(() => Name);
            }
        }
    }
 
    private void OnPropertyChanged(Expression<Func<object>> property)
    {
        MemberExpression me = property.Body as MemberExpression;
        if (me == null)
            throw new ArgumentException("property.Body must be of type MemberExpression", "property");
 
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(me.Member.Name));
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
}

The first benefit you get by doing this is that when you use the VS IDE refractoring tools to rename Name, the IDE will find Name in the Lambda expression and rename it there as well.  It is also possible to create type safe implementation using a generic base class:

public class BusinessBase<TObject> : INotifyPropertyChanged
{
    protected bool SetHelper<T>(Expression<Func<TObject, object>> property, ref T field, T value)
    {
        MemberExpression me = property.Body as MemberExpression;
        if (me == null)
            throw new ArgumentException("property.Body must be type MemberExpression", "property");
 
        if (!object.Equals(field, value))
        {
            field = value;
            OnPropertyChanged(me.Member.Name);
            return true;
        }
        return false;
    }
 
    private void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
 
}

and it’s corresponding implementation:

public class Person : BusinessBase<Person>
{
 
    private string _name;
 
    public string Name
    {
        get { return _name; }
        set { SetHelper<string>(p => p.Name, ref _name, value); }
    }
 
}

Honestly, I am not crazy about the second implementation which requires inheriting from a generic class and giving it a concrete implementation, which locks you into always being BusinessBase<Person> when you’ve extended Person to be Employee.  I will have a serious of blogs posts on some of these issues, with my Core series soon.

So anyway, enough with the business objects, lets look at databinding. Currently in .NET you create a binding between a control and an object using the following code:

this.firstTextBox.DataBindings.Add(new Binding("Text", this.personBindingSource, "First", true));

This loosely couples the Text property of the firstTextBox TextBox to the First property of some object, referenced by personBindingSource.  Once the designer generates this code, it is not checked against the actually objects.  Using Lambda Expressions the previous line of code could be implemented like:

this.firstTextBox.DataBindings.Add(
    new ExpressionBinding<TextBox, Person>(c => c.Text, this.personBindingSource, p => p.First, true));

where the definition of ExpressionBinding extends Binding:

public class ExpressionBinding<TControl, TDataSource> : Binding
    {
        public ExpressionBinding(Expression<Func<TControl, object>> property, object dataSource, Expression<Func<TDataSource, object>> dataMember)
            : this(property, dataSource, dataMember, false, DataSourceUpdateMode.OnValidation, null, string.Empty, null)
        { }
 
        public ExpressionBinding(Expression<Func<TControl, object>> property, object dataSource, Expression<Func<TDataSource, object>> dataMember, bool formattingEnabled)
            : this(property, dataSource, dataMember, formattingEnabled, DataSourceUpdateMode.OnValidation, null, string.Empty, null)
        { }
 
        public ExpressionBinding(Expression<Func<TControl, object>> property, object dataSource, Expression<Func<TDataSource, object>> dataMember, bool formattingEnabled, DataSourceUpdateMode dataSourceUpdateMode)
            : this(property, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode, null, string.Empty, null)
        { }
 
        public ExpressionBinding(Expression<Func<TControl, object>> property, object dataSource, Expression<Func<TDataSource, object>> dataMember, bool formattingEnabled, DataSourceUpdateMode dataSourceUpdateMode, object nullValue)
            : this(property, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode, nullValue, string.Empty, null)
        { }
 
        public ExpressionBinding(Expression<Func<TControl, object>> property, object dataSource, Expression<Func<TDataSource, object>> dataMember, bool formattingEnabled, DataSourceUpdateMode dataSourceUpdateMode, object nullValue, string formatString)
            : this(property, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode, nullValue, formatString, null)
        { }
 
        public ExpressionBinding(Expression<Func<TControl, object>> property, object dataSource, Expression<Func<TDataSource, object>> dataMember, bool formattingEnabled, DataSourceUpdateMode dataSourceUpdateMode, object nullValue, string formatString, IFormatProvider formatInfo)
            : base((property.Body as MemberExpression).Member.Name, dataSource, (dataMember.Body as MemberExpression).Member.Name, formattingEnabled, dataSourceUpdateMode, nullValue, formatString, formatInfo)
        { }
    }

Unfortunately, this implementation is not practical in WinForms since the current Visual Studio designer does not support lambda expressions for binding.   I would be interested to see if the WinForms team is looking into improved implementations of data binding with the 3.5 technologies. 

Posted in .NET, C#, LINQ, Visual Studio | No Comments »

A Richer DynamicFilterRepeater – Updated for April Release

Posted by jheyse on 11th April 2008

This week the Dynamic Data team has released a new drop of the runtime and templates.  It is substantially different and they have made many improvements to the design.  These changes, as expected, broke a significant portion of the Dynamic Filter controls I have been working on.  Last night I dug into the new bits and was able to breath life back into the dynamic filtering. 

Once I had finished doing symbolic renames on many of the properties and types renamed from the Decmeber CTP, I ran into several issues with the reflection based code I have written to inject the advanced querying capabilities into the existing controls.  I am going to start documenting what I have had to ‘hack’ to make the dynamic querying work.  Hopefully I can work with team members within Microsoft to promote some of the methods and types to the public. 

Here are some screen shots of the new implementation:

DD_4

DD_5

Download Solution: DynamicData.zip

Posted in .NET, ASP.NET, C#, Dynamic Data, LINQ | 1 Comment »

A Richer DynamicFilterRepeater – Part 4 Custom DynamicFilterControls & Expressions

Posted by jheyse on 11th April 2008

In post 2 in this series I contrived a scenario where composite parameters were used to select products from the AdventureWorks database by weight.  The weight of products is stored in either lbs or grams which make it necessary for the following SQL logic:

select
    *
from
    Production.Product
where
    (WeightUnitMeasureCode = 'LB' and Weight >= 2 and Weight <= 3) or
    (WeightUnitMeasureCode = 'G' and Weight >= 907 and Weight <= 1306)

Previously, this was accomplished by compounding a serious of And, Or, and Range expressions together.  We can condense this down to a single custom range parameter and corresponding FilterUserControl.

The first step is to create a user control which extends RangeFilterUserControlBase.  We will name it Weight and place it in the ~/App_Shared/DynamicDataFilters/ folder.  The control will allow for values in both Grams and Pounds to support globalization, so we need to add a UnitType property.  The ascx for the control defines tbMin, tbMax, and a DropDownList to allow the user to select either Pounds or Grams.  The control extends RangeFilterUserControlBase and overrides the default implementation of GetWhereParameters and returns a WeightExpressionParameter which we will define next.

public partial class Weight : Catalyst.Web.DynamicData.RangeFilterUserControlBase
{
    public enum WeightUnit
    {
        Pounds,
        Grams
    }

    public WeightUnit UnitType
    {
        get
        {
            return (WeightUnit)Enum.Parse(typeof(WeightUnit), DropDownList1.SelectedValue);
        }
        set
        {
            DropDownList1.Items.FindByValue(value.ToString()).Selected = true;
        }
    }

    public override string MinValue
    {
        get
        {
            return tbMin.Text;
        }
        set
        {
            tbMin.Text = value;
        }
    }

    public override string MaxValue
    {
        get
        {
            return tbMax.Text;
        }
        set
        {
            tbMax.Text = value;
        }
    }

    public override TypeCode Type
    {
        get { return TypeCode.Decimal; }
    }

    public override IEnumerable<Parameter> GetWhereParameters(IDynamicDataSource dataSource)
    {
        yield return new WeightExpressionParameter()
        {
            Name = this.DataField,
            MinValue = this.MinValue,
            MaxValue = this.MaxValue,
            UnitType = this.UnitType,
            Type = this.Type
        };
    }
}

The WeightExpressionParameter extends the RangeExpressionParameter and overrides the GetLambaExpression to return a LambdaExpression which will do the Grams to Pounds conversion for us.  The WeightExpressionParameter also includes a property for the UnitType which is stored in ViewState to ensure post backs and Ajax enabled controls work correctly.

public class WeightExpressionParameter : RangeExpressionParameter
{
    private const decimal ConversionFactor = 453.59237m;

    public WeightUnit UnitType
    {
        get
        {
            object obj2 = this.ViewState["UnitType"];
            if (obj2 == null)
            {
                return WeightUnit.Pounds;
            }
            return (WeightUnit)obj2;
        }
        set
        {
            if (this.UnitType != value)
            {
                this.ViewState["UnitType"] = value;
                this.OnParameterChanged();
            }
        }
    }

    public override LambdaExpression GetLambdaExpression(Type itType)
    {
        decimal? min = (decimal?)MinParameterValue;
        decimal? max = (decimal?)MaxParameterValue;

        if (UnitType == WeightUnit.Grams)
        {
            min /= ConversionFactor;
            max /= ConversionFactor;
        }

        if(itType == typeof(BusinessObjects.Product))
        {
            Expression<Func<BusinessObjects.Product, bool>> x = p =>
                (min == null || (p.WeightUnitMeasureCode == "lb" ? p.Weight : p.Weight / ConversionFactor) >= min) &&
                (max == null || (p.WeightUnitMeasureCode == "lb" ? p.Weight : p.Weight / ConversionFactor) <= max);
            return x;
        }
        else
            throw new ArgumentException("Expected itType to be of type BusinessObjects.Product", "itType");
    }
}

The LINQ-to-SQL interpreter is amazingly smart and is able to understand fairly complex statements. This SQL that is passed to the database server is exactly what we are looking for, it is even able to short circuit the query if either min or max is null!

SELECT TOP (10) [t0].[ProductID], [t0].[Name], [t0].[ProductNumber], [t0].[MakeFlag], [t0].[FinishedGoodsFlag], [t0].[Color], [t0].[SafetyStockLevel], [t0].[ReorderPoint], [t0].[StandardCost], [t0].[ListPrice], [t0].[Size], [t0].[SizeUnitMeasureCode], [t0].[WeightUnitMeasureCode], [t0].[Weight], [t0].[DaysToManufacture], [t0].[ProductLine], [t0].[Class], [t0].[Style], [t0].[ProductSubcategoryID], [t0].[ProductModelID], [t0].[SellStartDate], [t0].[SellEndDate], [t0].[DiscontinuedDate], [t0].[rowguid], [t0].[ModifiedDate], [t2].[test], [t2].[ProductSubcategoryID] AS [ProductSubcategoryID2], [t2].[ProductCategoryID], [t2].[Name] AS [Name2], [t2].[rowguid] AS [rowguid2], [t2].[ModifiedDate] AS [ModifiedDate2], [t4].[test] AS [test2], [t4].[ProductModelID] AS [ProductModelID2], [t4].[Name] AS [Name3], [t4].[CatalogDescription], [t4].[Instructions], [t4].[rowguid] AS [rowguid3], [t4].[ModifiedDate] AS [ModifiedDate3]
FROM [Production].[Product] AS [t0]
LEFT OUTER JOIN (
    SELECT 1 AS [test], [t1].[ProductSubcategoryID], [t1].[ProductCategoryID], [t1].[Name], [t1].[rowguid], [t1].[ModifiedDate]
    FROM [Production].[ProductSubcategory] AS [t1]
    ) AS [t2] ON [t2].[ProductSubcategoryID] = [t0].[ProductSubcategoryID]
LEFT OUTER JOIN (
    SELECT 1 AS [test], [t3].[ProductModelID], [t3].[Name], [t3].[CatalogDescription], [t3].[Instructions], [t3].[rowguid], [t3].[ModifiedDate]
    FROM [Production].[ProductModel] AS [t3]
    ) AS [t4] ON [t4].[ProductModelID] = [t0].[ProductModelID]
WHERE ((
    (CASE
        WHEN [t0].[WeightUnitMeasureCode] = @p0 THEN CONVERT(Decimal(29,5),[t0].[Weight])
        ELSE [t0].[Weight] / @p1
    END)) >= @p2) AND ((
    (CASE
        WHEN [t0].[WeightUnitMeasureCode] = @p3 THEN CONVERT(Decimal(29,5),[t0].[Weight])
        ELSE [t0].[Weight] / @p4
    END)) <= @p5)
-- @p0: Input NVarChar (Size = 2; Prec = 0; Scale = 0) [lb]
-- @p1: Input Decimal (Size = 0; Prec = 29; Scale = 5) [453.59237]
-- @p2: Input Decimal (Size = 0; Prec = 34; Scale = 5) [2]
-- @p3: Input NVarChar (Size = 2; Prec = 0; Scale = 0) [lb]
-- @p4: Input Decimal (Size = 0; Prec = 29; Scale = 5) [453.59237]
-- @p5: Input Decimal (Size = 0; Prec = 34; Scale = 5) [3]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8

Here is a screen shot of the control in action:

WeightFilterControl

Weight Filter Control Example: Weight.zip

Posted in .NET, ASP.NET, C#, Dynamic Data, LINQ | No Comments »

A Richer DynamicFilterRepeater – Part 3 DynamicFilterControl

Posted by jheyse on 10th April 2008

In part 1 of this series I discussed extending the LinqDataSource to provided advanced querying capabilities using the System.Linq.Dynamic namespace.  Part 2 demonstrated creating advanced WhereParameters which allowed for for complex binary logic.  In this posting I am going to go through our first ASP.NET Web Control which brings this powerful functionality to the user.

My original intention of this blog series was to create a more powerful DynamicFilterControl to replace the one currently in the Dynamic Data portion of the ASP.NET 3.5  Extensions.  During a recent project where we use the Dynamic Data framework, it became apparent that the repeater limited our layout ability and a DynamicFilterForm was better suited.  I choose the name DynamicFilterForm to follow the naming convention of the ASP.NET databound controls.

So, let’s start off with a demo of the the DynamicFilterForm.  In this demo I am building on the Products  example used in previous postings.  For the List.aspx page we’d like to be able to filter on the fields displayed in the DataGrid.

DynamicFilterForm_1[5]

The search allows for all of the fields, except weight, to be search on.  The Name, Product Number, Class and Color allow for partial string matches, ListPrice allows for a range, Subcategory uses a single selection drop down which is populated through the ForiegnKey, and Product Model allows for multiple selection.

The DynamicFilterForm allows for 3 commands: Search, Clear, and Browse.  (More on these later).

Let’s take a look at the ASP.NET code that runs the form:

<cc1:DynamicFilterForm DataSourceID="GridDataSource" runat="Server" ID="DynamicFilterForm1">
    <FilterTemplate>
        <div>
            Search
        </div>
        <table>
            <tr>
                <td>
                    Name:
                </td>
                <td>
                    <cc1:DynamicFilterControl ID="DynamicFilterControl3" runat="server" DataField="Name"
                        FilterMode="Contains" />
                </td>
                <td>
                    Product Number:
                </td>
                <td>
                    <cc1:DynamicFilterControl ID="DynamicFilterControl5" runat="server" DataField="ProductNumber"
                        FilterMode="Contains" />
                </td>
                <td>
                    Class:
                </td>
                <td>
                    <cc1:DynamicFilterControl ID="DynamicFilterControl6" runat="server" DataField="Class"
                        FilterMode="Contains" />
                </td>
                <td rowspan="2" valign="top">
                    Product Model:
                </td>
                <td rowspan="2" valign="top">
                    <cc1:DynamicFilterControl ID="DynamicFilterControl7" runat="server" DataField="ProductModel" FilterMode="MultiSelect" />
                </td>
                <td rowspan="2" valign="top">
                    <asp:LinkButton runat="server" CommandName="Search">Search</asp:LinkButton><br />
                    <asp:LinkButton runat="server" CommandName="Clear">Clear</asp:LinkButton><br />
                    <asp:LinkButton runat="server" CommandName="Browse">Browse</asp:LinkButton>
                </td>
            </tr>
            <tr>
                <td>
                    List Price:
                </td>
                <td>
                    <cc1:DynamicFilterControl ID="DynamicFilterControl1" runat="server" DataField="ListPrice"
                        FilterMode="Range" />
                </td>
                <td>
                    Color:
                </td>
                <td>
                    <cc1:DynamicFilterControl ID="DynamicFilterControl4" runat="server" DataField="Color"
                        FilterMode="Contains" />
                </td>
                <td>
                    Subcategory:
                </td>
                <td>
                    <cc1:DynamicFilterControl ID="DynamicFilterControl2" runat="server" DataField="ProductSubcategory" />
                </td>
            </tr>
        </table>
    </FilterTemplate>
</cc1:DynamicFilterForm>

DynamicFiltersFolder

The DynamicFilterForm mimics the functionality of the other DynamicData controls.  The DynamicFilterForm is a container object which has a FilterTemplate template that defines the layout.  Inside the FilterTemplate you define DynamicFilterControls.  These controls are responsible for loading the correct user control from the ~/App_Shared/DynamicDataFilters/ folder.  Here you can see I have created filter controls for each of the field types declared in the DynamicDataFields folder which are included in the base solution.

The DynamicFilterControl has a property FilterMode which allows for the following different enumeration values: Equals, Contains, Range, Multiselect.  This mode is appended to the field type like the DynamicControls.  So if you want to allow for a range of integers, the control name would be Integer_Range.ascx.  To facilitate quicker development and code reuse each of the custom DynamicExpressionParameters (EqualsExpressionParameter, InExpressionParameter, etc…) has a corresponding FilterTemplateUserControlBase(EqualsFilterUserControlBase, InFilterControlUserBase, etc…).

Here are the abstract methods for the FilterTemplateUserControlBase which need to be implemented.

public abstract class FilterTemplateUserControlBase : FieldTemplateUserControlBase, IWhereParametersProvider
{
    public abstract IEnumerable<Parameter> GetWhereParameters(IDynamicDataSource dataSource);
    public abstract void LoadQueryStringParameters(NameValueCollection parameters);
    public abstract NameValueCollection SaveQueryStringParameters();
    public abstract void Clear();

}

GetWhereParameters is the meat of this class, it is what is responsible for instantiating and returning the ExpressionParameter which is added to the DynamicLinqDataSource control’s where parameters.  Don’t you love the separation of concerns? I DO! The Clear method is called by the parent DynamicFilterForm when the Clear command is issued, this clears all values and researches to bring back the unfiltered result set.  LoadQueryStringParameters and SaveQueryStringParameters allow for the filter criteria to be stored in a URL.

<rant> One of my biggest annoyances on the web is developers who store state in session or form variables around searching.  Web users expect to be able to copy the URL of the page they are on and send it to their friend so that they can see the same thing. I run into this every time my friends and I are trying to plan a trip someplace and we are using Orbitz, Travelocity, etc… </rant>

Here is an example of the RangeFilterUserControlBase and the Integer_Range.ascx.cs control which extends it.

public abstract class RangeFilterUserControlBase : FilterTemplateUserControlBase
{
    public abstract string MinValue { get; set; }
    public abstract string MaxValue { get; set; }
    public abstract TypeCode Type { get; }

    public override IEnumerable<Parameter> GetWhereParameters(IDynamicDataSource dataSource)
    {
        yield return new RangeExpressionParameter() { Name = DataField, MinValue = MinValue, MaxValue = MaxValue, Type = Type };
    }

    public override void LoadQueryStringParameters(NameValueCollection parameters)
    {
        MinValue = parameters[string.Format("{0}_Min", DataField)];
        MaxValue = parameters[string.Format("{0}_Max", DataField)];
    }

    public override NameValueCollection SaveQueryStringParameters()
    {
        NameValueCollection parameters = new NameValueCollection();
        parameters.Add(string.Format("{0}_Min", DataField), MinValue);
        parameters.Add(string.Format("{0}_Max", DataField), MaxValue);
        return parameters;
    }

    public override void Clear()
    {
        MinValue = string.Empty;
        MaxValue = string.Empty;
    }
}

The Integer_Range.ascx control has two TextBoxes named tbMin and tbMax.

public partial class Integer_Range : Catalyst.Web.DynamicData.RangeFilterUserControlBase
{
    public override string MinValue
    {
        get
        {
            return tbMin.Text;
        }
        set
        {
            tbMin.Text = value;
        }
    }

    public override string MaxValue
    {
        get
        {
            return tbMax.Text;
        }
        set
        {
            tbMax.Text = value;
        }
    }

    public override TypeCode Type
    {
        get { return this.MetaMember.TypeCode; }
    }
}

As always, below is the source to the solution so far.  In my next post I will demonstrate creating your own FilterUserControl by re-implementing filtering on the Weight column.

Download Solution: DynamicData.zip

Posted in .NET, ASP.NET, C#, Dynamic Data, LINQ | No Comments »

Updated ASP.NET Dynamic Data Drop

Posted by jheyse on 10th April 2008

Scott Guthrie just posted about an updated version of the ASP.NET Dynamic Data framework.  I am going to look into this and investigate if there were any breaking changes to the Dynamic Filter control set.  I’m guessing there will be several since I am being a bad kid and using reflection to call non-public methods. LOL

Posted in .NET, ASP.NET, C#, Dynamic Data, LINQ | No Comments »

A Richer DynamicFilterRepeater: Part 2 – Advanced Parameters

Posted by admin on 5th April 2008

After extending the LinqDataSource in Part 1, I was curious to see what other types of parameters I could make.  My first idea was to create composite parameters for and/or operations.  The existing implementation of the LinqDataSource only allows you to append filter criteria to the list, which creates an AndAlso functionality.

Continuing with the AdventureWorks Products example, I devised a scenario where composite parameters would be useful. The Products in the AdventureWorks database allow you to store the weight of the item in either lbs or grams. This requires the use of a composite parameter to select all products which fall in between 2 – 3 lbs or the equivalent 907 – 1360 grams.  Since I have been playing with WPF and Silverlight I have taken a liking to the declarative syntax. The following query looked like this in my head:

<cc1:DynamicLinqDataSource ID="GridDataSource" runat="server">
    <WhereParameters>
        <cc1:OrExpressionParameter>
            <Parameters>
                <cc1:AndExpressionParameter>
                    <Parameters>
                        <cc1:EqualsExpressionParameter Name="WeightUnitMeasureCode" Value="LB" />
                        <cc1:RangeExpressionParameter Name="Weight" MinValue="2" MaxValue="3" Type="Int32" />
                    </Parameters>
                </cc1:AndExpressionParameter>
                <cc1:AndExpressionParameter>
                    <Parameters>
                        <cc1:EqualsExpressionParameter Name="WeightUnitMeasureCode" Value="G" />
                        <cc1:RangeExpressionParameter Name="Weight" MinValue="907" MaxValue="1360" Type="Int32" />
                    </Parameters>
                </cc1:AndExpressionParameter>
            </Parameters>
        </cc1:OrExpressionParameter>
    </WhereParameters>
</cc1:DynamicLinqDataSource>

Once I had the use case for the feature completed it was time to refactor my code to support the new functionality.  As I am writing this I am kicking myself for not creating some corresponding UnitTests for this little project.  If I have some time I will write up some tests before I release the code base to the public.

Anyway… To support the change in functionality I had to make several changes:

  • Modify IDyanmicExpressionParameter
  • Extend ParameterCollection
  • Create CompositeExpressionParameterBase

First, I had to modify the IDynamicExpressionParameter interface to support an additional method called GetLambdaExpression.  The reason for this is that we can’t just append to the query anymore, the composite parameters must compile their own binary tree of Lambda Expressions to work correctly.

public interface IDynamicExpressionParameter
{
    IQueryable AppendQuery(IQueryable query);

    LambdaExpression GetLambdaExpression(Type itType);
}

Next, I needed to extend the ParameterCollection to accept parameters beyond the built in ones with the .NET Framework.  Under my previous implementation parameters were not being tracked in ViewState and this was causing an issue with postbacks.  The DynamicParameterCollection accepts additional Parameter types, but still uses a hard coded list.  A better implementation would be to create a static list of knownTypes that appends new knownTypes as they are found.

public class DynamicParameterCollection : ParameterCollection
{
    private readonly Type[] _knownTypes = new Type[] { typeof(AndExpressionParameter),
                                                        typeof(OrExpressionParameter),
                                                        typeof(InExpressionParameter),
                                                        typeof(LikeExpressionParameter),
                                                        typeof(RangeExpressionParameter),
                                                        typeof(EqualsExpressionParameter) };

    protected override object CreateKnownType(int index)
    {
        int baseTypesLength = base.GetKnownTypes().Length;
        if(index < baseTypesLength)
            return base.CreateKnownType(index);

        return Activator.CreateInstance(_knownTypes[index - baseTypesLength]);
    }

    protected override Type[] GetKnownTypes()
    {
        List<Type> knownTypes = new List<Type>(base.GetKnownTypes());
        knownTypes.AddRange(_knownTypes);
        return knownTypes.ToArray();
    }
}

Lastly, I implemented CompositeExpressionParameterBase which supports both And and Or binary tree operations.  There were two key pieces to this class.

The first was wiring up the IStateManager interface to work properly with the parent DynamicParamterCollection.  This proved fairly difficult to debug before I remembered that I could now step into the .NET Framework using Visual Studio 2008. This was my first experience doing so, and I think it is a HUGE feature. The VS team rocks!

The next step was to implement binary trees as Lambda Expressions.  This forced me to get a little deeper into Lamdba Expressions and the ExpressionTreeVisualizer example paid off big time.  You can find the Debug Visualizer in the %PROGRAM FILES%Microsoft Visual Studio 9.0Samples1033CSharpSamples.zip directory.

LinqExpressionVisualizer

The code for creating a binary tree expression looks likes this:

protected LambdaExpression GetLambdaExpression(Type itType, ExpressionType binaryType)
{
    List<LambdaExpression> lambdas = new List<LambdaExpression>();
    foreach (Parameter p in Parameters)
    {
        LambdaExpression lambda = p.GetLambdaExpression(itType);
        if (lambda != null)
            lambdas.Add(lambda);
    }

    if (lambdas.Count == 0)
        return null;

    if (lambdas.Count == 1)
        return lambdas[0];

    LambdaExpression left = lambdas[0];
    LambdaExpression right = lambdas[1];

    ParameterExpression param = Expression.Parameter(left.Parameters[0].Type, "");
    BinaryExpression root = Expression.MakeBinary(binaryType, left.Body, right.Body);
    for (int i = 2; i < lambdas.Count; i++)
    {
        right = lambdas[i];
        root = Expression.MakeBinary(binaryType, root, right.Body);
    }

    left = Expression.Lambda(ReplaceParameter(root, param), param);
    return left;
}

Some other ideas for advanced parameters include a NotNullOrEmpty parameter and a Not parameter which inverses the result of the composite expression.  Up next I will talk about a writing web controls to expose this advance functionality to users.

Download Solution DynamicData.zip

Posted in .NET, ASP.NET, C#, Dynamic Data, LINQ | 1 Comment »

A Richer DynamicFilterRepeater: Part 1 – Extending the LinqDataSouce

Posted by admin on 27th February 2008

Download Solution – DynamicData.zip

I’ve recently started digging into the ASP.NET Dynamic Data Framework and evaluating its usage on a new RAD web site project. The project is a short duration web site with fairly standard data entry, searching, and detail views. The Dynamic Data Framework fit the requirements well except that the dynamic searching controls did not have the required features needed for the project. I started investigating how hard it would be add the following search features:

  • Searching ranges (ListPrice > 10 AND ListPrice < 500)
  • Searching in a list of possible values (Class in (‘L’, ‘M’))
  • Partial text searching (Color LIKE ‘B%’)

The DynamicFilterRepeater is the control provided with the Dynamic Data Framework which automatically gives the ListTemplate and ListDetailsTemplate the ability to filter on any foreign key found within the type of object listed. The DynamicFilterRepeater is a UI control which dynamically generates a list of DropDown controls for the user to select from. It doesn’t actually do any of the filtering, that responsibility is handled by the LinqDataSource.

The LinqDataSource is a new control in ASP.NET 3.5 which allows the developer to specify in markup the DataContext, table, and query parameters of a LinqQuery. Other databound controls, such as GridView, DetailsView, and FormView can then reference the datasource. I was surprised to find that the LinqDataSource only supported where parameters where the searching field and value must be equal. In addition you can only AND logic between parameters in the where clause, there is no ability to do OR. There is an option to type in a parameterized clause into the Where property and then specify the names of the parameters, but this is a dynamic data project and that doesn’t sound very dynamic, does it?

No worries, Reflector was to the rescue. I popped open reflector and dug into the System.Web.Extensions 3.6 3.6 dll. First thing I noticed was their extensive use of yield statement’s which does not make for easily read code. Secondly, I saw that the ASP.NET development team did an excellent job of separating the LinqDataSource UI control, from the LinqDataSourceView which actually creates the query. After digging around a while, I found it was possible to replace the LinqDataSourceView using reflection. :)

The current architecture of the LinqDataSource and the Parameters puts all of the querying logic into the LinqDataSourceView.  This makes it extremely hard to extend and add additional capabilities.  I suggest a new design which passes the query creation responsibility on to the individual parameters.  This can be accomplished through the following interface:

public interface IDynamicExpressionParameter
{
    IQueryable AppendDynamicQuery(IQueryable query);
}

Two of the three DynamicExpressionParameters can use the System.Linq.Dynamic.DynamicQuery class.  This class isn’t in the .NET Framework but you can find it in the examples installed with Visual Studio 2008, C:Program FilesMicrosoft Visual Studio 9.0Samples1033CSharpSamples.zipLinqSamplesDynamicQuery. There are several excellent examples in here including a ExpressionTreeVisualizer.

This is an example of the RangeExpressionValidator.

public class RangeExpressionParameter : DynamicExpressionParameterBase
    {
        [DefaultValue("")]
        public string MinValue { get; set; }
 
        [DefaultValue("")]
        public string MaxValue { get; set; }
 
        public override IQueryable AppendDynamicQuery(IQueryable query)
        {
            if (!string.IsNullOrEmpty(MinValue) && !string.IsNullOrEmpty(MaxValue))
                return query.Where(string.Format("{0} >= @0 AND {0} <= @1", this.Name), new object[] { MinParameterValue, MaxParameterValue });
            else if (!string.IsNullOrEmpty(MinValue))
                return query.Where(string.Format("{0} >= @0", this.Name), new object[] { MinParameterValue });
            else if (!string.IsNullOrEmpty(MaxValue))
                return query.Where(string.Format("{0} <= @0", this.Name), new object[] { MaxParameterValue });
            else
                return query;
        }
 
        private object MinParameterValue
        {
            get
            {
                return GetValue(MinValue, null, Type, this.ConvertEmptyStringToNull, false);
            }
        }
 
        private object MaxParameterValue
        {
            get
            {
                return GetValue(MaxValue, null, Type, this.ConvertEmptyStringToNull, false);
            }
        }
    }

Here is some example markup from the aspx pages of both a standard Web site and a Dynamic Data Extensions Web Site.

<cc1:DynamicLinqDataSource ID="DynamicLinqDataSource1"  ContextTypeName="BusinessObjects.AdventureWorksDataContext" 
    TableName="Products" runat="server" EnableDelete="True" EnableInsert="True" EnableUpdate="True">
    <WhereParameters>
        <cc1:RangeExpressionParameter Name="ListPrice" MinValue="10" MaxValue="500" Type="Decimal" />
        <cc1:InExpressionParameter Name="Class" ValueType="String" ValueList="L,M" />
        <cc1:LikeExpressionParameter Name="Color" Like="StartsWith" Value="B" />
    </WhereParameters>
</cc1:DynamicLinqDataSource>

These screen shots show the above expressions in action.  The DynamicLinqDataSource works exactly like the standard LinqDataSource with the addition of the Expression Parameters.

WebPage[1] DynamicData

I am attaching the source code for the solution to this point.  My next steps are to extend the ExpressionParameter to create a CompositeExtension parameter which accepts AND / OR operations between parameters which will allow for even more complex search criteria. 

Posted in ASP.NET, Dynamic Data, LINQ | 1 Comment »