Databases Reference
In-Depth Information
Building Dynamic UI with Custom Properties
Chapter 11 discussed how by automatically generating a reasonably good user interface at
run time based on metadata information, Dynamic Data allows you to rapidly evolve the
entity model. This approach helps delay implementation of the presentation code while at
the same time helping to collect feedback from the business users working with a real
application and not just screen mockups or diagrams.
Both Entity Framework and LINQ to SQL enable you to extend entity classes and define
custom properties, such as calculated properties or properties that return data from related
entities. Custom properties are a powerful tool that allows you to extend the entity model
beyond the limits imposed by the current tools for Entity Framework and LINQ to SQL.
For instance, you could extend the Product entity class with a custom property called
Country that returns the country of the Supplier:
partial class Product
{
public string Country { get { return this.Supplier.Country; } }
}
With a custom property defined, you can easily have a Country column automatically
generated on a Product list page. Listing 13.5 shows markup of a custom page that displays
a GridView control with plain-text columns for all public properties of the Product class,
including the custom Country property just defined. The only special steps needed are to
set the ContextTypeName property of the EntityDataSource control to enable lazy loading,
which the property relies on, and set the EnableFlattening property to false , which
prevents the data source control from generating special wrappers for each entity object.
Entity wrappers are used with the initial release of Entity Framework, which did not
support foreign key properties. Today, they are not necessary and only prevent data bound
controls, such as the GridView , from accessing custom properties defined in the entity
class.
LISTING 13.5 List Page with Plain-Text Columns for Custom Properties
<%@Page Language=”C#” MasterPageFile=”~/Site.master” CodeBehind=”SamplePage.aspx.cs”
Inherits=”WebApplication.Samples.Ch13.CustomProperty.SamplePage” %>
<asp:Content ContentPlaceHolderID=”main” runat=”server”>
<asp:GridView runat=”server” DataSourceID=”dataSource” />
<asp:EntityDataSource runat=”server” ID=”dataSource”
ContextTypeName=”DataModel.NorthwindEntities”
EntitySetName=”Products” EnableFlattening=”False” />
</asp:Content>
Using custom properties with Dynamic Data can help reduce the need for creating custom
pages too early in web application projects. In some cases, custom properties make sense
from the entity modeling prospective, making highly normalized entity models more
 
 
Search WWH ::




Custom Search