我接管了一个由已经离开的第三方顾问编写的项目。Spring.Net HibernateTemplate.Execute澄清
我来自EF backgournd。其中一个DAO类有以下内容,我发现很难让我的头脑一步一步详细解释正在发生的事情。如果有人能够帮助我理解这段代码将非常感谢。
return HibernateTemplate.Execute(
delegate(ISession hbSession) // <<--What is this code actually trying to do?
{
string queryText = "from {0} x where x.Code = :Code";
queryText = string.Format(queryText, typeof(Product));
IQuery query = hbSession.CreateQuery(queryText);
query.SetParameter("Code", productCode);
query.SetCacheable(true);
query.SetCacheRegion(CoreCacheConstants.ProductQueryCacheRegion); // <-- What is this code trying to do.
var fund = query.UniqueResult(); // <-- Is this similar to DISTINCT keyword in LINQ?
if (fund == null)
throw new ArgumentException(String.Format("No product found with productcode: {0}", productCode:));
NHibernateUtil.Initialize(((Product)Product).Details); // <--What is this code trying to do. And where is the execute method for above queries.
return fund;
}
) as Product
基本上,我与委托部分混淆,为什么使用委托而不是简单查询数据库。上述方法有什么好处。
另外我不能看到任何nHibernate的ORM映射XML。 Spring.NET是否需要映射文件以将数据传递到数据源?为了说明ISession如何知道要连接哪个数据库以及要使用哪个表等
@Marjin,感谢你的帮助。你是如何学习Spring.net的,你会推荐哪本书? Spring.net文档假设读者知道回调,委托调用等。我正在寻找101描述。 – 2012-02-11 20:37:08
另外,这个查询的目的是什么IQuery query = hbSession.CreateQuery(queryText)。换句话说,为什么要放在会话而不是直接查询数据库。我无法看到将查询本身存储在会话中的优势。 – 2012-02-11 20:39:07
不幸的是,我不能推荐你一本关于Spring.Net的书。其实,我从文档,spring.net论坛和源代码中学到了很多东西。对于Spring框架的一般理解,[Spring in Action](http://www.manning.com/walls3/)是一本很好的书,但它适用于Java的Spring。 – Marijn 2012-02-11 20:46:20