Building a Silverlight 3 based RIA Image Management System - 1
page 4 of 7
by Xianzhong Zhu
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 35122/ 93

ADO.NET Data Service Based Database Access

Before delving into the ADO.NET Data Service, let's sum up the typical data access solutions used in Silverlight applications.

Overview of Silverlight Data Access Technology

Experienced ASP.NET developers know, with the use of data access, they can write corresponding logics so that application can connect to various external data sources and further query and process data. With sophisticated data access provided by ASP.NET, developers can quickly accomplish the realization of the above process, almost without writing any code. The reason is that, ASP.NET operating environment is a complete installation of .NET framework, so ASP.NET can invoke ADO.NET to access the database, and then the results are transformed into HTML to be sent to the client.

But, for Silverlight applications, the same problem is not so simple. Because the Silverlight plug-in runs on the client browser, the corresponding micro .NET Framework is not possible to contain huge ADO.NET components. Aiming at this particular structure of Silverlight applications, the realization of Microsoft's technology is first to submit HTTP requests to the server to let the server carry out the task of data access, and then the results are sent back to the client in accordance with the format of the two sides agreed to. Thus, developers can use any SOAP-based technologies or WCF service to obtain data. Once the data are returned to the client, you can process them by calling the corresponding data components.

In summary, Silverlight applications can use the following three typical techniques for data access:

·         Traditional Web Service to access server-side database.

·         Using WCF service shipped with.NET 3.0 framework to access the server database.

·         Through the up-to-date and the most closely Silverlight bound technology--ADO.NET data service, to access the server-side database.

For Web service and WCF solution, readers may refer to the relevant information in MSDN. The sample in this article only shows interest in ADO.NET data service technology. Let's next start the more detailed discussion.

ADO.NET Data Services Technology

ADO.NET Entity Framework is Microsoft's next generation of ADO.NET. This framework, in fact, is not a brand-new and independent infrastructure. It is only another new choice based upon the traditional ADO.NET framework.

On the whole, ADO.NET Entity Framework has the following characteristics:

·         Entity data model that allows of high abstraction for data modeling.

·         Powerful mapping engine that allows developers to easily set up the mapping between the data model and data storage definition.

·         Supporting the use of entity SQL syntax and LINQ to query the definition of EDM data.

·         Object services layer that allows you to choose whether or not to present the results of a query as the row/column records or .NET objects.

·         Opening data provider model, allowing other storage mechanisms to be combined with ADO.NET Entity Framework.

For a simple schematic diagram of the ADO.NET Entity Framework, you can refer to here.

Use ADO.NET Data Services to Access the Server-side Image Data

Next, we are going to give details in the image management system, on how to use ADO.NET data services to access the image data on the server side.

(A) Implement Data Model

1. Right-click the ASP.NET Web Site project in Solution Explorer, point to Add, and then select New Item.

2. In the Add New Item dialog box, select ADO.NET Entity Data Model. Name the data model PhotoAlbumModel.edmx, and then click Add.

3. In the Entity Data Model Wizard, select Generate from Database, and then click Next.

4. In the Generate from Database dialog box, create a connection to the AjaxAlbumDB database. Notice that the entity connection settings that are shown will be added to the Web.config file of the application. The connection will be named AjaxAlbumDBEntities. The data service requires this connection but the Silverlight client will not use it. Click Next.

5. In the Choose Database Objects dialog box, select the two tables Category and Photo in the AjaxAlbumDB database. Then name the model namespace PhotoAlbumModel and click Finish.

This finishes the AjaxAlbumDB data model implementation and opens the Entity Data Model Designer. In the designer, we touch nothing. So you can see an association relationship diagram between two tables Category and Photo, as shown in Figure 7 given previously.

(B) Implement Data Service

1. Right-click the ASP.NET Web application project in Solution Explorer, point to Add, and then select New Item.

2. In the Add New Item dialog box, select ADO.NET Data Service. For this sample app, name the service PhotoAlbumDataService.svc, and then click Add. This adds the data service template that contains the required reference to the System.Data.Services library.

3. Visual Studio will open a file named PhotoAlbumDataService.cs that contains the source code that initializes the ADO.NET data service. The type of the template class identifies the data model. Set the type in the DataService declaration to AjaxAlbumDBEntities, as shown in the following example. As you see, the data service class is derived from System.Data.Services.DataService<T>.

4. Modify the config.SetEntitySetAccessRule method as shown in the following code. Authorization to read and write all data is defined by the call to config.SetEntitySetAccessRule("*", EntitySetRights.All). The asterisk applies the rule to all data sets in the data model.

5. Also, we modify the config. SetServiceOperationAccessRule method to grant all access rights in service operations.

Listing 1-- Initialize ADO.NET Data Service

public class PhotoAlbumDataService : DataService<AjaxAlbumDBModel.AjaxAlbumDBEntities>
{
    //Initialize Service
    public static void InitializeService(IDataServiceConfiguration config)
    {
        //Authorization to read and write all data 
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        //Service operations access configuration
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
    }
}

(C) Generating Data Service Client Library

This step will generate data service classes from the running data service. Note these classes represent each of the entities that are defined in the data service.

To do this, please first make sure the data service is running. Then, right-click the Silverlight project and select Add Service Reference. In the Add Service Reference dialog box, we just let Visual Studio discover the base URL of the data service, since the service and Silverlight project are in the same solution. If you click OK, Visual Studio goes and generates all the client side classes for you, adds the related references, and you can start using the service right away.

In our case, such as in the MainPage.xaml.cs, add the following using directive: using System.Data.Services.Client.

Since everything gets ready, let's come to the final phase--implementing the data access on the client side.

(D) Data Client Implementation

1. Open the *.xaml.cs source code files that will access ADO.NET Data Service in the Silverlight project.

2. Add a using directive to System.Data.Services.Client.

3. Add using directives to the PhotoServiceReference Service classes that are generated by Add Service Reference or DataSvcUtil.exe. The generated classes make it possible for the client application to materialize the data that is received from the data service.

Listing 2-- Add necessary using directives

using System.Collections.ObjectModel;
using System.Data.Services.Client;
using ADONET = S2PhotoAlbum.PhotoServiceReference;

4. Create an instance of the data service context of the PhotoServiceReference data service. Then by invoking the relevant methods (such as BeginExecute<(Of <(TElement>)>)(Uri, AsyncCallback, Object)), you can implement your client-side data access.

Listing 3-- Instantiate the proxy DataServiceContext

namespace S2PhotoAlbum
{
    public partial class MainPage : UserControl
    {
         //Define a variable of DataServiceContext proxy
        ADONET.AjaxAlbumDBEntities proxy;
        public MainPage()
        {
            InitializeComponent();
            //others omitted…
            //Define the instantiation of proxy DataServiceContext
            proxy = new ADONET.AjaxAlbumDBEntities(new 
                  Uri("PhotoAlbumDataService.svc", UriKind.Relative));
        }

For details in using the instantiation of proxy DataServiceContext to implement the typical CRUD operations, please refer to the later on modules.

In the next section, we are to preview the main finished line of the RIA image management system, and then set out to develop the first module of the sample system.


View Entire Article

User Comments

Title: add to xml file   
Name: sir jone
Date: 2009-09-19 4:18:31 AM
Comment:
hi dear ,
would you mind send me add to xml file from textbox sample
if you send my email is : tanha_x10@yahoo.com
tnx so much .






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-10-15 11:47:47 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search