Databases Reference
In-Depth Information
NOTE
Remember that by using the
UIHintAttribute
, you add
presentation
logic to the
busi-
ness
layer of your application. Although placing business logic
code
in the code-behind
files of a web application clearly is undesirable as it quickly leads to code duplication
and makes automated testing difficult, combining presentation and business
models
is
not necessarily a bad idea. You could argue that consistently using a
UIHint
instead of
a
DataType
annotation helps to clarify your design intent in the model—a tangible
benefit. However, every time you use the
UIHint
annotation, more and more of the
presentation logic is mixed in with the business logic, making the model more complex.
Use this as an opportunity to stop and consider whether creating a custom page or
template for the given entity and
separating
the presentation logic from the business
model would make the overall design simpler and easier to maintain.
As discussed in Chapter 10,
DynamicControl
can initialize custom properties of field
templates using matching attributes specified in markup. As an example, the built-in
Text_Edit
field template was extended with a custom
Columns
property that can be used
to set the
Columns
property of the
TextBox
control it contains. If you had created a custom
entity or page template for the Employee entity, you could make the Address text box
wider and accommodate longer addresses by using the following markup:
<asp:DynamicControl runat=”server” DataField=”Address”
Columns=”65”
/>
The
UIHintAttribute
also supports custom property values. They are exposed for
consumption via its
ControlParameters
property, which returns an
IDictionary<string,
object>
, and have to be specified as a sequence of names and values in the
UIHintAttribute
constructor, following the hint and presentation layer parameters. It is
easier to explain this with an example; following is how you could specify the
Columns
parameter for the
Address
property in the metadata:
[MetadataType(typeof(Metadata))]
partial class Employee
{
private abstract class Metadata
{
[UIHint(“Text”, “WebForms”, “Columns”, 65)]
public object Address { get; set; }
}
}
In this example,
“
Text
”
is the hint, the name of a field template; “
WebForms
”
is the name
of the presentation layer for which the
UIHint
is intended; and
“
Columns
”
is the name of
the first control parameter followed by
65
, its value. The
UIHintAttribute
constructor has