Databases Reference
In-Depth Information
NOTE
As discussed in Chapter 7, “Metadata API,” the
MetaColumn
class encapsulates data
annotation attributes applied to the entity property. Although it allows accessing data
annotation attributes, using its
Attributes
property, it also provides strongly typed
properties to access specific annotations directly. For example, the
DisplayName
and
Prompt
properties of the
MetaColumn
class return values specified in the
Name
and
Prompt
properties of the
DisplayAttribute
, respectively. Using these properties is
not only easier, it is also faster due to metadata caching performed by Dynamic Data.
NOTE
The
??
operator
is a C# null-coalescing operator. It's a powerful shortcut but can be
confusing if you are not familiar with it. Here is how a single line of code from the
snippet just shown can be replaced with a more verbose, but straightforward,
if
state-
ment
:
if (this.currentColumn.Prompt != null)
label.Text = this.currentColumn.Prompt;
else
label.Text = this.currentColumn.DisplayName;
After implementing support for the
Prompt
annotation in the dynamic entity template,
you can further refine the definition of the Employee entity and specify a custom string
for use with the
HomePhone
property in form labels as shown next. Also the
RequiredAttribute
is added to the HomePhone property to help illustrate the fact that
the
Name
annotation is still important because it is used to generate validation error
messages.
[MetadataType(typeof(Metadata))]
partial class Employee
{
private abstract class Metadata
{
[Required]
[Display(Name = “Home Phone”, ShortName = “Home #”,
Prompt = “Home Phone Number:”)]
public object HomePhone { get; set; }
}
}
Notice how the
Name
,
ShortName
, and
Prompt
annotations look in a dynamically generated
UI in Figure 11.2. At the top of the picture, you can see a fragment of a dynamic form
where the
Prompt
annotation appears as a label to the left of the phone number text box.