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 interface. For more information, see Data Services Providers (WCF Data Services).

Example

The following example defines a data model that includes Orders and Items. The entity container class OrderItemData has two public methods that return IQueryable 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 class from which the OrderItems data service is derived.

Dd728281.note(en-us,VS.100).gifNote:
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).

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);
        }
    }
}

See Also

Community Additions

ADD
Show:
© 2014 Microsoft