关于在IBatis中返回DataSet
2007-05-30 09:55 Anders Cui 阅读(4109) 评论(20) 编辑 收藏 举报
在一个邮件列表的讨论中,了解了如何在IBatis.Net中返回DataSet以及一些相关的内容。
在这个过程中,我们还可以顺便得出获得SQL语句的方法:
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;
}
{
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)
{
ISqlMapper mapper = GetMapper();
IMappedStatement statement = mapper.GetMappedStatement(statementName);
if (!mapper.IsSessionStarted)
{
mapper.OpenConnection();
}
RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, mapper.LocalSession);
return scope.PreparedStatement.PreparedSql;
}
{
ISqlMapper mapper = GetMapper();
IMappedStatement statement = mapper.GetMappedStatement(statementName);
if (!mapper.IsSessionStarted)
{
mapper.OpenConnection();
}
RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, mapper.LocalSession);
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的初衷和意图。
作者:Anders Cui
出处:http://anderslly.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://anderslly.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端