ASPNET Dynamic Data
Beyond using LightSpeed as the domain model in a web application, your LightSpeed entities can be used to create ASP.NET Dynamic Data sites.
To create a Dynamic Data site using LightSpeed:
· Create an ASP.NET Dynamic Data site using the Visual Studio New Project command.
· Add a LightSpeed model to the project.
· Add a reference to Mindscape.LightSpeed.Web.DynamicData.dll.
· Open the DynamicData > PageTemplates directory. For each .aspx page in this directory:
· Add the following directive immediately below the @Page directive
<%@Register TagPrefix="ls" Assembly="Mindscape.LightSpeed.Web.DynamicData" Namespace="Mindscape.LightSpeed.Web.DynamicData" %> |
· Find any references on the page to asp:LinqDataSource and change them to ls:LightSpeedLinqDataSource. Do not change any attributes or other content.
· Declare a LightSpeedContext<MyModelUnitOfWork>. This is typically declared as a static read-only field in Global.asax.cs.
Example declaration of a static LightSpeedContext in Global.asax.cs |
public static LightSpeedContext<MyModelUnitOfWork> LightSpeedContext |
· In Global.asax.cs, locate the section entitled “IMPORTANT: DATA MODEL REGISTRATION” and add the following line in place of the commented-out call to model.RegisterContext:
Dynamic Data Model Registration |
model.RegisterContext( |
Once you have completed these steps your Dynamic Data site will be configured to use the LightSpeed data source. You can then customise the appearance of your site and your entities using the usual Dynamic Data customisation techniques.
Handling Associations
When presenting associations, ASP.NET Dynamic Data calls ToString() on the associated object. The ToString() implementation in the Entity class returns a generic string which is not very friendly to end users. You should override ToString() in any class you want to display on a Dynamic Data site.
Validation
By default, ASP.NET Dynamic Data does not recognise LightSpeed validation attributes. Although validations still run, they run only on the server and ASP.NET Dynamic Data does not display validation error messages in a usable form.
To enable integrated ASP.NET Dynamic Data validation, including client-side validation and error message display, when registering the data context, set the ConfigurationContext.MetadataProviderFactory to a call back which creates an instance of EntityDataAnnotationProvider. You will need to add a reference to Mindscape.LightSpeed.Web to bring this class into scope.
Dynamic Data Model Registration |
model.RegisterContext( |
(Please notice the use of AssociatedMetadataTypeTypeDescriptionProvider as an “inner” provider. This is required to enable “buddy classes” to work in cases where these are needed.)
EntityDataAnnotationProvider does not surface all LightSpeed validations because ASP.NET Dynamic Data does not contain equivalents for all of them. Validations which are not surfaced still run, but without the integrated user experience. The following LightSpeed validations are surfaced:
· ValidatePresenceAttribute
· ValidateLengthAttribute
· ValidateRangeAttribute (int and double ranges only)
· ValidateFormatAttribute
In addition, EntityDataAnnotationProvider depends on the use of designer naming conventions to locate validations (because LightSpeed validations are specified on fields but must be surfaced on properties). That is, the backing field for each property must have the same name as the property, prefixed with an underscore. If you have hand-coded entities which do not follow this convention, you will need to add Dynamic Data validation attributes to your properties by hand.