WcF Data services client Library

The flexibility of querying WCF Data Services using an URL is attractive, but you hardly want to build an application that creates URLs whenever you want to query or edit a database. Fortunately, WCF Data Services also provides the capability to manipulate the data using LINQ. DS converts the LINQ requests into the appropriate URL.

To use the client library, you need to add a Service Reference to your data service (see Figure 12-40).

Visual Basic Code Snippets

figure 12-40

POST

Just as with other WCF services, adding the service reference creates a client-side proxy of your service. You can then query the objects directly, and DS creates the appropriate URL from your LINQ query. You first instantiate a context to your service, and then make the query:

Dim context As New PubsEntities(URL) Dim authors = From a In context.authors Where a.state = "CA" Order By a.city Select a For Each author In authors

'do something with the author type here

Next

Code snippet from SimpleDataService

This provides a much more natural means of querying the service, regardless of whether it's using JSON, Atom, or HTTP to make the request.

Add a new Windows Forms application to act as a client for the data service, and add a Service Reference to the project. Figure 12-41 shows one possible user interface. In this case, the list box on the left will be populated with the authors. Selecting an author will allow you to edit the properties using the fields on the right, or you can clear the fields to create a new author (see Figure 12-42).

FIGURE 12-41

FIGURE 12-42

FIGURE 12-41

FIGURE 12-42

Imports System.Data.Services.Client Imports SimpleDataServiceClient.PubsService

Public Class MainForm

'update this to match your service

Dim ServiceUri As Uri = New Uri("http://localhost:49233/PubsService.svc") Dim isNew As Boolean = True Dim isDirty As Boolean = False

Dim context As PubsEntities

Private Sub MainForm_Load(ByVal sender As Object,

ByVal e As System.EventArgs) Handles Me.Load context = New PubsEntities(ServiceUri)

InitializeList() End Sub

Private Sub RefreshButton_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles RefreshButton.Click 'retrieves the list of authors 'and updates the list InitializeList() End Sub

Private Sub InitializeList()

Me.AuthorsList.Items.Clear()

Dim authors = From a In context.authors Where a.state = "CA" Order By a.city Select a For Each author In authors

Me.AuthorsList.Items.Add(author)

Next End Sub

Private Sub ClearButton_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles ClearButton.Click isNew = True

Au_fnameTextBox.Text = String.Empty Au_lnameTextBox.Text = String.Empty PhoneTextBox.Text = String.Empty AddressTextBox.Text = String.Empty CityTextBox.Text = String.Empty StateTextBox.Text = String.Empty ZipTextBox.Text = String.Empty ContractCheckBox.Checked = False End Sub

Private Sub Field_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles ZipTextBox.TextChanged, _ StateTextBox.TextChanged, _ PhoneTextBox.TextChanged, _ CityTextBox.TextChanged, _ Au_lnameTextBox.TextChanged, _ Au_fnameTextBox.TextChanged, _ AddressTextBox.TextChanged isDirty = True End Sub

Private Sub SaveButton_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles SaveButton.Click Dim selectedAuthor As author = Nothing

If isNew Then

'saving a new entity selectedAuthor = New author ElseIf isDirty Then

'updating an existing entity selectedAuthor = Me.AuthorsList.SelectedItem End If

'update fields

With selectedAuthor

.au_id = Au_idTextBox.Text .au_fname = Au_fnameTextBox.Text .au_lname = Au_lnameTextBox.Text .phone = PhoneTextBox.Text .address = AddressTextBox.Text .city = CityTextBox.Text .state = StateTextBox.Text .zip = ZipTextBox.Text .contract = ContractCheckBox.Checked End With If isNew Then context.AddToauthors(selectedAuthor) ElseIf isDirty Then context.UpdateObject(selectedAuthor) End If context.SaveChanges()

isNew = False isDirty = False

End Sub

Private Sub AuthorsList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles AuthorsList.SelectedIndexChanged

'fill fields

Dim SelectedAuthor As author =

DirectCast(Me.AuthorsList.SelectedItem, author) With SelectedAuthor

Au_idTextBox.Text = .au_id Au_fnameTextBox.Text = .au_fname Au_lnameTextBox.Text = .au_lname PhoneTextBox.Text = .phone AddressTextBox.Text = .address CityTextBox.Text = .city StateTextBox.Text = .state ZipTextBox.Text = .zip ContractCheckBox.Checked = .contract End With isNew = False

End Sub End Classs

Code snippet from SimpleDataService

Most of the preceding code should be fairly self-explanatory. Two routines probably need explanation, however.

The InitializeList routine is a simple LINQ query to retrieve the list of authors. It then adds them to the list box. The DisplayMember of the list box is set to the Last Name field (au_lname), while the ValueMember is set to the key (au_id).

The SaveButton code is divided into three logical parts. First, you must identify the author you want to save. As this may be either an existing author or a new one, the isNew and isDirty flags are used to determine if this is an insert or an update. Next, the fields are set to the new values. Finally, the magic happens: The proxy method Addtoauthors is used to add a new author to the list if you are performing an insert, while UpdateObject is used to mark for an update. We could have made a number of changes here, and the context tracks these. Once the SaveChanges method is called, they are sent to the server.

A couple of options are available when calling SaveChanges using the SaveChangesOptions enumeration. By default, each request is sent individually. If an error occurs, the save will end, but any saves that have already occurred will remain in place. If you use the ContinueOnError option, DS will continue to save items. You can use the return value from the SaveChanges method to determine the result of each update. Alternately, the SaveChangesOptions.Batch will send all of the requests within a single ChangeSet. While not technically a transaction, the ChangeSet behaves similarly: Either all the updates will happen or none will. Again, the return from the SaveChanges method will identify where the errors occurred.

0 0

Post a comment