Where does this go? Applying SoC to dynamic data – Part 1
Since a major goal of Dynamic Data is to separate the business logic from the user interface, let’s look at a few areas where these concepts still blend and their alternatives.
Today we look at the…
UIHintAttribute
Attributes like System.ComponentModel.DataAnnotations.UIHintAttribute are placed on the properties of your Entity class. That’s the business layer. Yet UIHintAttribute explicitly determines which Field Template is used. The Field Template is in the UI layer.
The business layer should influence the user interface into picking a Field Template, but it shouldn’t know anything about specific Field Templates. It’s influence comes from identifying the data type of the property (table column), such as a date or integer. The business layer has two ways to identify the type:
- The DataTypeAttribute
- The property’s type (when there is no DataTypeAttribute assigned)
public class Products
{
public string ProductName { get; set; }
[DataType(DataType.Currency)]
public decimal UnitPrice { get; set; }
[DataType(DataType.Date)]
public DateTime RestockDate { get; set; }
}
Your app will have many Field Template files whose file name matches a data type, like “Text.ascx” (for string types), “Currency.ascx” and “Date.ascx”. These are the default user interfaces for a data type. If you need something different from the default, create a new Field Template file and give it a unique name. Perhaps you want a Date label to be formatted using the long date pattern. You might name your new Field Template “DateLongPattern.ascx”.
How do you fix this?
Here’s how to replace the UIHintAttribute.
- Use the DataTypeAttribute to specify a general type. What about the case when the enumerated type System.ComponentModel.DataAnnotations.DataType does not have an element to match your needs? Initially you would think to use the UIHintAttribute. Instead, pass a string name of the type to the DataTypeAttribute.
[DataType("Measurement")]
public decimal Distance { get; set; } - The user interface developer specifies the Field Template’s name in the UIHint property found on the DynamicControl and DynamicField objects.
<asp:DynamicControl id="RestockDate" runat="server" UIHint="DateLongPattern" />