Session Management in Castle Active Record

与世无争的帅哥 提交于 2019-12-24 07:01:06

问题


How do I manage session if I am using Castle Active Record over nHibernate. Basically I can manage the life cycle of ISession on my own if I am using nHibernate directly. But when I am using Castle AR it does not give me a way to manage the life cycle of the session. I want to use single Session per thread.


I am using Castle AR in a WCF service and would like to use Session per WCF Request.


回答1:


Instead of using ISession, in Castle ActiveRecord you want SessionScope:

using(new SessionScope())
{
  ; // do work here
}

If you need access to the ISession inside the SessionScope for some reason, you can do this:

ISession dbSession = Castle.ActiveRecord.ActiveRecordMediator
      .GetSessionFactoryHolder().CreateSession(
          typeof(Castle.ActiveRecord.ActiveRecordBase));

More documentation is here:

http://www.castleproject.org/activerecord/documentation/trunk/usersguide/scopes.html#sess_scope

and here:

http://www.castleproject.org/activerecord/documentation/trunk/manual/scopes.html




回答2:


I assume you are working in a web app. Is that not the case?

There are a couple of ways to do it - Castle AR documentation

The simplest way is to the use SessionScopeWebModule to give a session per request.



来源:https://stackoverflow.com/questions/2223504/session-management-in-castle-active-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!