Databases Reference
In-Depth Information
LISTING 13.10 CustomPropertyAttribute Implementation
using System;
namespace Unleashed.DataAnnotations
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class CustomPropertyAttribute: Attribute
{
public string Expression { get; set; }
}
}
Extending Dynamic Data to Support Sort Expression Metadata
The first task is to let Dynamic Data know that a custom property with an expression
specified is sortable. This can be done in the UnleashedColumnProvider class, by setting its
IsSortable property to true. The code snippet that follows shows how this can be done
(only the new code is shown; you can see the rest of the code indicated by the ellipsis
back in Listing 13.6):
public UnleashedColumnProvider(
UnleashedTableProvider table, PropertyDescriptor descriptor)
: base(table)
{
// ...
this. IsSortable = descriptor.Attributes.OfType<CustomPropertyAttribute>()
.Any(a => !string.IsNullOrEmpty(a.Expression));
}
Unfortunately, the current version of Dynamic Data does not allow you to specify the
actual sort expression through the metadata. On one hand, the ColumnProvider base class
does not have a SortExpression property you can set. On the other hand, although the
MetaColumn class does have a SortExpression property, it is nonvirtual and cannot be over-
ridden. MetaColumn assumes that the sort expression always matches the name of the prop-
erty returned by its ColumnProvider . Although you can have the UnleashedColumnProvider
use the expression of the custom property as its name, it leads to other problems and
makes working with metadata more confusing.
Luckily, DataControlField (the base class used by the GridView control to represent
columns) defines its SortExpression property as virtual. The DynamicField class provided
by Dynamic Data actually overrides this property to get SortExpression from its
MetaColumn by default. To work around the limitation in the metadata and provider APIs,
this property was overridden in the UnleashedField created earlier to get the expression
 
Search WWH ::




Custom Search