15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Dynamic Data ❘ 1251<br />

This code extracts the value from a route parameter called productnameparameter <strong>and</strong> passes this value to<br />

the @productname parameter in a SQL query.<br />

dynamiC daTa<br />

If you think about data-driven web sites (which these days includes most web sites), you will probably<br />

realize that a lot of their functionality is very similar. Such a web site is likely to include one or more of the<br />

following concepts:<br />

➤<br />

➤<br />

➤<br />

➤<br />

Render HTML that is shaped dynamically based on data in an underlying data source (such as<br />

a database table or individual database row)<br />

Include pages in a site map that map directly or indirectly to entries in a data source (such as a<br />

database table)<br />

Have a structure that relates directly or indirectly to the structure of the underlying data source<br />

(a section of the site may map to a database table, such as About or Products, for example)<br />

Allow modification to the underlying data source that will be reflected on pages<br />

If you wanted to build a data-driven site, you would probably use fairly st<strong>and</strong>ardized code to achieve the<br />

above. You might bind ASP.<strong>NET</strong> elements such as tables of data directly to a database table, or you might<br />

include an intermediate layer of data objects to represent data in the database <strong>and</strong> bind to those. You have<br />

seen a lot of the code you would use for that in earlier chapters.<br />

However, because this situation is so common, there is an alternative. You could instead use a framework<br />

that provides a lot of the code for you to save yourself a lot of tedious coding. ASP.<strong>NET</strong> dynamic data is just<br />

such a framework, <strong>and</strong> it makes creating a data-driven web site much easier. As well as giving you the code<br />

outlined above (referred to in dynamic data web sites as scaffolding), dynamic data web sites provide a lot of<br />

additional functionality, as you will see shortly.<br />

In this section, you will see how to create a dynamic data site <strong>and</strong> look at some of the features that it offers.<br />

Creating dynamic data Web sites<br />

The best way to get a taste of what dynamic data web<br />

sites have to offer is to build one in Visual Studio, which<br />

is a surprisingly simple thing to do. In order to create<br />

a dynamic data web site you need to have some source<br />

data. You can use whatever data you like, but if you<br />

want you can use the sample data that is included in the<br />

downloadable code for this chapter. This data is a SQL<br />

Server Express database with the filename MagicShop<br />

.mdf. Figure 42-3 shows the tables that are included in<br />

this database.<br />

The MagicShop.mdf database represents a simple<br />

structure that you could use in an e-commerce web site.<br />

The types of <strong>and</strong> relationships between data will serve<br />

to illustrate how dynamic data sites work.<br />

figure 42-3<br />

Choosing a Data access Model<br />

When you create a dynamic data site through the File ➪ New ➪ Web Site… menu item, you will notice that<br />

there are two templates available:<br />

➤<br />

➤<br />

Dynamic Data Linq to SQL Web Site<br />

Dynamic Data Entities Web Site<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!