实战 .Net 数据访问层 - 9

Ok ,上面的 DafBase 只是个 Abstract Base Class ( ABC ),请继续

看下面的真正 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 完成(需要根据 配置 ** **

** 信息 ** 进行一系列“枯燥无味”的处理),自定义部分才由自己来完成,

这就是 MyDaf 和 CustomerDaf 出现的真正原因!

MyDaf 相当于当前 Enterprise Application 的数据访问基础,可以

针对应用程序的特点提供一些基本的服务,在此服务下,真正的

CustomerDaf 就可以集中精力对具体的 Data Access Logic (不同于

Business Logic )进行处理了,例如:数据访问前的基本 校验 ,对返

回结果进行 转换 操作等。

根据 ** Ease of Use ** 原则,我们也可以绕过 MyDaf 这层,直接让

CustomerDaf 从 DafBase 继承,在这种情况下,整个 Inheritance

Hierarchy 就显得更加简单了。

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

Published At
Categories with Web编程
Tagged with
comments powered by Disqus