Exploring Dynamic Data: Textual attributes for business logic
Index to this series of articles
Business logic is applied to your Entity classes (objects that describe individual tables where columns are the properties) through metadata. Typically this metadata is defined by applying attributes from the System.ComponentModel.DataAnnotations namespace.
Each column has a name taken from the database. Yet there are many ways to display text about the column which does not match the column’s name. The DisplayAttribute (introduced in ASP.NET 4) provides the following properties that are available to your user interface:
- Name – The column’s name corrected to reflect language and formatting issues.
- ShortName – A shorter version of the Name.
- Description – A description that may appear in a hint, tooltip or help label.
- Prompt – A description that works well as a prompt, such as in a wizard or the label preceding a textbox.
This attribute supports resource based localization.
[Display(Name="Category", Prompt="Enter the category name")]
public object CategoryName { get; set; }
If you are still using ASP.NET 3.5, you can define the Name and Description from the System.ComponentModel.DisplayNameAttribute and System.ComponentModel.DescriptionAttribute classes. The rest are not available.
Naming for tables
Like a column, each table has a name taken from the database. If you want to display the table name, it may need an alternative version that reflects language and formatting issues.
[DisplayName("Categories")]
public class Category
{
}
Peter’s Soapbox
I have mixed feelings about the DisplayAttribute introduced in .net 4. I feel it mixes too many concepts together and these concepts already had their own attributes.
- Text – Already in DisplayNameAttribute and DescriptionAttribute. No support for ShortName and Prompt in the past though.
- Scaffolding (the “AutogenerateField” property) – Allows business logic to dictate if the column appears. There already exists ScaffoldColumnAttribute, although this new version introduces the Order and GroupName properties to determine the column’s position.
I think that the new properties should have been added to the ScaffoldColumnAttribute. I foresee are additional rules for scaffolding which should appear in the framework, such as defining a list of columns that match a specific category. The ScaffoldColumnAttribute makes sense for this kind of growth.
- Filter scaffolding (the “AutoGenerateFilter” property). Its another aspect to scaffolding that impacts generating a list of filters in the QueryableFilterRepeater control. This probably belongs in the ScaffoldColumnAttribute so it can share the Order and GroupName properties.
Again, there are probably other rules for scaffolding to come for filters. One is already defined in a separate attribute, FilterUIHintAttribute, which helps select the user interface used for filtering, such as setting up a range or a checkboxlist.