I am currently playing around with the HybridSessionBuilder class found on Jeffrey Palermo's blog post:
我目前正在研究在Jeffrey Palermo的博客中发现的hybrid essionbuilder类:
http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/
http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/
Using this class, my repository looks like this:
使用这个类,我的存储库如下所示:
public class UserRepository : IUserRepository
{
private readonly ISessionBuilder _sessionBuilder;
public UserRepository(ISessionBuilder sessionBuilder)
{
_sessionBuilder = sessionBuilder;
}
public User GetByID(string userID)
{
using (ISession session = _sessionBuilder.GetSession())
{
return session.Get<User>(userID);
}
}
}
Is this the best way to go about managing the NHibernate session / factory? I've heard things about Unit of Work and creating a session per web request and flushing it at the end. From what I can tell, my current implementation isn't doing any of this. It is basically relying on the Repository to grab the session from the session factory and use it to run the queries.
这是管理NHibernate会话/工厂的最佳方式吗?我听说过一些关于工作单元的事情,每个web请求创建一个会话,并在最后刷新它。从我所知道的情况来看,我目前的实现并没有做任何这方面的工作。它基本上依赖于存储库来从会话工厂获取会话,并使用它来运行查询。
Are there any pitfalls to doing database access this way?
以这种方式访问数据库有什么缺陷吗?
5 个解决方案
#1
37
You should not wrap your ISession in a using statement -- the point of passing the ISessionBuilder into the repository constructor (dependency injection) is that the calling code is responsible for controlling the life cycle of the ISession. By wrapping it in a using, Dispose() is called on the ISession and you won't be able to lazy load object members or persist it.
不应该将ISession打包到using语句中——将ISessionBuilder传递给repository构造函数(dependency injection)的一点是,调用代码负责控制ISession的生命周期。通过使用包装它,在ISession上调用Dispose(),这样您就不能延迟加载对象成员或持久化它。
We do something similar by just passing in an ISession to the repository constructor. Mr. Palermo's code, as I understand it, simply adds lazy initialization of the ISession. I don't think that's needed because why would you new up a repository if you're not going to use it?
我们通过向存储库构造函数传递一个ISession来做类似的事情。按照我的理解,巴勒莫先生的代码只是添加了ISession的惰性初始化。我不认为这是必要的,因为如果你不打算使用它,为什么要新建一个存储库呢?
#2
10
With ASP.Net MVC you want to make sure the life of the session is maintained during the Action method on your controller, as once your controller has exited all your data should be collected. I am not sure if this mechanism will help with that.
ASP。您希望确保在控制器上的操作方法中维护会话的生命,就像您的控制器已经退出了所有的数据一样。我不确定这种机制是否会起到作用。
You might want to look into S#arp Architechure which is a set of libraries and guidance for building ASP.Net MVC application using nHibernate. http://code.google.com/p/sharp-architecture/
您可能希望了解s# arp Architechure,它是一套用于构建ASP的库和指南。使用nHibernate的Net MVC应用程序。http://code.google.com/p/sharp-architecture/
#3
3
This is the setup I used after researching this more. Seems to work great and doesn't have that annoying habit of creating an ISession on static file requests like most guides out there:
这是我进一步研究后使用的设置。似乎工作得很好,并且没有那种烦人的习惯,在静态文件请求上创建一个ISession,像那里的大多数指南:
http://www.kevinwilliampang.com/2010/04/06/setting-up-asp-net-mvc-with-fluent-nhibernate-and-structuremap/
#4
1
I wouldn't open and close sessions on each data request to NHibernate. I would use the Unit of Work libraries that many others suggest or do some more reading. NHForge.org is getting started and I believe that there are some practices on setting up NHibernate for a general web application.
我不会对NHibernate的每个数据请求打开和关闭会话。我会使用许多人建议的工作库单元,或者多读一些书。NHForge.org正在启动,我相信有一些为通用web应用程序设置NHibernate的实践。
One of the "oh wow that's cool moments" that I've gotten from NHibernate was taking advantage of lazily loading collections during development. It was a neat experience being able to not have to do all those joins in order to display data on some associated object.
我从NHibernate得到的“哇,好酷的时刻”之一就是在开发过程中缓慢地加载集合。不需要做所有这些连接,就可以在关联的对象上显示数据,这是一种很棒的体验。
By closing the session like this, the above scenario would not be possible.
通过这样结束会话,上述场景是不可能的。
There might be something that is going on with transactions as well.
事务中也可能发生一些事情。
#5
1
Just found a clean solution using Unity to inject a session per request:
刚刚找到了一个干净的解决方案,使用Unity为每个请求注入一个会话:
http://letsfollowtheyellowbrickroad.blogspot.com/2010/05/nhibernate-sessions-in-aspnet-mvc.html
http://letsfollowtheyellowbrickroad.blogspot.com/2010/05/nhibernate-sessions-in-aspnet-mvc.html
#1
37
You should not wrap your ISession in a using statement -- the point of passing the ISessionBuilder into the repository constructor (dependency injection) is that the calling code is responsible for controlling the life cycle of the ISession. By wrapping it in a using, Dispose() is called on the ISession and you won't be able to lazy load object members or persist it.
不应该将ISession打包到using语句中——将ISessionBuilder传递给repository构造函数(dependency injection)的一点是,调用代码负责控制ISession的生命周期。通过使用包装它,在ISession上调用Dispose(),这样您就不能延迟加载对象成员或持久化它。
We do something similar by just passing in an ISession to the repository constructor. Mr. Palermo's code, as I understand it, simply adds lazy initialization of the ISession. I don't think that's needed because why would you new up a repository if you're not going to use it?
我们通过向存储库构造函数传递一个ISession来做类似的事情。按照我的理解,巴勒莫先生的代码只是添加了ISession的惰性初始化。我不认为这是必要的,因为如果你不打算使用它,为什么要新建一个存储库呢?
#2
10
With ASP.Net MVC you want to make sure the life of the session is maintained during the Action method on your controller, as once your controller has exited all your data should be collected. I am not sure if this mechanism will help with that.
ASP。您希望确保在控制器上的操作方法中维护会话的生命,就像您的控制器已经退出了所有的数据一样。我不确定这种机制是否会起到作用。
You might want to look into S#arp Architechure which is a set of libraries and guidance for building ASP.Net MVC application using nHibernate. http://code.google.com/p/sharp-architecture/
您可能希望了解s# arp Architechure,它是一套用于构建ASP的库和指南。使用nHibernate的Net MVC应用程序。http://code.google.com/p/sharp-architecture/
#3
3
This is the setup I used after researching this more. Seems to work great and doesn't have that annoying habit of creating an ISession on static file requests like most guides out there:
这是我进一步研究后使用的设置。似乎工作得很好,并且没有那种烦人的习惯,在静态文件请求上创建一个ISession,像那里的大多数指南:
http://www.kevinwilliampang.com/2010/04/06/setting-up-asp-net-mvc-with-fluent-nhibernate-and-structuremap/
#4
1
I wouldn't open and close sessions on each data request to NHibernate. I would use the Unit of Work libraries that many others suggest or do some more reading. NHForge.org is getting started and I believe that there are some practices on setting up NHibernate for a general web application.
我不会对NHibernate的每个数据请求打开和关闭会话。我会使用许多人建议的工作库单元,或者多读一些书。NHForge.org正在启动,我相信有一些为通用web应用程序设置NHibernate的实践。
One of the "oh wow that's cool moments" that I've gotten from NHibernate was taking advantage of lazily loading collections during development. It was a neat experience being able to not have to do all those joins in order to display data on some associated object.
我从NHibernate得到的“哇,好酷的时刻”之一就是在开发过程中缓慢地加载集合。不需要做所有这些连接,就可以在关联的对象上显示数据,这是一种很棒的体验。
By closing the session like this, the above scenario would not be possible.
通过这样结束会话,上述场景是不可能的。
There might be something that is going on with transactions as well.
事务中也可能发生一些事情。
#5
1
Just found a clean solution using Unity to inject a session per request:
刚刚找到了一个干净的解决方案,使用Unity为每个请求注入一个会话:
http://letsfollowtheyellowbrickroad.blogspot.com/2010/05/nhibernate-sessions-in-aspnet-mvc.html
http://letsfollowtheyellowbrickroad.blogspot.com/2010/05/nhibernate-sessions-in-aspnet-mvc.html