Databases Reference
In-Depth Information
LISTING 13.6
Continued
{
this.Name = descriptor.Name;
this.ColumnType = descriptor.PropertyType;
this.Nullable = this.ColumnType.IsValueType == false ||
System.Nullable.GetUnderlyingType(this.ColumnType) != null;
this.IsReadOnly = descriptor.IsReadOnly;
this.IsCustomProperty = true;
}
}
}
Extending the TableProvider to Support Custom Properties
The
UnleashedTableProvider
class inherits from the built-in
TableProvider
and encapsu-
lates the metadata information describing a single entity type, such as Product. Because on
one hand, the Entity Framework and LINQ to SQL model providers are internal and on
the other hand they contain quite a bit of logic, you do not want to simply reimplement
them. Instead, the
UnleashedTableProvider
delegates implementation of most of its
virtual properties and methods to the
original
TableProvider
object it receives as the
second parameter in its constructor.
The only property that does not simply return the original value is
Columns
. Instead, the
UnleashedTableProvider
constructor takes a collection of
ColumnProvider
objects
returned by the original
Columns
property and adds to it the
UnleashedColumnProvider
objects for each custom property defined in the entity class. This combined collection is
stored in a private field called
columns
and returned by the overridden
Columns
property.
You can see the complete source code of the
UnleashedTableProvider
in Listing 13.7 with
points worthy of attention shown in bold.
LISTING 13.7
UnleashedTableProvider Implementation
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Security.Principal;
using System.Web.DynamicData.ModelProviders;
namespace Unleashed.DynamicData
{
public sealed class UnleashedTableProvider: TableProvider
{