WCF Ria Service三個常用的核心類關系:
DomainService與LinqtoEntitiesDomainService /LinqToSqlDomainService關系制圖如下:
<a href="http://blog.51cto.com/attachment/201201/102324306.png" target="_blank"></a>
DomainService類是領域服務的基類-域名服務是用WCF Service來封裝一個應用程式的業務邏輯. 一旦你執行個體化一個Domain Service. Domain Service會對外暴露出與這個Domain Service相關l連的業務邏輯層 通路代理proxy.
LinqToEntitiesDomainService類:對領域服務提供了一個利用Linq操作實體的基類. 同理而言LinqToSqlDomainService同樣一個操作領域服務的基類. 隻不過方式變成了Linq to SQL.
一個域名服務類必須注明EnableClientAccessAttribute屬性,以使服務對給用戶端項目時可見的. 如下建立一個執行個體來說明如何使用Ria Service在Silverlight通路資料.
A:建立一個Silverlight Application.
<a href="http://blog.51cto.com/attachment/201201/102330366.png" target="_blank"></a>
B:在建立項目時指定Silverlight版本為4.0 注意必須啟用RIA Service.
<a href="http://blog.51cto.com/attachment/201201/102336913.png" target="_blank"></a>
Silverlight 添加RIa Service後空白解決方案.
<a href="http://blog.51cto.com/attachment/201201/102343795.png" target="_blank"></a>
D:如何通過RIA Service在Silverlight通路資料. 基本思路如下;
<a href="http://blog.51cto.com/attachment/201201/102349201.png" target="_blank"></a>
E:基于解決方案中伺服器端Silverlight2.Web添加一個資料實體類:ADO.NET Entity Data Model.
<a href="http://blog.51cto.com/attachment/201201/102354181.png" target="_blank"></a>
出現一個添加向導 選擇實體類資料源-來自資料庫:
<a href="http://blog.51cto.com/attachment/201201/102400835.png" target="_blank"></a>
下一步. 建立一個資料庫有效連接配接. 下一步 選擇指定資料庫下實體資料對應的表:
<a href="http://blog.51cto.com/attachment/201201/102406643.png" target="_blank"></a>
ok即完成了資料實體的建立.需要注意實體所在命名空間支援自定義. 即将在Silverlight2用戶端程式引用它. 也可以選中該檔案在屬性中找到CurrentNameSpace直接修改該檔案所在命名空間.
F:在解決方案伺服器端Silverlight2.Web中添加領域服務Domain Service檔案:
<a href="http://blog.51cto.com/attachment/201201/102411708.png" target="_blank"></a>
出現建立向導.如下:
<a href="http://blog.51cto.com/attachment/201201/102432410.png" target="_blank"></a>
發現在向導中Domain Service Class 要選擇實體類是空. 注意在建立Ria Service中每做一次修改都需要Ctra +Shit+B快捷鍵重寫生成一下解決方案.
<a href="http://blog.51cto.com/attachment/201201/102439629.png" target="_blank"></a>
Enable Client Access指定在Silverlight用戶端可編輯的 選中實體類中Customer表.ok 生成一個Domain Service. 這時解決方案自動生成一個DomainService1.CS檔案.
該檔案特點如下:
(1)該CustomerDomainService繼承于抽象的LinqToEntitiesDomainService類 。這個基類是自動使用的域名服務公開一個與之像關聯ADO.NET實體資料類Customer.
(2)CustomerDomainService 類是标有EnableClientAccessAttribute 屬性,以表明它是可見的用戶端層Silverlight2項目中是可見的.
(3)一個沒有任何過濾和排序操作的GetCustomer()方法獲得全部Customer表資料. 同理而言在這個類也可以定義增删改查相應資料操作方法.
G:Silverlight用戶端項目Silverlight2中引用.
重寫生成以下解決方案,在Silverlight用戶端項目Silverlight2中檢視所有檔案.發現在Generated_Code檔案下多了一個SilverlightApplication2.Web.g.CS代碼檔案
<a href="http://blog.51cto.com/attachment/201201/102444606.png" target="_blank"></a>
打開該代碼檔案. 其特點如下:
(1)WebContext 繼承與類WebContextBase 生成的一個類
(2) CustomerDomainContext 繼承于類 DomainContext。這個類有一個方法名為 GetCustomersQuery 相對應的查詢方法在域中建立服務
(3) Customer 從派生類 Entity 生成的類是由Domain Service暴露公開的實體。該用戶端 的Customer 實體類通伺服器上Customer實體類向對應比對/
G:在Silverlight中獲得對應資料:
在MainPage.XAMl中必須導入兩個命名空間: 一個是定義資料實體所在命名空間[即在建立ADO.NET Entity指定空間] 另外一個是Domain Service所在命名空間[預設為項目名稱].
在MainPage放入一個DataGrid來測試是否獲得資料. 背景編碼如下:
public partial class MainPage : UserControl
{
private CustomerDomainContext _customerContext = new CustomerDomainContext();
public MainPage()
{
InitializeComponent();
LoadOperation<Customer> loadOp = this._customerContext.Load(this._customerContext.GetCustomersQuery());
CustomerGrid.ItemsSource = loadOp.Entities;
}
}
測試結果:
<a href="http://blog.51cto.com/attachment/201201/102450434.png" target="_blank"></a>
一個完整通過Ria Service擷取資料庫MSSQL中資料過程如上.
本文轉自chenkaiunion 51CTO部落格,原文連結:http://blog.51cto.com/chenkai/765090