Databases Reference
In-Depth Information
Notice that the order of columns in the grid has been modified to make both the filter
and the grid column fit on the page without the user needing to scroll. This brings up an
important point you might want to consider when using custom properties along with the
metadata classes in your project. Here is a larger extract from the
Product
entity class,
which shows how the
DisplayAttribute
was applied to the
ProductName
and
Country
properties to change their default order:
[MetadataType(typeof(Metadata))]
partial class Product
{
[Display(Order = 20)]
public string Country { get { return this.Supplier.Country; } }
private abstract class Metadata
{
[Display(Name = “Product Name”, Order = 10)]
public object ProductName { get; set; }
}
}
With entity classes generated automatically from the model definition by Entity
Framework or LINQ to SQL, you normally use a metadata class to specify data annota-
tions, such as the
DisplayAttribute
, for regular entity properties. The custom properties,
on the other hand, are created manually in the partial class, and you can apply data anno-
tations directly. Which approach is better?
In the example just given, placing the
DisplayAttribute
directly above the definition of
the
Country
property allows you to have all of its annotations in a single place (the
UIHint
and
FilterUIHint
attributes are not shown to make the example shorter). However, it also
splits the information about the order of properties between the metadata class and the
entity class itself. Notice how the
Country
property appears first in the source file and
second in the dynamically generated web page. Alternatively, you could also consolidate
data annotations for
all
properties in the metadata class, as demonstrated in the next code
sample. While this approach brings the order of properties in the metadata class in sync
with their order on the dynamically generated pages, definition of the
Country
property is
now split between two different places:
[MetadataType(typeof(Metadata))]
partial class Product
{
public string Country { get { return this.Supplier.Country; } }
private abstract class Metadata
{
[Display(Name = “Product Name”, Order = 10)]
public object ProductName { get; set; }