惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Recorded Future
Recorded Future
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
S
SegmentFault 最新的问题
博客园 - 司徒正美
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
博客园 - 叶小钗
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
腾讯CDC
博客园 - 聂微东
D
DataBreaches.Net
博客园 - Franky
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 【当耐特】
量子位
宝玉的分享
宝玉的分享
D
Docker
T
Tailwind CSS Blog
IT之家
IT之家
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Scott Helme
Scott Helme
Project Zero
Project Zero
Microsoft Azure Blog
Microsoft Azure Blog
AWS News Blog
AWS News Blog
Google DeepMind News
Google DeepMind News
H
Heimdal Security Blog
W
WeLiveSecurity
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
NISL@THU
NISL@THU
C
Cybersecurity and Infrastructure Security Agency CISA
Google DeepMind News
Google DeepMind News
T
Threatpost
TaoSecurity Blog
TaoSecurity Blog
N
News and Events Feed by Topic
aimingoo的专栏
aimingoo的专栏
Recent Commits to openclaw:main
Recent Commits to openclaw:main
www.infosecurity-magazine.com
www.infosecurity-magazine.com
SecWiki News
SecWiki News
S
Securelist

博客园 - Richard Sun

推荐:学习unity3d的好站点 WPF中的MVVM模式 - Richard Sun - 博客园 谈谈我理解的WPF团队模型——在UI Designer与Developer之间 101个Google技巧——Google技巧的终极收集 又一次不太爽的经历 王侯将相,宁有种乎 唱几句反调 为一路通(16tone)开博 一路通没有帮到我 白领小秘Vivien的一个上午 - office SharePoint 协同作业- 之图示例1 NHibernate和Cuyahoga(二)(翻译): NHibernate和Cuyahoga初体验 NHibernate和Cuyahoga初体验 Cuyahoga的安装及结构 Team Foundation Server (TFS)中关于连接和用户验证的解决方案 DNS在活动目录中的使用常见问题及解答 学生与女老师的对答 (强) 十招轻松搞定社会媒体 jQuery插件集合.(240).
NHibernate和Cuyahoga(一)(翻译)
Richard Sun · 2008-02-19 · via 博客园 - Richard Sun

在我的所有试图去解释Cuyahoga是怎么样利用NHibernate去持久化对象的文章中,这是第一篇. 在这一系列的文章中提到的NHibernate的版本是0.7, 还有Cuyahoga是0.7.0.1 .

Configuration:
       那么让我们开始NHibernate的配置吧. 对象和数据库之间的映射存在在运行时. 在Cuyahoga中, 配置的过程被封装在Cuyahoga.Core.Service.SessionFactory类中. 这个类主要封装了NHibernate的Configuration和SessionFactory类的功能. 她使用了单件模式(Singleton),因为创建Configuration的代价是非常昂贵的, 并且整个应用程序线程可以安全的使用这个相同的实例. 在创建时,Cuyahoga的核心类的在NHibernate中被注册,而且一个ISessonFactory实例被创建:

Configuration config = new Configuration();
this._nhibernateConfiguration = config.AddAssembly(this.GetType().Assembly);
this._nhibernateFactory = this._nhibernateConfiguration.BuildSessionFactory();

      既然核心类和SessionFactory处在相同的程序集中,我们可以通过简单的添加程序集的方法(this.GetType().Assembly)一次性将核心类注册到NHibernate中. 
 
          提示:Cuyahoga SessionFactory拥有一个NHibernate Configuration的引用.文档说:这个Configuration是被丢弃的,但是我们在独立的Cuyahoga的Modules中需要用它添加独立Modules的映射(对象关系).

public void RegisterPersistentClass(Type type)
{
         
if (this._nhibernateConfiguration.GetClassMapping(type) == null)
         
{
                   
// Class isn't mapped yet, so do it now.
                   this._nhibernateConfiguration.AddClass(type);                  
                   
this._classesAdded = true;
         }

}

        提示:NHibernate SessionFactory不会被立即重新创建. Modules在Configuration中可以添加多个映射类, 然而在添加完之后,Modules有责任调用Rebuild()方法重新创建NHibernate的ISessionFactory.

 Session管理方式:
       在Configuration后,在真正需要的地方我们就拥有了一切(NHibernate). 使用GetSession方法,可以获得一个打开的可以用于持久化对象的NHibernate session实例.

       Cuyahoga不会使用”真正”的NHibernate session来持久化核心对象, 但是她拥有一个外观类Cuyahoga.Core.CoreRepository,它隐藏了几乎所有的NHibernate对Cuyahoga特定的操作. 它包含了常用方法:检索, 保存, 更新 ,删除一个指定的类型的对象和一些细节的操作(例如:过滤和排序). 说到低,CoreRepository使用了 从SessionFactory中获得的NHibernate session.

       Session的管理使用了模式, 我们叫她”Session Per Request”. 在Asp.NET的整个页面生命周期中, 在页面的每一个请求开始的时候, 单个NHibernate session的实例被创建. 通常我们将他存储在HttpContext.Current.Items 集合中. Cuyahoga稍微有点不一样, 她是存放CoreRepository类的一个实例, 但是他们在概念上或多或少是差不多的.

       我们有一个专门的HttpModule(Cuyahoga.Web.Util.NHSessionModule.), 来处理在请求时存储CoreRepository. 在开始请求事件的时候,我们创建CoreRepository实例, 并且在结束请求的时候关闭session.

private void Context_BeginRequest(object sender, EventArgs e)
{
         
// Create the repository for Core objects and add it to the current HttpContext.
         CoreRepository cr = new CoreRepository(true);
         HttpContext.Current.Items.Add(
"CoreRepository", cr);
}

private void Context_EndRequest(object sender, EventArgs e)
{
         
// Close the NHibernate session.
         if (HttpContext.Current.Items["CoreRepository"!= null)
         
{
                   CoreRepository cr 
= (CoreRepository)HttpContext.Current.Items["CoreRepository"];
                   cr.CloseSession();
         }

}

       你也可以将这些内容放在Global.asax中, 这只和个人爱好有关. 我喜欢使用HttpModules.现在, 在Cuyahoga中的Asp.NET页面和用户控件可以在页面的生命周期的任何时候访问CoreRepository了.

       CoreRepository没有为Modules提供多的功能, 所以他们必须自己管理持久化. 这样的例子,可以在Cuyahoga.Modules.Articles.ArticleModule或者Cuyahoga.Modules.StaticHtml.StaticHtmlModule的父类Cuyahoga.Core.Domain.ModuleBase中找到.

       Modules使用和编译页面的Cuyahoga.Web.UI.PageEngine类相同的session是非常重要的. 他们可以从Context.Items集合中的CoreRepository中获得session. 然而目前,当Modules在需要一个session的时候,产生一个事件, PageEngine会处理这个事件.这是早期Cuyahoga还没有使用Context.Items来存储session的一个小的遗留. 将会重构.