15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

856 ❘ ChaPTer 30 cOre AdO.net<br />

One of the difficulties with this model is deciding what data to transport between your tiers as well as<br />

figuring out the format in which the data should be transported. With ADO.<strong>NET</strong>, you will be pleased to<br />

learn that these wrinkles have been ironed out, <strong>and</strong> support for this style of architecture is part of<br />

the design.<br />

One of the things that is much better in ADO.<strong>NET</strong> than OLE DB is the support for copying an entire record<br />

set. In .<strong>NET</strong>, it is easy to copy a DataSet:<br />

DataSet source = {some dataset};<br />

DataSet dest = source.Copy();<br />

This creates an exact copy of the source DataSet — each DataTable, DataColumn, DataRow, <strong>and</strong> Relation<br />

will be copied, <strong>and</strong> all data will be in exactly the same state as it was in the source. If all you want to copy is<br />

the schema of the DataSet, you can use the following code:<br />

DataSet source = {some dataset};<br />

DataSet dest = source.Clone();<br />

This again copies all tables, relations, <strong>and</strong> so on. However, each copied DataTable will be empty. This<br />

process really could not be more straightforward.<br />

A common requirement when writing a tiered system, whether based on a Windows client application or<br />

the Web, is to be able to ship as little data as possible between tiers. This reduces the amount of resources<br />

consumed.<br />

To cope with this requirement, the DataSet class has the GetChanges() method. This simple method<br />

performs a huge amount of work, <strong>and</strong> returns a DataSet with only the changed rows from the source<br />

data set. This is ideal for passing data between tiers because only a minimal set of data has to be<br />

passed along.<br />

The following example shows how to generate a “changes” DataSet:<br />

DataSet source = {some dataset};<br />

DataSet dest = source.GetChanges();<br />

Again, this is trivial. Under the hood, things are a little more interesting. There are two overloads of<br />

the GetChanges() method. One overload takes a value of the DataRowState enumeration, <strong>and</strong> returns<br />

only rows that correspond to that state (or states). GetChanges() simply calls GetChanges(Deleted |<br />

Modified | Added), <strong>and</strong> first checks to ensure that there are some changes by calling HasChanges(). If no<br />

changes have been made, null is returned to the caller immediately.<br />

The next operation is to clone the current DataSet. Once this is done, the new DataSet is set up to ignore<br />

constraint violations (EnforceConstraints = false), <strong>and</strong> then each changed row for every table is copied<br />

into the new DataSet.<br />

When you have a DataSet that just contains changes, you can then move these off to the data services tier<br />

for processing. After the data has been updated in the database, the “changes” DataSet can be returned<br />

to the caller (for example, there might be some output parameters from the stored procedures that have<br />

updated values in the columns). These changes can then be merged into the original DataSet using the<br />

Merge() method. Figure 30-9 depicts this sequence of operations.<br />

Client Tier<br />

Data Services Tier<br />

Database Tier<br />

Changes<br />

Update<br />

DataSet<br />

Merge<br />

DataSet<br />

New Data<br />

figure 30-9<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!