MyGeneration学习笔记(4) :在Web Service中使用dOOdad(上)
来源:互联网 发布:js字符串反转函数 编辑:程序博客网 时间:2024/05/14 00:28
dOOdad对Web Service的支持还不够好,dOOdad的核心是DataTable,而在ADO.Net 2.0中的DataTable虽然相对1.x功能扩展了不少,微软鼓吹DataTable功能有多强多强,据说Web Service也都支持返回DataTable,但俺试了一下,发现还存在不少问题。
既然Web service不支持DataTable作返回值,所以要将业务实体类内部的信息在网络中传输,就得想其他办法。
下面是MyGeneration论坛上某网友提供的一种解决方法,基本思路是将dOOdad业务实体内中的DataTable封装到DataSet,将dataset做为参数或返回值:
//Concrete class中添加如下两个方法:
public class Employees : _Employees
...{
public void FromDataSet(DataSet ds)
...{
this.DataTable = ds.Tables[0];
}
public DataSet ToDataSet()
...{
DataSet dataSet = new DataSet();
dataSet.Tables.Add(this.DataTable);
return dataSet;
}
}
...{
public void FromDataSet(DataSet ds)
...{
this.DataTable = ds.Tables[0];
}
public DataSet ToDataSet()
...{
DataSet dataSet = new DataSet();
dataSet.Tables.Add(this.DataTable);
return dataSet;
}
}
Web Service中的代码片断:
[WebMethod]
public DataSet LoadAll()
...{
try
...{
Employees emps = new Employees();
emps.LoadAll();
return emps.ToDataSet();
}
catch ...{ return null; }
}
[WebMethod]
public bool Save(DataSet dataset)
...{
bool wasSaved = false;
try
...{
Employees emps = new Employees();
emps.FromDataSet(dataset);
emps.Save();
wasSaved = true;
}
catch ...{ }
return wasSaved;
}
public DataSet LoadAll()
...{
try
...{
Employees emps = new Employees();
emps.LoadAll();
return emps.ToDataSet();
}
catch ...{ return null; }
}
[WebMethod]
public bool Save(DataSet dataset)
...{
bool wasSaved = false;
try
...{
Employees emps = new Employees();
emps.FromDataSet(dataset);
emps.Save();
wasSaved = true;
}
catch ...{ }
return wasSaved;
}
客户端代码:
//----------------------------------------------------------
// Invoke the Web Service and Load all of the Data
//----------------------------------------------------------
EmployeesService empsService = new EmployeesService();
DataSet ds = empsService.LoadAll();
//----------------------------------------------------------
// Load that Data into a local Employees object
//----------------------------------------------------------
Employees emps = new Employees();
emps.FromDataSet(ds);
emps.FirstName = "WebService!!";
emps.GetChanges(); // very important
//----------------------------------------------------------
// Reconvert to a DataSet (after calling GetChanges)
//----------------------------------------------------------
ds = emps.ToDataSet();
//----------------------------------------------------------
// Marshall it back to the web service
//----------------------------------------------------------
empsService.Save(ds);
// Invoke the Web Service and Load all of the Data
//----------------------------------------------------------
EmployeesService empsService = new EmployeesService();
DataSet ds = empsService.LoadAll();
//----------------------------------------------------------
// Load that Data into a local Employees object
//----------------------------------------------------------
Employees emps = new Employees();
emps.FromDataSet(ds);
emps.FirstName = "WebService!!";
emps.GetChanges(); // very important
//----------------------------------------------------------
// Reconvert to a DataSet (after calling GetChanges)
//----------------------------------------------------------
ds = emps.ToDataSet();
//----------------------------------------------------------
// Marshall it back to the web service
//----------------------------------------------------------
empsService.Save(ds);
这个方法不错,不过有个小问题就是这个DataSet有自己的Schema,如果客户端对该web服务接口的访问量比较大的话,给网络传输带来一定的性能影响。
下一篇文章中,我将给出我自己的一个另一种实现方法,敬请关注。
- MyGeneration学习笔记(4) :在Web Service中使用dOOdad(上)
- MyGeneration学习笔记(4) :在Web Service中使用dOOdad(上)
- MyGeneration学习笔记(5) :在Web Service中使用dOOdad(中)
- MyGeneration学习笔记(5) :在Web Service中使用dOOdad(中)
- MyGeneration学习笔记(6) :在Web Service中使用dOOdad(下)
- MyGeneration学习笔记(6) :在Web Service中使用dOOdad(下)
- MyGeneration学习笔记(9) :在WebService使用dOOdad时,对ToXml/FromXml的一点改进
- MyGeneration学习笔记(9) :在WebService使用dOOdad时,对ToXml/FromXml的一点改进
- MyGeneration学习笔记(2) : 使用dOOdad提供的通用操作
- MyGeneration学习笔记(2) : 使用dOOdad提供的通用操作
- MyGeneration学习笔记(7) :dOOdad的String Properties和动态查询
- MyGeneration学习笔记(8) :dOOdad提供的数据绑定、特殊函数和事务处理
- MyGeneration学习笔记(7) :dOOdad的String Properties和动态查询
- MyGeneration学习笔记(8) :dOOdad提供的数据绑定、特殊函数和事务处理
- MyGeneration学习笔记
- MyGeneration学习笔记(1) : 使用MyGeneration生成存储过程和数据访问层代码
- MyGeneration学习笔记(1) : 使用MyGeneration生成存储过程和数据访问层代码
- MyGeneration学习笔记(1) : 使用MyGeneration生成存储过程和数据访问层代码
- ATL Internals: Working with ATL 8 (2nd Edition) DOWNLOAD
- hello, blog
- 该是学习是时候了,也是最后一次学习大机会了。我会珍惜
- 我为什么要入党
- Life is a highway
- MyGeneration学习笔记(4) :在Web Service中使用dOOdad(上)
- 不知多少Java程序员能解答这个问题!
- GLIB 安装手记
- intel笔试题&面试题
- 2005 motorola笔试题
- 面试软件必备编程题
- Clouds
- 微软笔试和面试题
- Sun和Apache Harmony的融合前景还不明朗