Databases Reference
In-Depth Information
public class MetaModel
{
public void RegisterContext(Type contextType);
public void RegisterContext(DataModelProvider dataModelProvider);
}
To enable support for custom properties, the Decorator design pattern can be applied to
create a set of provider classes that wrap the original Entity Framework provider objects
and provide additional metadata information on top of what is available out of the box.
In the sample solution, a class called
UnleashedColumnProvider
encapsulates custom
entity properties. The
UnleashedTableProvider
class wraps a built-in
TableProvider
object provided by Dynamic Data and returns a
Columns
collection populated with
ColumnProvider
objects representing the properties defined in the EDMX file plus
the
UnleashedColumnProvider
objects representing custom properties defined in the
partial classes of the entity model. Finally, the
UnleashedModelProvider
wraps a
DataModelProvider
, enabling you to replace the limited built-in provider with the
extended one, which supports custom properties.
Creating a Custom Property ColumnProvider
The
UnleashedColumnProvider
class inherits from the built-in
ColumnProvider
class and
encapsulates metadata about a single custom property. As you see shortly, when the
chapter gets into the implementation of the
UnleashedTableProvider
, it relies on the
TypeDescriptor
API to extract property metadata provided by the .NET run time. As you
recall from the discussion in Chapter 7, the
TypeDescriptor
API is defined in the
System.ComponentModel
namespace. Both Dynamic Data and the validation framework rely
on the
TypeDescriptor
instead of the low-level .NET Reflection API it extends.
The complete source code of the
UnleashedColumnProvider
is shown in Listing 13.6.
Although most of the members in the
ColumnProvider
base class are virtual, they don't
have to be overridden. Instead, the constructor of the
UnleashedColumnProvider
class
takes a
PropertyDescriptor
object as its second parameter and initializes the properties,
such as the
Name
and
ColumnType
. The first parameter of the constructor supplies a
TableProvider
object, required by the only constructor available in the
ColumnProvider
base class.
LISTING 13.6
UnleashedColumnProvider Implementation
using System.ComponentModel;
using System.Web.DynamicData.ModelProviders;
namespace WebApplication
{
public sealed class UnleashedColumnProvider: ColumnProvider
{
public UnleashedColumnProvider(
UnleashedTableProvider table, PropertyDescriptor descriptor)
: base(table)