上面的示意图中,步骤 7 指向的 Remoting Server 就是 Host 程序,
而 Remoting Server 包裹着的 ** RemoteCustomer ** 就是真正提供服务的数
据操作类。
以下所列代码即为该类的部分实现:
代码 13 :使用 Data Access Logic 进行 Remoting 调用 – 3 , RemoteCustomer
public class ** RemoteCustomer ** : ** MarshalByRefObject **
{
public ** RemoteCustomer ** () { }
public ** ArrayList ** GetAllCustomers()
{
** SqlConnection ** conn = new ** SqlConnection ** (
** Helper ** .GetApplicationSetting("ConnectionString"));
// 通过 ObjectSpaces 获取所有 Customer 数据
** ObjectSpace ** os = new ** ObjectSpace ** (
** Helper ** .GetApplicationSetting("MappingFile"), conn);
** ObjectSet ** ost = os.GetObjectSet( typeof ( ** MyCustomer ** ), "");
// 以 ArrayList 方式返回所有 Customer 数据
// _ 注:当前版本中, _ _ ObjectSet _ _ 对象通过 _ _ Remoting _ _ 进行传递有 _ _ Bug _ _ _
** ArrayList ** al = new ** ArrayList ** (ost);
return al;
}
}
作者相信,在看完代码后,肯定有些朋友会产生这样的疑问:
代码 13 中的 ** RemoteCustomer ** 完成的也是我们前面分析过的
Data Access Logic 所能完成的工作,虽然当中隔了一层 Remoting ,
但本质相同,难道就不能与代码 11 , 12 中的 ** CustomerDal_ORM ** 进行
一次“有效重组”吗?
回答是肯定的!
不过,我们还是需要做两个非常简单的操作:
(1) 大家还记得代码 9 中的 ** DalBase ** 吗?只要我们令它从 ** MarshalByRefObject ** 继承(原先是默认的 ** object ** ),即可“轻松”解决这个问题!但也别高兴太早,一旦如此,所有其它的 Data Access Logic 类也将不得不接受这“ 多余的馈赠 ” L ,要知道,毕竟通过 Remoting 进行 Data Access Logic 操作的机会还不是很多(一般通过 Business Logic 即可解决问题),这样的“馈赠”并不是人人可以消受的(这也是作者并没在 DAF Solution 中这么实现的原因)!
(2) 将上述 ** RemoteCustomer ** 的代码并入 ** CustomerDal_ORM ** 中,但是,请注意:别忘了将方法名称改掉(因为已经有一个 GetAllCustomers 方法存在,虽然返回类型不同,但任何 .NET 下的 Compiler 都是无法区分这种差别的 J )!