1

例如。我的会话工厂位于 MyDomain.SessionProvider 类中。会话可以打开using ISession session = SessionProvider.Instance.OpenSession()

步骤:SessionProvider.cs

public static SessionProvider Instance { get; private set; }
        private static ISessionFactory _SessionFactory;

        static SessionProvider()
        {
            var provider = new SessionProvider();
            provider.Initialize();
            Instance = provider;
        }

        private SessionProvider()
        {

        }

        private void Initialize()
        {
            string csStringName = "ConnectionString";
            var cfg = Fluently.Configure()
               //ommiting mapping and db conf.

                .ExposeConfiguration(c => c.SetProperty("current_session_context_class", "web"))
                .BuildConfiguration();
            _SessionFactory = cfg.BuildSessionFactory();

        }

        public ISession OpenSession()
        {
            return _SessionFactory.OpenSession();
        }

        public ISession GetCurrentSession()
        {
            return _SessionFactory.GetCurrentSession();
        }

步骤:Global.asax.cs

public static ISessionFactory SessionFactory { get; private set; }

申请开始

SessionFactory = SessionProvider.Instance.OpenSession().SessionFactory;

App_BeginRequest

var session = SessionFactory.OpenSession();
CurrentSessionContext.Bind(session);   

EndRequest 处理会话

var session = CurrentSessionContext.Unbind(SessionFactory);
session.Dispose();

Step3.HomeController 我应该使用当前会话

var session = SessionProvider.Instance.GetCurrentSession();
using (var tran = session.BeginTransaction())
{
   //retrieve data from session
}

现在,尝试在我的控制器上检索数据,例如 desc。在步骤 3。我收到错误消息,提示我的会话已关闭。我试图删除 global.asax 中的 Application_EndRequest 块,因为我的事务被会话包装但没有成功。还是一样的错误。

第二个/侧面问题:这种模式是否被广泛接受,或者最好将其包装在 mvc 控制器的自定义属性中。谢谢。

更新:在我的控制器上尝试在线实例化当前会话

var session = SessionProvider.Instance.GetCurrentSession();

我收到以下错误:

**Connection = 'session.Connection' threw an exception of type 'NHibernate.HibernateException'**

**base {System.ApplicationException} = {"Session is closed"}**
4

2 回答 2

2

谢谢@LeftyX

我使用 TekPub 视频 Mastering NHibernate 解决了这个问题,并进行了一些自定义。

全球.asax

//Whenever the request from page comes in (single request for a page)
//open session and on request end close the session.

public static ISessionFactory SessionFactory =
   MyDomain.SessionProvider.CreateSessionFactory();

public MvcApplication() 
{
    this.BeginRequest += new EventHandler(MvcApplication_BeginRequest);
    this.EndRequest +=new EventHandler(MvcApplication_EndRequest);
}

private void MvcApplication_EndRequest(object sender, EventArgs e)
{
    CurrentSessionContext.Unbind(SessionFactory).Dispose();
}

private void MvcApplication_BeginRequest(object sender, EventArgs e)
{
    CurrentSessionContext.Bind(SessionFactory.OpenSession());
}

protected void Application_Start()
{
    SessionFactory.OpenSession();
}

在我的控制器里面

 var session = MvcApplication.SessionFactory.GetCurrentSession();
 {
     using (ITransaction tx = session.BeginTransaction())
      {... omitting retrieving data}
 }
于 2012-05-22T09:30:10.963 回答
2

您可以在此处此处找到几个简单易用的实现,并在此处找到一些代码。
我喜欢 Ayende 保持一切简单和干净的方法:

public class Global: System.Web.HttpApplication
{
    public static ISessionFactory SessionFactory = CreateSessionFactory();

    protected static ISessionFactory CreateSessionFactory()
    {
        return new Configuration()
            .Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"))
            .BuildSessionFactory();
    }

    public static ISession CurrentSession
    {
        get{ return (ISession)HttpContext.Current.Items["current.session"]; }
        set { HttpContext.Current.Items["current.session"] = value; }
    }

    protected void Global()
    {
        BeginRequest += delegate
        {
            CurrentSession = SessionFactory.OpenSession();
        };
        EndRequest += delegate
        {
            if(CurrentSession != null)
                CurrentSession.Dispose();
        };
    }
}

在我的项目中,我决定使用 IoC 容器(StructureMap)。
如果你有兴趣可以看看这里

于 2012-05-22T08:11:58.460 回答