ASP.Net 3.5 SP1-Dynamic Data
Michał Morciniec – GPSD [email protected]
Agenda
• Dynamic Data– Concepts and Architecture
• Project creation in VS 2008 SP1
• Dynamic Data Website Elements– Page Templates, Page Modes
– Field Tempates
• Customization– Custom Page Templates, Custom Field Templates
– MetaData application, data validation
• Dynamic Data Wizard
Architecture
Data Source Data Model Data ModelMeta-dataapplication
Page generateddynamically
LIN
Q t
oSQ
LPage and Field Templates
Tables, columns,constraints
Entities, Field,Classes, Metadata
Partial classes,Attributes
Dynamic Data Concepts
• Makes easy to create data driven web sites(operaciones CRUD)
• UI is inferred from the Data Model
• Data Model uses metadata to describe the structureand data relationships.
• Basic data validation rules are imposed automatically
• Pages are generated automatically based ontemplates
• URL Routing feature is used to map URLs to pages.
Dynamic Data WebSite – Creation 1
• Use template“Dynamic Data Web Site”
• Web Site Project contains “DynamicData” folder
Dynamic Data WebSite – Creation 2
• Add LINQ to SQL o LINQ to Entityclasses to project
• Note - App_Codefolder is added withthe Data Model(.dbml)
Dynamic Data WebSite – Creation 3
• Create data connection, dragtables to designer of Data Model
• Data Model (classes ) is generated in “code-behind” associatesdwith the model(.designer.cs)
Dynamic Data WebSite – Creation 4
• Configure Data Model class (a.k.a.DataContext) in Global.asax in RegisterRoutes()
MetaModel model=new MetaModel();
model.RegisterContext(
typeof(NorthwindDataContext),
new ContextConfiguration()
{ ScaffoldAllTables = true }
);
Dynamic Data-Creation DEMO
Page Templates
• Located in DynamicData\Page Templates
• Page Uses Controls-– DynamicDataManager
– DynamicValidator
– FilterRepeater
– DynamicControl
– DynamicField
• GridView used to show data.
Page Modes
• Separate-Page Mode (default)
– Separate pages used to list, edit and insert new data
• Combined-Page Mode
– Data is manipulated in the same page
• Different Page Modes can be associated withdifferent tables (entities)
Separate-Page Mode
• Uses following route in Global.asax– {table}/{action}.
• {action} can be– Edit
– List
– Details
– Insert
• Every operation uses different page– No “in-line” editing
routes.Add(
new DynamicDataRoute("{table}/{action}.aspx")
{
Constraints = new RouteValueDictionary(
new { action = "List|Details|Edit|Insert" }),
Model = model
});
Combined Page Mode
• Uses following routes in Global.asax– {table}/ListDetails.aspx
• Same page ListDetails.aspx used to manipulate data and show detail
– “in-line” editing
routes.Add(
new DynamicDataRoute("{table}/ListDetails.aspx")
{ Action = PageAction.List,
ViewName = "ListDetails",
Model = model
});
routes.Add(
new DynamicDataRoute("{table}/ListDetails.aspx")
{ Action = PageAction.Details,
ViewName = "ListDetails",
Model = model
});
Page Modes combination
• Each entity can use its Page Mode:
• Order is significantroutes.Add(
new DynamicDataRoute(“Products/{action}.aspx")
{
Constraints = new RouteValueDictionary(
new { action = "List|Details|Edit|Insert" }),
Model = model,
Table = “Products”
});
routes.Add(
new DynamicDataRoute("{table}/ListDetails.aspx")
{ Action = PageAction.List,
ViewName = "ListDetails",
Model = model
});
routes.Add(
new DynamicDataRoute("{table}/ListDetails.aspx")
{ Action = PageAction.Details,
ViewName = "ListDetails",
Model = model
});
Field Templates
• Dynamic Data selects controlsbased on field datatype– Boolean shown as checkbox
– Relationships shown as drop down list
– Text show as TextBox or Literal
• Pages use Field Templates toshow control at runtime– Controls do not form part of Page Template
markup, injected at runtime
• FieldTemplates are stored in DynamicData\FieldTemplates– “Intrinsic types”
Field Templates
• Fields with unknown datatype are not displayed
• Atribute UIHint associates control with datatype
– Substitute control for known datatype
– Assign control for “unknown” datatype
• Field data is displayed using markup– <%# FieldValueString %>
– <%# FieldValueEditString %> (edit mode)
Custom Page Templates
• Page Templates can be designed
• They follow naming convention– DynamicData\CustomPages\{entity_name
}\PageName.aspx
• To associate page templateListDetails.aspxwith tableSuppliers store it in– DynamicData\CustomPages\Supplier
s\ListDetails.aspx
Custom Page Templates
• Tipically, page markup contains DynamicField control
– Concrete control for field is selected at runtime
• Dynamic Data Engine maintains consistent URLs
– Name of the customized page has to coincide with the “built-in” page names (ListDetails.aspx, Edit.aspx, List.aspx, etc.)
– Same URL used to access customized and uncustomized pages
<Columns>
<asp:DynamicField DataField="CompanyName" />
<asp:DynamicField DataField="Phone" />
<asp:DynamicField DataField="Fax" />
<asp:DynamicField DataField="Products" />
</Columns>
Custom Field Template
• It is possible to modify control associated with a datatype
– Associate Calendar control with field of type Date
• New templates can be assigned to “unknown” datatypes
– Use Image control for binary field.
• Custom Field Templates extend FieldTemplateUserControl
• Property FieldValue retrieves field data
<asp:Calendar ID="Calendar1" runat="server“
SelectedDate="<%# (FieldValue!=null)? FieldValue:DateTime.Now %>"
VisibleDate="<%# (FieldValue!=null)? FieldValue:DateTime.Now %>"
</asp:Calendar>
Custom Field Template-Edit Mode
• Use ExtractValues() to write control data toDB
protected override void ExtractValues(IOrderedDictionary
dictionary) {
dictionary[Column.Name] =
ConvertEditedValue(Calendar1.SelectedDate.ToShortDateString());
}
MetaData application
• Dynamic Data uses MetaData specified in the Data Model– But when model is regenerated changes are lost
– Define additional metadata in partial class
using System;
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(EmployeeMetadata))]
public partial class Employee
{
}
public class EmployeeMetadata
{
[UIHint("DateTimeCalendar")]
public object HireDate { get; set; }
}
Data Validation
• Can use metadata
– [Required()]
– [Range(0,100)]
public class CustomerMetadata {
[Required()]
public object Title;
}
• Using validation events (Local)• Pattern On<Field>Changing, On<Field>Changed
public partial class Customer {
partial void OnTitleChanging(string value) {
if (!Char.IsUpper(value[0])) {
throw new ValidationException(
"Title must start with an uppercase letter.");
}
}
}
Data Validation
• Using Validate event (Global)
– OnValidate fires on change for any field
– You can filter on type on change
(ChangeAction)
public partial class Employee{
partial void OnValidate(System.Data.Linq.ChangeAction action){
if (action == System.Data.Linq.ChangeAction.Insert)) {
if (this._BirthDate.Value.CompateTo(DateTime.Now)>0)
throw new ValidationException(“The birth date cannot be
in the future”);
}
}
}
Customization-DEMO
Dynamic Data Wizard
• Facilitates creation of Dynamic Data web sites
• In development
– Not part of the .Net 3.5 SP1 RTM (not supported)
– Downloadable from CodePlex
– Requires SP1 of VS 2008
– Will not work with VS 2008 Express Ed.
Dynamic Data Wizard
Step1: Data Model
• Data Model
– New Data Model generation
– Can select existing model
Step2: Choose Entities for the Model
• Table selection
• Can specify Namespace
• Can specify class name ( “DataContext” is appended)
• LINQ to SQL or LINQ to Entities
Step3: Page Template Selection
• Wizard generates Custom Page Templates
– Template for a list view (List)
– for details (Details), adds Details link for each element in list view
– For edition view (Edit)
– New element (Insert)
Step4: Operation Customization
• Select/Restrict operations type
– edition, insertion, deletion, details
• You can select fields for each view
Dynamic Data Wizard - Summary
• Data Model is generated (DataContext) and the Page Templates
• All Page Tempates go into CustomPages
– Wizard does not use “standard” page templates
• Can select Fields to appear in each View and types of operations
Dynamic Data Wizard - DEMO
Summary
• Dynamic Data facilitates creation of data-driven web sites
– Increases development productivity
– Simplifies maintenance
– Possibility of customization
• Dynamic Data Wizard still under development
References
• Introduction to ASP.NET Dynamic Data-Dynamic Data in Action Webcasts– http://www.asp.net/dynamicdata/
• Using ASP.NET Dynamic Data (MSDN)– http://msdn.microsoft.com/en-us/library/cc488545.aspx
• Dynamic Data Futures (Source Code)– http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=14475
• Dynamic Data Wizard (Template for VS 2008 SP1)– http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=14474
• ASP .NET Dynamic Data (Video Links)– http://www.myvbprof.com/2007_Version/Dynamic_Data_Tutorial.aspx
• ASP .NET Dynamic Data Attributes– http://blogs.msdn.com/mairaw/archive/2008/04/24/dynamic-data-attributes.aspx
Top Related