次の方法で共有


方法: リフレクション プロバイダーを使用してデータ サービスを作成する (WCF Data Services)

WCF Data Services では、IQueryable インターフェイスを実装するオブジェクトとして公開されているクラスに基づいてデータ モデルを定義できます。 詳細については、「データ サービス プロバイダー (WCF Data Services)」を参照してください。

次の例は、Orders および Items を含むデータ モデルを定義します。 エンティティ コンテナー クラス OrderItemData には、IQueryable インターフェイスを返す 2 つのパブリック メソッドがあります。 これらのインターフェイスは、Orders エンティティ型と Items エンティティ型のエンティティ セットです。 Order には複数の Items を含めることができるので、Orders エンティティ型には Items オブジェクトのコレクションを返す Items ナビゲーション プロパティがあります。 OrderItemData エンティティ コンテナー クラスは、OrderItems データ サービスが派生する DataService クラスのジェネリック型です。

Dd728281.note(ja-jp,VS.100).gif注 :
この例はメモリ内のデータ プロバイダーを示し、変更は現在のオブジェクト インスタンスの外部で永続化されないので、IUpdatable インターフェイスを実装する利点はありません。IUpdatable インターフェイスを実装する例については、「方法: 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);
        }
    }
}

参照

処理手順

方法: LINQ to SQL データ ソースを使用してデータ サービスを作成する (WCF Data Services)
方法: ADO.NET Entity Framework データ ソースを使用してデータ サービスを作成する (WCF Data Services)

概念

データ サービス プロバイダー (WCF Data Services)