實戰 .Net 數據訪問層 - 9

 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Ok,上面的DafBase只是個Abstract Base ClassABC),請繼續

看下面的真正Daf

 

代碼8:讓DAF工作起來!

// MyDaf:提供當前應用程序所需的Data Access支持,從DafBase繼承

public class MyDaf: DafBase

{

    public MyDaf() { }

   

    protected override DefBase CallDalMethod(

object[] paramsValue)

    {

       ...

       DefBase def = base.CallDalMethod(paramsValue);      

       ...

return def;

    }

}

 

// CustomerDaf:包含實際的數據訪問方法聲明,

//   通過調用DAL獲取數據,從MyDaf繼承

public class CustomerDaf: MyDaf

{

    public CustomerDaf() { }

 

    public MyCustomer GetCustomerById(string strId)

    {

       ...  // 檢查或轉換傳入參數

       MyCustomer cust = (MyCustomer)CallDalMethod(

new object[] { strId });

       ... // 對數據結果進行修改或轉換

       return cust

    }

 

    public MyCustomer GetCustomers(string strName)

    {

       ...  // 檢查或轉換傳入參數

       MyCustomer cust = (MyCustomer)CallDalMethod(

new object[] { strName });

       ... // 對數據結果進行修改或轉換

return cust

    }

    ...

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

統一的Data Access Logic調用推給DefBase完成(需要根據配置

信息進行一系列“枯燥無味”的處理),自定義部分才由自己來完成,

這就是MyDafCustomerDaf出現的真正原因!

       MyDaf相當於當前Enterprise Application的數據訪問基礎,可以

針對應用程序的特點提供一些基本的服務,在此服務下,真正的

CustomerDaf就可以集中精力對具體的Data Access Logic(不同於

Business Logic)進行處理了,例如:數據訪問前的基本校驗,對返

回結果進行轉換操作等。

       根據Ease of Use原則,我們也可以繞過MyDaf這層,直接讓

CustomerDafDafBase繼承,在這種情況下,整個Inheritance

Hierarchy就顯得更加簡單了。

 

下一段:http://www.csdn.net/develop/Read_Article.asp?id=27552

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章