ITPub博客

首页 > Linux操作系统 > Linux操作系统 > ADO.NET Data Services 新功能

ADO.NET Data Services 新功能

原创 Linux操作系统 作者:chinahero_erp 时间:2010-06-28 13:26:36 0 删除 编辑

1.Server-driven paging

 

這是 1.5 版的新增功能,它可以由伺服器決定每一次分頁的大小,在 DataServiceConfiguration 中開放了一個新的 SetEntitySetPageSize(name, count) 的方法,它可以設定指定的 (或用 “*” 來表示全部的) Entity Set 每一頁的資料列個數,如下面的程式:

 

config.SetEntitySetPageSize("Customers", 10);
config.SetEntitySetPageSize("Orders", 5);

 

2. Query Projection

 

查詢投影 (query projection) 是 1.5 的新功能,只要有用過 1.0 版的開發人員基本上都會問:為什麼沒有可以指定查詢欄位的功能呢?也就是說,1.0 版只支援 SELECT *,但為了效能以及資料量的考量,更多開發人員希望可以有 SELECT f1, f2, … 的方法,因此微軟特別在 1.5 中加入了一個新的查詢選項子 $select,它可以設定要回傳的 entity 的欄位有哪些,這樣開發人員就不必再以 SELECT * 為主,它在特定案例中也可以防止過多關鍵性的資料外流。

 

http://myserver/Northwind.svc/Orders(10643)?$select=OrderID,Order_Details

 

同樣的,在 .NET Client Library 中的 LINQ select 指令 (VB 是 Select) 也終於可以支援設定要擷取的欄位的功能了。

 

3. Custom Data Service Provider

 

以往 1.0 版只支援 Entity Framework 的 Data Model,在 1.5 版時將會有所改變,微軟重寫了使用在 Data Services 的 Provider 基底類別,並提供了一組新的 Provider:Reflection Provider,它可以讓現有已建立的程式化資料模型 (簡單的說,就是現有的類別啦~) 可以直接套用在 Data Service 上作為資料模型,筆者認為這是一個很大的突破,因為已經自己開發 DAL 或是 BAL 的企業可以利用這個方式直接將現有的元件開放給 Data Services,而不必再繞經 Entity Framework 再使用 Data Services。例如下列的程式碼就是一個使用 Reflection Provider 開放模型給 Data Service 的範例:

 

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 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 _orders;
        static IList _items;
        static OrderItemData()
        {
            _orders = new Order[]{
              new Order(){ rderId=0, Customer = "Peter Franken", Items = new List()},
              new Order(){ rderId=1, Customer = "Ana Trujillo", Items = new List()}};
            _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 Orders
        {
            get { return _orders
.AsQueryable(); }
        }
        public
IQueryable Items
        {
            get { return _items
.AsQueryable(); }
        }
    }
    public class OrderItems :
DataService
    {
        // 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);
        }
    }
}

 

4. Streaming of binary resource

 

1.5 版的 Data Services 利用 ATOM 提供的 Media Link 屬性,實作了一個可以由遠端下載二進位資料流的功能,此功能以 IDataServiceStreamProvider 提供。用戶端可以使用 DataServiceContext 的 GetReadStream() 取得讀取的資料流,而使用 SetSaveStream() 將要更新的二進位資料流設定給 DataServiceContext 以更新二進位資料。

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8318659/viewspace-666485/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 微软云计算
请登录后发表评论 登录
全部评论

注册时间:2010-06-18

  • 博文量
    3
  • 访问量
    1626