Export (0) Print
Expand All

How to: Create a Data Service Using the Reflection Provider (WCF Data Services)

WCF Data Services enables you to define a data model that is based on arbitrary classes as long as those classes are exposed as objects that implement the IQueryable(Of T) interface. For more information, see Data Services Providers (WCF Data Services).

The following example defines a data model that includes Orders and Items. The entity container class OrderItemData has two public methods that return IQueryable(Of T) interfaces. These interfaces are the entity sets of the Orders and Items entity types. An Order can include multiple Items, so the Orders entity type has an Items navigation property that returns a collection of Items objects. The OrderItemData entity container class is the generic type of the DataService(Of T) class from which the OrderItems data service is derived.

Note Note

Because this example demonstrates an in-memory data provider and changes are not persisted outside of the current object instances, there is no benefit derived from implementing the IUpdatable interface. For an example that implements the IUpdatable interface, see How to: Create a Data Service Using a LINQ to SQL Data Source (WCF Data Services).

Imports System
Imports System.Collections.Generic
Imports System.Data.Services
Imports System.Data.Services.Common
Imports System.Linq

Namespace CustomDataServiceClient
    <DataServiceKeyAttribute("OrderId")> _
    Public Class Order
        Private _orderId As Integer 
        Private _customer As String 
        Private _items As IList(Of Item)
        Public Property OrderId() As Integer 
            Get 
                Return _orderId
            End Get 
            Set(ByVal value As Integer)
                _orderId = value
            End Set 
        End Property 
        Public Property Customer() As String 
            Get 
                Return _customer
            End Get 
            Set(ByVal value As String)
                _customer = value
            End Set 
        End Property 
        Public Property Items() As IList(Of Item)
            Get 
                Return _items
            End Get 
            Set(ByVal value As IList(Of Item))
                _items = value
            End Set 
        End Property 
    End Class
    <EntityPropertyMappingAttribute("Product", "productname", _
        "orders", "http://schema.examples.microsoft.com/dataservices", True)> _
    <DataServiceKeyAttribute("Product")> _
    Public Class Item
        Private _product As String 
        Private _quantity As Integer 
        Public Property Product() As String 
            Get 
                Return _product
            End Get 
            Set(ByVal value As String)
                _product = value
            End Set 
        End Property 
        Public Property Quantity() As Integer 
            Get 
                Return _quantity
            End Get 
            Set(ByVal value As Integer)
                _quantity = value
            End Set 
        End Property 
    End Class 
    Partial Public Class OrderItemData
#Region "Populate Service Data" 
        Shared _orders As IList(Of Order)
        Shared _items As IList(Of Item)
        Sub New()
            _orders = New Order() { _
                New Order() With {.OrderId = 0, .Customer = "Peter Franken", .Items = New List(Of Item)()}, _
              New Order() With {.OrderId = 1, .Customer = "Ana Trujillo", .Items = New List(Of Item)()}}
            _items = New Item() { _
              New Item() With {.Product = "Chai", .Quantity = 10}, _
              New Item() With {.Product = "Chang", .Quantity = 25}, _
              New Item() With {.Product = "Aniseed Syrup", .Quantity = 5}, _
              New Item() With {.Product = "Chef Anton's Cajun Seasoning", .Quantity = 30}}
            _orders(0).Items.Add(_items(0))
            _orders(0).Items.Add(_items(1))
            _orders(1).Items.Add(_items(2))
            _orders(1).Items.Add(_items(3))
        End Sub
#End Region
        Public ReadOnly Property Orders() As IQueryable(Of Order)
            Get 
                Return _orders.AsQueryable()
            End Get 
        End Property 
        Public ReadOnly Property Items() As IQueryable(Of Item)
            Get 
                Return _items.AsQueryable()
            End Get 
        End Property 
    End Class 
    Public Class OrderItems
        Inherits DataService(Of OrderItemData)
        ' This method is called only once to initialize 
        ' service-wide policies. 
        Shared Sub InitializeService(ByVal config As DataServiceConfiguration)
            config.SetEntitySetAccessRule("Orders", EntitySetRights.All)
            config.SetEntitySetAccessRule("Items", EntitySetRights.All)
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2
        End Sub 
    End Class 
End Namespace
Show:
© 2014 Microsoft