Connecting to the Service
In the code-behind of the app's main page, we need some code that connects to the service, passing credentials. Since you created a user in the administration screens of the LightSwitch application assigning specific permissions, credentials of this user will be passed to the OData service. Listing 4 shows how to connect to the service and how to declare class-level variables that will store some information. (Comments have been added to the code for better understanding.)
Listing 4Connecting to the OData service and declaring class-level variables.
'The following directives are required 'Imports System.Data.Services.Client 'Imports MobileOrdersClient.OrdersServiceReference 'Store the service URL. Replace localhost with your server's name Private Shared ReadOnly ServiceUri As New Uri("http://localhost/ExposingOData/ApplicationData.svc") 'Two collections for storing the list of entities exposed by the service Private Property orders As DataServiceCollection(Of Order) Private customers As DataServiceCollection(Of Customer) 'Represent the LightSwitch's intrinsic data Private applicationData As OrdersServiceReference.ApplicationData 'Constructor Public Sub New() InitializeComponent() 'Start an instance of the WCF service Me.applicationData = New OrdersServiceReference.ApplicationData(ServiceUri) 'Pass credentials of a LightSwitch user Me.applicationData.Credentials = New NetworkCredential("TestUser", "TestUser$") 'Create instances of collections to store data Me.orders = New DataServiceCollection(Of Order)(applicationData) Me.customers = New DataServiceCollection(Of Customer)(applicationData) 'Declare event handlers for handling loaded data AddHandler Me.orders.LoadCompleted, AddressOf orders_LoadCompleted AddHandler Me.customers.LoadCompleted, AddressOf customers_LoadCompleted 'Load customers and orders loadCustomers() loadOrders() End Sub
The System.Data.Services.Client.dll assembly exposes the DataServiceCollection(Of T) class, which allows for storing collections of objects exposed by a WCF service. Technically speaking, the DataServiceCollection(Of Order) inherits from the ObservableCollection(Of T); such a collection supports notifications for changes over objects that it contains, and thus it can be used for data-binding. When a change occurs over one or more elements in the collection, the user interface will automatically reflect that change, and vice versa.
In this case, we have a property of type DataServiceCollection(Of Order) and a field of type DataServiceCollection(Of Customer). We're using a property to represent orders because they'll be data-bound to the user interface, and a field wouldn't be an appropriate choice. Notice that a connection to the WCF service is started by creating an instance of the ApplicationData class representing the OData feed, which contains data coming from LightSwitch's application data. Before making any other calls, the code must pass the appropriate credentials to the instance of the connection. This is done with an instance of the NetworkCredential class, whose constructor receives the username and passwords as the arguments. You can easily recall how both username and password were defined previously. The page constructor also defines event handlers for the Loaded event of both data collections; the collections are populated via two methods, loadCustomers and loadOrders, which are defined in the next paragraph.
Are you wondering why we're loading the list of customers if we'll only show orders? The reason is that we'll also implement code for inserting a new order, so we must specify a customer instance because of the one-to-many relationship existing between these two objects (customers and orders).