HOW TO:使用反映提供者建立資料服務 (WCF Data Services)
WCF Data Services 可讓您定義以任意類別為基礎的資料模型,只要將這些類別公開為實作 IQueryable<T> 介面的物件即可。 如需詳細資訊,請參閱資料服務提供者 (WCF Data Services)。
範例
下列範例定義包含 Orders 及 Items 的資料模型。 實體容器類別 OrderItemData 具有兩個會傳回 IQueryable<T> 介面的公開方法。 這些介面是 Orders 和 Items 實體類型的實體集。 Order 可以包括多個 Items,因此 Orders 實體類型具有一個 Items 導覽屬性,該屬性會傳回 Items 物件的集合。 OrderItemData 實體容器類別是 DataService<T> 類別的泛型型別,OrderItems 資料服務即衍生於此。
注意
由於本範例示範的是記憶體內部資料提供者,在目前物件執行個體外部所進行的變更將無法持續,因此實作 IUpdatable 介面並不會產生任何益處。如需實作 IUpdatable 介面的範例,請參閱 HOW TO:使用 LINQ to SQL 資料來源建立資料服務 (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
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
namespace CustomDataServiceClient
{
[DataServiceKeyAttribute("OrderId")]
public class Order
{
public int OrderId { get; set; }
public string Customer { get; set; }
public IList<Item> Items { get; set; }
}
[DataServiceKeyAttribute("Product")]
public class Item
{
public string Product { get; set; }
public int Quantity { get; set; }
}
public partial class OrderItemData
{
#region Populate Service Data
static IList<Order> _orders;
static IList<Item> _items;
static OrderItemData()
{
_orders = new Order[]{
new Order(){ OrderId=0, Customer = "Peter Franken", Items = new List<Item>()},
new Order(){ OrderId=1, Customer = "Ana Trujillo", Items = new List<Item>()}};
_items = new Item[]{
new Item(){ Product="Chai", Quantity=10 },
new Item(){ Product="Chang", Quantity=25 },
new Item(){ Product="Aniseed Syrup", Quantity = 5 },
new Item(){ 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]);
}
#endregion
public IQueryable<Order> Orders
{
get { return _orders.AsQueryable<Order>(); }
}
public IQueryable<Item> Items
{
get { return _items.AsQueryable<Item>(); }
}
}
public class OrderItems : DataService<OrderItemData>
{
// This method is called only once to initialize
//service-wide policies.
public static void InitializeService(IDataServiceConfiguration
config)
{
config.SetEntitySetAccessRule("Orders", EntitySetRights.All);
config.SetEntitySetAccessRule("Items", EntitySetRights.All);
}
}
}
請參閱
工作
HOW TO:使用 LINQ to SQL 資料來源建立資料服務 (WCF Data Services)
HOW TO:使用 ADO.NET Entity Framework 資料來源建立資料服務 (WCF Data Services)