【IT168知识库】
此项目名字叫Cuyahoga,是一个CMS。该项目中就使用了NHibernate0.6作为持久层。并且使用了NHibernate0.6新添加的Lazy loading特性,是学习使用NHibernate的上佳例子。
下面是对他的一些评价:
- Use the HttpContext to store your NHibernate session facade. This is what I call the HttpContext Local Session pattern 8-)
使用HttpContext .Item来存储NH的Session,亦即使用了HttpRequest--Session的模式
- Access to the session facade is provided by a property get in your overrided ASP.NET page's base class, such as "base.GetCoreRepository.LoadObjectById(..)". This is an alternative to having the repository itself control instantiation such as "CoreRepository.Instance.LoadObjectById(..)"
CoreRepository就是一个数据访问逻辑组件了,对Session进行了管理,并且提供数据持久化的方法。部分代码如下:
- A new NHibernate Open Session at beginning of a web request and close it at the end of the request. This is the "session-per-request" pattern.
- Using a custom IHttpModule called NHSessionModule to provide the handlers for Context_BeginRequest and Context_EndRequest. This is a nice way of automatically wiring up your creation/opening and closing and sessions.
使用了一个HttpModule 来进行Session的存储和获得,代码如下: