在前面的文章中,我用了大量的篇幅對UDA及ORM的使用進行了講解和示範,我們已經知道并熟悉的使用UDA和ORM建構簡單的應用,AgileEAS.NET在應用的縱向結構上建議使用分層結構,提出獨立資料層,資料層構成以ORM技術為基礎、UDA技術做為輔助,共同完成這一系列功能。
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NET_10AF4/image_2.png"></a>
基于接口開發
接口驅動的資料層
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NET_10AF4/image_11.png"></a>
也就是說資料層的消費者BL層或者UI層通路資料層依賴于DAL.Interface,而與具體的實作無關,基于這種了解的擴充,就是我們可以實作不同資料通路層實作的動态替換,如如某一個業務需要運作在基于SQLServer的資料庫上,也需要運作的Oracle資料庫之上,也可能需要運作在MySQL之上,因為不同資料庫之間的細微差别,比如所使用的表名不同、資料庫系統的函數、過程使用方式的差異,我們沒有做到統一的資料通路層,那麼我們就基于接口開發的思想把他分解成一個接口和三個實作。
在開發中如何進行
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NET_10AF4/image_15.png"></a>
需要說明一點的是如果我們需要生成基于接口驅動的資料層,那麼我們必須要選中項目頁籤上的是否生成接口層:
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NET_10AF4/~R56HWFZYZW7L%5D0@%7BD4%24_MR_2.jpg"></a>
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NET_10AF4/6UH1%25TI12FUWRZH%7B%5DCNN~4P_2.jpg"></a>
關鍵代碼分析
在分析和介紹代碼之前我有必要介紹一下示範解決方案中的項目依賴(引用關系):
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NET_10AF4/image_17.png"></a>
資料層消費者項目ClassLib.OrmDemo依賴于資料接口層項目ClassLibDemo.DAL.Interface,而不依賴于具體的實作ClassLibDemo.DAL.SQLServer,而我們之前的案子是ClassLib.OrmDemo依賴于ClassLibDemo.DAL.SQLServer的。
首先我們來看看生成的接口層代碼ClassLibDemo.DAL.Interface項目中的一個接口IDALManager:
1 /// <summary>
2 /// IDALManager接口,負責業務接口的執行個體化。
3 /// </summary>
4 public interface IDALManager
5 {
6 IProduct CreateProduct();
7 IProductList CreateProductList();
8
9 IProductIn CreateProductIn();
10 IProductInList CreateProductInList();
11
12 IProductStore CreateProductStore();
13 IProductStoreList CreateProductStoreList();
14
15 }
這個接口是一個與業務無關的接口,即不是我們定義的實體的抽像接口,而是一個抽像實作的實作化管理接口,即它管理幹具體工作的實體接口的執行個體化,IDALManager在ClassLibDemo.DAL.SQLServer項目中有對應的實作DALManager:
1 /// <summary>
2 /// DALManager實作,負責業務接口的執行個體化。
3 /// </summary>
4 public class DALManager : IDALManager
6 public IProduct CreateProduct()
7 {
8 return new Product();
9 }
10
11 public IProductList CreateProductList()
12 {
13 return new ProductList();
14 }
15
16 public IProductIn CreateProductIn()
17 {
18 return new ProductIn();
19 }
20
21 public IProductInList CreateProductInList()
22 {
23 return new ProductInList();
24 }
25
26 public IProductStore CreateProductStore()
27 {
28 return new ProductStore();
29 }
30
31 public IProductStoreList CreateProductStoreList()
32 {
33 return new ProductStoreList();
34 }
35 }
那麼IDALManager的執行個體化由誰負責呢,請看到ClassLibDemo.DAL.Interface項目中類DALHelper:
2 /// IDALManager執行個體化過程。
4 public class DALHelper
5 {
6 const string ConfigKey="ClassLibDemo.DAL";
7 public static IDALManager DALManager
8 {
9 get
10 {
11 return ContextHelper.GetContext().Container.GetComponentInstance(ConfigKey) as IDALManager;
12 }
13 }
14 }
DALHelper類中隻有一個名稱為DALManager的隻讀屬性,傳回AgileEAS.NET平台IOC容器中的一個名稱為ClassLibDemo.DAL的對象,也就是IDALManager接口的執行個體化由DALHelper調用IOC容器來完成了IDALManager具體實作的動态挂載與延遲綁定,對象設計器為我們生成了以上模式的代碼,根據代碼我們需要在系統配置中增加如下的對象配置:
系統配置檔案
至此為止,我們則可以在任何使用實體接口進行業務時采用如下的寫法:
IProductList table = DALHelper.DALManager.CreateProductList();
消費者代碼
當我們需要使用資料接口層中的某個接口進行業務操作的時間,我們需要使用DALHelper.DALManager的特定方法進行實作化抽像的資料層接口,我們隻需要修改原有代碼中的實體執行個體化過程的代碼,我貼上ClassLib.OrmDemo項目中的ConditionDemo類的代碼:
ConditionDemo
按此方法修改ClassLib.OrmDemo中的其他代碼,最後編譯運作:
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NET_10AF4/image_19.png"></a>
連結
<a href="http://www.cnblogs.com/eastjade/archive/2010/09/19/1830812.html">一步一步教你使用AgileEAS.NET基礎類庫進行應用開發-系列目錄</a>
<a href="http://www.cnblogs.com/eastjade/archive/2010/09/12/1824405.html">AgileEAS.NET平台開發指南-系列目錄</a>
<a href="http://www.cnblogs.com/eastjade/archive/2010/09/09/1822530.html">AgileEAS.NET應用開發平台介紹-文章索引</a>
<a href="http://www.cnblogs.com/eastjade/archive/2010/09/15/1826870.html">AgileEAS.NET平台應用開發教程-案例計劃</a>
<a href="http://www.smarteas.net/">AgileEAS.NET官方網站</a>
<a href="http://www.agilelab.cn/">靈活軟體工程實驗室</a>
QQ群:116773358
本文轉自 agilelab 51CTO部落格,原文連結:http://blog.51cto.com/agilelab/603035