WHAT IS ASP.NET DYNAMIC DATA?

http://www.hanselman.com/blog/PuttingASPNETDynamicDataIntoContext.aspx

WHAT IS ASP.NET DYNAMIC DATA?

Here's the Preview Drop of Dynamic Data for download. It'll be released later this year in a future Service Pack for .NET Framework 3.5. This is a pre-release build that's updated since the relatively quiet preview release last December.

Fundamentally, Dynamic data is new controls for DetailsViewFormViewGridView, or ListView controls along with a lot of meta-infrastructure classes to make it possible. The design uses convention over configuration in a number of places, and this marks, IMHO, the recognition of a simpler way to do things.

If you can point at one thing in Dynamic Data and say, "that's it!",  it'd be DynamicControl. it's a control that takes metadata from your Database model (LINQ to SQL or LINQ to Entities in v1, other POCOs (Plain Old CLR Objects) or ORM's possibly in v.Next) and selects from a FieldTemplateUserControl.

Stated differently, if you've got a field in a dozen places that should show Phone Numbers, you probably want a Textbox, some validators, maybe a MaskedEditControl, or perhaps some 3rd party controls. You think about it as a Phone Number. if you decide to change that control, you want to change it for the whole application. You want to keep it DRY (Don't Repeat Yourself). Currently, unless you had the discipline to create a PhoneNumberUserControl, you'd have to do "monkey work" and go find all the instances of that field and keep them up to date. You'll be repeating yourself all over with data that should exist in one place, like the length of the field in the database, gets copied into possibly dozens of places, validators, controls, etc.

Here's a comment that my friend Peter Blum left on ScottGu's Blog. It actually was Peter, not the Product Team, who helped me "grok" Dynamic Data a few weeks ago. We spent a few hours on the phone talking and it finally popped for me.

I have been working with Dynamic Data for over a month and am very excited about it. I wanted promote an aspect of Dynamic Data that will be used in many other situations than the application Scott showed.
Dynamic Data introduces a new web control called DynamicControl. It can be used in FormView and ListView controls to provide a UI based on database-level schema. When using GridView and DetailsView, you add a DynamicField, which is a replacement for the BoundField that internally has a DynamicControl. (Thus has all of the features of a DynamicControl.)
This results in your Grids and data entry forms being built around tables and columns of your database with whatever smart data entry controls you like, such as the AJAX Control Toolkit's MaskedTextBoxExtender on a textbox or your favorite third party controls. The database schema also determines the validators for this column. That avoids errors implementing the right validators or updating them when the database schema is modified. (No more "how do I validate a BoundField?" issues!)

To me, Dynamic Data is a major step forward in developing data entry oriented web applications for ASP.NET.

imageOne of the demo's that is often shown is Dynamic Data's "scaffolding" feature. Basically, you point the wizard at a database and "poof" you've got a nice DataGrid Admin-like site. However, to dismiss it as a scaffolding tool is just the mistake I made when I first saw it.

HOW DOES IT WORK?

There's a LOT of cool plumbing to Dynamic Data and I'll try to work with the team to get a podcast or two done and some articles explaining thedeep how. Here's the basic how.

You can make a new Dynamic Data Website and it looks pretty much the same as any other site, because it is. It's a WebForms site just like any others, except for a new folder, called DynamicData. That folder has a web.config of its own to block access. This is the convention. Let's open it up.

image 

Let's say I want some serious customization. Let's so that the page for the Products table should be TOTALLY custom, but just the ListDetails page. I'll put that under /CustomPages. Let's say I want to use a Telerik control for DateTimes, Integers, and TextAreas, but just some of them.

 image

Nothing HAS to look the way it comes by default. Everything is customizable. If I want grid paging to look like whatever i want, I change the GridViewPager.ascx. You can customize at the Table, Column and DataType level.

I can put "hints" on a metadata-specific class like this:

[Range(0, 100)]
[UIHint("IntegerTelerik")]
public object UnitsOnOrder { get; set; }

That UIHint will be used later when I hook up a DynamicControl. As soon as DynamicData sees I'm bound to the UnitsOnOrder column, it'll automatically find IntegerTelerick_Edit - based on the naming and location convention - when it needs to show the "Edit" state. I can set it on a pull column basis, and then I can change it everywhere from one place.

Here's what that ASCX looks like in code:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ Control Language= "C#" Inherits= "System.Web.DynamicData.FieldTemplateUserControl" %>
<%@ Import Namespace= "System.Collections.Generic" %>
<%@ Import Namespace= "System.Linq" %>
<%@ Register assembly= "Telerik.Web.UI" namespace = "Telerik.Web.UI" tagprefix= "telerik" %>
 
<script runat= "server" >
     
     protected void Page_Load( object sender, EventArgs e) {
         var metadata = MetadataAttributes.OfType<System.ComponentModel.DataAnnotations.RangeAttribute>().FirstOrDefault();
         if (metadata != null ) {
             RadSlider1.MinimumValue = ( int )metadata.Minimum;
             RadSlider1.MaximumValue = ( int )metadata.Maximum;
         }
         
     }
    
     protected override void ExtractValues(IOrderedDictionary dictionary) {
         dictionary[Column.Name] = ConvertEditedValue(RadSlider1.Value.ToString());
     }
     
</script>
 
<telerik:RadSlider ID= "RadSlider1" runat= "server" Value= "<%# (short)FieldValue %>" />

See how it's generic (by that I mean non-specific, not CLR Generic)?  I've replaced the Textbox that is usually used for an Integer and am using a Telerik Slider. The [Range] attribute can be pulled off and popped into this instance of the control.

Here it is at runtime, next to some other Integer columns:

image

Now, I get to think about selecting the correct DataType via UIHint rather than using the ASCX directly. It makes the control really reusable within the context of data binding and keeps me from repeating myself.

I can add Validation Controls to a data type, so the Validation exists in one place, rather than repeated all over. For example, if you wanted to swap out validation for one, or all, your controls, to use Peter's Validation and More (which rocks, by the way, we bought a site license at my last job) than you'd do it once per data type.

This is just one VERY simple example, but it's, I think, I pretty good one. Thanks to Scott Hunter for helping me out with this as well as my talk at Devscovery last week. There are some good Dynamic Data samples he's done that you can download and check out. One includes a "before and after" application that shows how an address book would be coded using the new DynamicControl and the older way (using BoundControl).

An even cooler (and advanced) example is one where Scott Hunter wants to pull an funky binary encoded image out of an old database. He makes a custom control that handles a custom data type called "DbImage" and completely encapsulates the display of that column. From the point of view of the GridView, it's just a DynamicControl that shows itself as an <img> tag. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值