Detailed description of data-driven applications developed by the ADO. NET Client

Source: Internet
Author: User

In ASP. net ajax 4.0 Preview 3, developers can access two important new features: ADO. NET Data Service Client Library and ASP. net ajax Template. Familiar with ASP. NET Server development, but not familiar with client development, you can simply understand these two features as the DataSource and ListView of the client, as long as the data is passed through ADO. NET Data Service output to the front end, you can develop Data-driven applications on the client just like using the combination of DataSource and ListView.

In this article, we will look at how to use the ADO. NET Data Service Client Library to directly obtain the REST Data interface exposed by ADO. NET Data Service to the Client's JavaScript code for calling. The sample code used in this article can be downloaded here: ASP. net ajax 4.0 Preview 3 Demo, and then refer to AdoNetDataServiceDemo. aspx in it.

Server preparation

In the following example, we will use an SQL Server 2005 Express Edition database, which contains a table named OscarWinners, which records the Oscar winners for this year, fields include AwardID, Award, Winner, and Film. Then we create an ADO. NET Entity Model for this table, and then create an ADO. NET Data Service for the Entity class it generates. These operations can be completed by clicking a few mouse points in Visual Studio 2008, so we will not explain them in detail. In the InitializeService () method of ADO. NET Data Service, we only provide it with the loosest rule:

config.SetEntitySetAccessRule("*", EntitySetRights.All);

At this point, we have prepared all the work to be done on the server. Open the ADO. NET Data Service address you created to see if the correct Atom format Data is output. If not, check whether the WCF on your machine has been correctly installed and configured. Make sure that the preparations for the server are complete, and then go to the client for development.

Connect to Data Service
To use ADO. NET Data Service on the client, we need to have only one class, namely Sys. Data. AdoNetServiceProxy. First, we need to connect to ADO. NET Data Service, that is, to instantiate this class using the URL of ADO. NET Data Service:

var dataService = new Sys.Data.AdoNetServiceProxy("WebDataService.svc");

Then, we can use dataService to call ADO. NET Data Service for CRUD operations.

CRUD operation

All CRUD operations are performed on the Sys. Data. AdoNetServiceProxy object. The methods are query (), insert (), update (), and remove (). In our example, the query () and update () methods are used. The other two methods are similar to the update () method and are not described in detail.

Query operations

dataService.query("OscarWinners", function(result, context, operation) {
/* display result */
}, errorHandler);

With the preceding statement, all data in the OscarWinners table is queried. The first callback function will be called when the query is successful. Therefore, you can compile the logic for splicing HTML to display the result. For specific code, see AdoNetDataServiceDemo. aspx In the download. The second callback function will be called when the query fails. We can compile a unified error handling function named errorHandler and pass it to this parameter.

If you need to pass complex query parameters, you can use the format of ADO. NET Data Service, which can be found on MSDN. For example, you can query the number of Oscar awards that Slumdog Millionaire won and output the awards by name:

dataService.query("OscarWinners?$filter=Film eq 'Slumdog Millionaire'&$orderby=Award", 
function(result, context, operation) {
/* display result */
}, errorHandler);

Update operation

DataService. update (item, function (result, context, operation) {}, errorHandler );

Although you save the query results as an items set and modify the attributes of items based on the operations you perform on the interface, these logics need to be manually maintained, however, to update the item to the server, you only need to make such a simple call.

In the sample code I provided, I wrote an InPlaceEditBehavior, the so-called "local editor", which enables users to click the displayed text and change the displayed text to an input box. Then, I bound this InPlaceEditBehavior to the span of the Winner field and Film field displayed in each record, so that these spans can receive user input. Finally, I added an onchanged event for InPlaceEditBehavior and completed the update item and update () operation in the event processing function.

Summary

In this article, I briefly introduced the ease of use of the ADO. NET Data Service Client Library, and explained how to use it to save a lot of Data Interaction code.

If you have ever written AJAX-Enabled WCF Service, you should know how troublesome it is to expose the entity class to the WCF Service Interface, even if every entity class simply supports the CRUD method, you must also manually write these four methods. ADO. NET Data Service is equivalent to helping you complete all this. You only need to give it entity classes and rules, and it will help you generate a Data Service. In addition, a large number of client proxy classes are automatically generated through the data interfaces included in AJAX-Enabled WCF Service, while ADO. NET Data Service Client Library has only one fixed proxy class, and the Client code volume does not increase with the increase of interface complexity.

After talking about the advantages of the ADO. NET Data Service Client Library, what is not doing well in this example? I think the most difficult part to maintain is to splice HTML code after obtaining data. The human-hand-written HTML code is prone to errors and will be difficult to update in the future, when debugging fails, it is not easy to locate the problem.

  1. Support for Bidirectional Routing in ASP. NET Web Forms 4.0
  2. Concepts that are confusing in ADO. NET
  3. Use Ajax and Web Service Development instances of ASP. Net 3.5

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.