Exploring Dynamic Data: Scaffolding 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.
Scaffolding is a term that describes letting the business logic determine the list of columns and tables displayed in your user interface. You see it in action when using Dynamic Data’s Page Templates. Yet, it is fine to explicitly define the list of columns in your user interface code, especially when you need flexibility in your layout from column to column.
The ScaffoldColumnAttribute and DisplayAttribute are both used to identify if a column is included or excluded. ScaffoldColumnAttribute was introduced first and has fewer capabilities. DisplayNameAttribute is new to .net 4, adding the Order and GroupName properties to the scaffolding rules.
[ScaffoldColumn(true)]
public object Picture { get; set; }
or
[Display(AutoGenerateField=true)]
public object Picture { get; set; }
You can read about my complaints of the DisplayAttribute’s design in the previous post. See the “Peter’s Soapbox” section.
When no attribute is supplied
When neither of these attributes are assigned, the Dynamic Data scaffolding engine determines if the field shows based on the following rules:
- Included: A Field Template name is specified in the UIHintAttribute.
- Excluded: MetaColumn.IsForeignKeyComponent = true (Part of a ForeignKeyColumn)
- Excluded: MetaColumn.IsGenerated = true (The column’s data is generated by the database)
- Included: MetaColumn.IsPrimaryKey = true (The column is a primary key)
- Excluded: MetaColumn.IsCustomProperty = true (The column was created in the EntityTable class, but does not exist in the database)
- Included: MetaColumnType is one of these: string, char, integer, floating point, bool, DateTime, TimeSpan, or DateTimeOffset.
- Excluded: Anything not listed above.
Ordering the scaffolded columns
By default, Dynamic Data shows the columns in the same order they appear in the Entity class. If you want to change the order, use the DisplayAttribute. Its Order and GroupName properties are used by the scaffolding engine to sort the columns.
Scaffolding Tables
Like columns, Dynamic Data can provide a specific list of tables to show. Use the ScaffoldTableAttribute to exclude a table. When initializing Dynamic Data in Application_Start(), use the ScaffoldAllTables property to include all tables that are not set to [ScaffoldTableAttribute(false)].
DefaultModel.RegisterContext(typeof(NorthwindModel.Entities),
new ContextConfiguration() { ScaffoldAllTables = true });
This attribute impacts two features of Dynamic Data:
- The “entry point” web form, which is Default.aspx, initially, can show this table. It internally uses the MetaModel.VisibleTables property to get a list of tables. Be aware that this list is ordered by how the Tables were defined by the ModelProvider class to the MetaModel class. If you want to control the ordering yourself, create a list of MetaTables in the desired order instead of using MetaModel.VisibleTables.
- Url Routing will prevent Urls that contain table names that are not scaffolded.
Scaffolding for Filters
List oriented interfaces often have filters to let the user modify the query used to generate the list. Your business logic can assist your user interface in building filters with these two attributes:
- DisplayAttribute – Use the AutoGenerateFilter property to include or exclude the column from your filter interface.
- FilterUIHintAttribute – Specifies an alternative Filter Template file (a violation of the separation of concerns).
ASP.NET Dynamic Data uses this with its QueryableFilterRepeater control to generate a user interface based on business logic.
When no DisplayAttribute is supplied
When the DisplayAttribute is not assigned, the Dynamic Data scaffolding engine determines if the filter shows based on the following rules:
- Included: A Filter Template name is specified in the FilterUIHintAttribute.
- Excluded: The column is not scaffolded (see the earlier scaffolding rules for fields)
- Excluded: MetaColumn.IsCustomProperty = true (The column was created in the EntityTable class, but does not exist in the database)
- Included: MetaColumnType is one of these: bool, enumerated type
- Excluded: Anything not listed above.