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服务接口的访问量比较大的话,给网络传输带来一定的性能影响。
下一篇文章中,我将给出我自己的一个另一种实现方法,敬请关注。