什么是确保您的信息库和一流的UnitOfWork共享相同的NHibernate会话对象的正确方法? [英] What is the right way to ensure your Repository and UnitOfWork class share the same nhibernate session object?

查看:98
本文介绍了什么是确保您的信息库和一流的UnitOfWork共享相同的NHibernate会话对象的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有编辑和删除对象的问题,我认为它是因为我不共享我的仓库类和我的UnitOfWork类之间的相同的会话对象。我试图找到最好的方式一些文件,这样组装起来,所以我共享同一个会话对象。

I am having issues editing and deleting objects and i think its because i am not sharing the same session objects between my repository classes and my unitofwork class. I am trying to find some document on the best way to wire this up so I share the same session object.

我使用ninject作为MVC开发的网站我的IOC容器。

I am using ninject as my IOC container in the mvc website.

推荐答案

我通常设置会话作为仓库的依赖关系,因此Ninject能够解决的依赖(ISession的= NHibernate.ISession):

I usually set the session as a dependency of the repository, so Ninject can resolve the dependency (ISession = NHibernate.ISession):

public UserRepository(ISession session)
{
    ...
}

这是我如何设置绑定:

kernel.Bind<ISession>().ToMethod(x => GetRequestSession()).InRequestScope();

所以需要会话时Ninject将调用GetRequestSession()来检索会话。的功能的实现如下:

So when a session is required Ninject will call GetRequestSession() to retrieve the session. The function is implemented as follows:

private static ISession GetRequestSession()
        {
            IDictionary httpContextItems = HttpContext.Current.Items;

            ISession session;
            if (!httpContextItems.Contains(MvcApplication.SESSION_KEY))
            {
                // Create an NHibernate session for this request
                session = MvcApplication.SessionFactory.OpenSession();
                httpContextItems.Add(MvcApplication.SESSION_KEY, session);
            }
            else
            {
                // Re-use the NHibernate session for this request
                session = (ISession)httpContextItems[MvcApplication.SESSION_KEY];
            }
            return session;
        }

NHibernate会话存储在HttpContext的项目。这是一个关键值集合可使用一个请求的handlng期间存储和共享数据

The NHibernate session is stored in the HttpContext items. This is a key-value collection which can be used to store and share data during the handlng of one request.

创建每个请求一次会议,并请求期间重新使用。

The session is created only once per request, and is re-used during the request.

MvcApplication.SESSION_KEY只是我在Global.asax中定义的常数串,以便能够从HttpContext的存储和检索的会话。另外,会议工厂坐落在Global.asax中,并在启动时被创建。

MvcApplication.SESSION_KEY is just a constant string I defined in Global.asax to be able to store and retrieve the session from the HttpContext. Also the session factory is located in global.asax and is created at start-up.

您的工作类的单位也可以设置的Isession作为依赖,因此Ninject将解决这种依赖关系,以及因此使用相同的会话。在另一方面,你可能不需要工作类的单位,因为NHibernate的本身执行的ISession已经是工作类的单位。

Your unit of work class could also set the ISession as a dependency, so Ninject will resolve this dependency as well and therefore use the same session. On the other hand, you might not need a unit of work class, because NHibernate's implementation of ISession in itself is already a unit of work class.

我不知道这是否是一个最佳实践,但它的作品完美的我。

I'm not sure if this is a best practice, but it works perfectly for me.

这篇关于什么是确保您的信息库和一流的UnitOfWork共享相同的NHibernate会话对象的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆