I have just started learning NHibernate.
我刚开始学NHibernate。
Over the past few months I have been using IoC / DI (structuremap) and the repository pattern and it has made my applications much more loosely coupled and easier to test.
在过去的几个月中,我一直在使用IoC / DI (structuremap)和存储库模式,它使我的应用程序更加松散地耦合,并且更容易测试。
When switching my persistence layer to NHibernate I decided to stick with my repositories. Currently I am creating a new session on each method call but of course this means that I can not benefit from lazy loading.
当我将我的持久性层切换到NHibernate时,我决定继续使用我的存储库。目前,我正在为每个方法调用创建一个新的会话,但是当然这意味着我不能从延迟加载中获益。
Therefore I wish to implement session-per-request but in doing so this will make my web project dependent on NHibernate (perhaps this is not such a bad thing?). I was planning to inject ISession into my repositories and create and dispose sessions on beginrequest/endrequest events (see http://ayende.com/Blog/archive/2009/08/05/do-you-need-a-framework.aspx)
因此,我希望实现每个请求会话,但这样做将使我的web项目依赖于NHibernate(也许这不是一件坏事?)我计划将ISession注入到我的存储库中,并在beginrequest/endrequest事件上创建和处理session(请参阅http://ayende.com/blog/archive/2009/08/05/do you-need- framework.aspx)
Is this a good approach? Presumably I cannot use session-per-request without having a reference to NHibernate in my web project?
这是一个好方法吗?假设在web项目中没有对NHibernate的引用时,我不能使用每个请求的会话?
Having the web project dependent on NHibernate prompts my next (few) questions - why even bother with the repository? Since my web app is calling services that talk to the repositories, why not ditch the repositories and just add my NHibernate persistance code inside the services? And finally, is there really any need to split out into so many projects. Is a web project and an infrastructure project sufficient?
依赖于NHibernate的web项目会引发我的下一个(少数)问题——为什么还要费心使用存储库呢?既然我的web应用程序正在调用与存储库对话的服务,为什么不抛弃存储库,在服务中添加我的NHibernate持久化代码呢?最后,真的有必要将项目分成这么多项目吗?web项目和基础设施项目是否足够?
I realise that I have veered off a bit from my original question but it seems that everyone seems to have their own opinion on these topics. Some people use the repository pattern with NHibernate, some don't. Some people stick their mapping files with the related classes, others have a separate project for this.
我意识到我有点偏离了我最初的问题,但似乎每个人对这些问题都有自己的看法。有些人使用NHibernate的存储库模式,有些人不使用。有些人将他们的映射文件与相关的类粘在一起,有些人为此有一个单独的项目。
Many thanks, Ben
多谢,本
3 个解决方案
#1
1
I wouldn't make the business logic dependent on NHibernate. I wrote a (more or less simple) class to create the session into a context:
我不会让业务逻辑依赖于NHibernate。我编写了一个(或多或少简单)类,将会话创建到一个上下文中:
using (TransactionService.CreateTransaction())
{
// use repository here
// rollback on exception, only commit when reach this last line:
TransactionService.Commit();
}
You just get an IDisposable, you don't need to know the session.
你只需要一个IDisposable,你不需要知道会话。
The repositories gets an API to access the session. For instance:
存储库获取一个API来访问会话。例如:
// example repository implementation
public Entity Get(Guid id)
{
return SessionProvider.Session.Get<Entity>(id);
}
To implement this, I put the session into a ThreadStatic
variable, which is initialized in CreateTransaction
and returned by SessionProvider.Session
.
为了实现这一点,我将会话放入一个ThreadStatic变量中,该变量在CreateTransaction中初始化并由SessionProvider.Session返回。
#2
1
Take a look at S#arp Architecture. It answers everything you need, including, how to separate data layer (NHibernate), web layer, and business logic.
看看s# arp架构。它可以回答您需要的所有问题,包括如何分离数据层(NHibernate)、web层和业务逻辑。
#3
0
You may want to look at the TransactionScope class:
您可能想看看TransactionScope类:
http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx
http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx
#1
1
I wouldn't make the business logic dependent on NHibernate. I wrote a (more or less simple) class to create the session into a context:
我不会让业务逻辑依赖于NHibernate。我编写了一个(或多或少简单)类,将会话创建到一个上下文中:
using (TransactionService.CreateTransaction())
{
// use repository here
// rollback on exception, only commit when reach this last line:
TransactionService.Commit();
}
You just get an IDisposable, you don't need to know the session.
你只需要一个IDisposable,你不需要知道会话。
The repositories gets an API to access the session. For instance:
存储库获取一个API来访问会话。例如:
// example repository implementation
public Entity Get(Guid id)
{
return SessionProvider.Session.Get<Entity>(id);
}
To implement this, I put the session into a ThreadStatic
variable, which is initialized in CreateTransaction
and returned by SessionProvider.Session
.
为了实现这一点,我将会话放入一个ThreadStatic变量中,该变量在CreateTransaction中初始化并由SessionProvider.Session返回。
#2
1
Take a look at S#arp Architecture. It answers everything you need, including, how to separate data layer (NHibernate), web layer, and business logic.
看看s# arp架构。它可以回答您需要的所有问题,包括如何分离数据层(NHibernate)、web层和业务逻辑。
#3
0
You may want to look at the TransactionScope class:
您可能想看看TransactionScope类:
http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx
http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx