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;
}
}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;
} 客戶端代碼:
//----------------------------------------------------------
// 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服務接口的訪問量比較大的話,給網絡傳輸帶來一定的性能影響。
下一篇文章中,我將給出我自己的一個另一種實現方法,敬請關注。

浙公網安備 33010602011771號