public static DataSet QueryForDataSet(string statementName, object paramObject)
{
DataSet ds = new DataSet();
ISqlMapper mapper = GetMapper();
IMappedStatement statement = mapper.GetMappedStatement(statementName);
if (!mapper.IsSessionStarted)
{
mapper.OpenConnection();
}
RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, mapper.LocalSession);
statement.PreparedCommand.Create(scope, mapper.LocalSession, statement.Statement, paramObject);
mapper.LocalSession.CreateDataAdapter(scope.IDbCommand).Fill(ds);
return ds;
}
在這個過程中,我們還可以順便得出獲得SQL語句的方法:
public static string GetSql(string statementName, object paramObject)
return scope.PreparedStatement.PreparedSql;
下面是我對文中内容的了解:
并不是所有地方都要OO,在IBatis的Java和.NET實作中都支援Dictionary類型的對象.DataTable因為有了DataView的支援而較IDictionary具有一些優勢.如果我們需要對資料進行額外的排序或者過濾操作,那麼DataTable會更友善一些.是以像這種傳回DataSet的方法會使得IBatis更加易用.
但同時感覺,這種方法将IBatis打開了一個缺口,似乎背離了IBatis的設計初衷——建立一個優秀的“Persistence Ignorance” domain model。比如現在,我們進行一次查詢,獲得“Plain Old CLR Objects”,看起來輸入是與資料持久化相關的,而輸出則完全集中在了領域模型上。
是以,傳回DataSet的方法可能會導緻不好的設計,從長遠來看,也模糊了IBatis的初衷和意圖。
本文轉自一個程式員的自省部落格園部落格,原文連結:http://www.cnblogs.com/anderslly/archive/2007/05/30/queryfordatasetinibatis.html,如需轉載請自行聯系原作者。