ORM 存储库模式

发布于 2024-10-20 08:31:07 字数 464 浏览 1 评论 0原文

这更多是我在寻求意见的问题。我正在开发一个同时使用 NHibernate 和 EntityFramework 的项目(这是设计使然,需要灵活性)。因此,我继续并开始研究存储库模式,但遇到了一个轻微的困境。

基本上,我想知道你们对以下领域的看法:

  1. 存储库应该是单例吗? - 这将允许我保持会话打开,但同时,我认为它将保持与数据库的连接打开。对于 NHibernate,ORM 只能保证同一会话中的对象是相同的。这是轻松编码的理想选择,但肯定有一些方法可以使用键并覆盖 GetHashCode 和 Equals 方法来克服这个问题。

  2. 如果它不是单例(或者即使是),我是否应该在使用连接后立即关闭它们?对于 NHibernate,这意味着每次存储库“处置”时(即每次使用后)都关闭会话。

  3. 您是否为 NHibernate 或 EF 4.0 实现了存储库模式并发现了任何有用的想法?

This is more of a question where I am looking for opinions. I am working on a project that uses both NHibernate and EntityFramework (this is by design, wanted flexibility). So, I went ahead and started working on a Repository pattern, but came across a slight dilemma.

Basically, I wanted to know what you guys think about the following areas:

  1. Should the Repository be a singleton? - This will allow me to keep the sessions opened, but at the same time, I think it's going to keep connections opened to the database. For NHibernate, the ORM can only gurantee an objet is the same within the same session. This is ideal for easy coding, but there are definatly ways to overcome this using keys and overriding the GetHashCode and Equals methods.

  2. If it's not a singleton (or even if it is), should I be closing the connections as soon as they are used? For NHibernate, that means closing the session each time the Repository is "Disposed", which is after every use.

  3. Have you implemented a Repository pattern for either NHibernate or EF 4.0 and found any useful ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦忆晨望 2024-10-27 08:31:07

不要自己编写单例创建的代码(即单例模式本身),使用像 StructureMap 这样的 IOC 框架来处理对象的生命周期管理。

  1. 这个我们无法回答。如果它是单例的,那么它在内部管理的资源方面必须是线程安全的(例如数据库实时连接的连接池)。线程安全代码并非微不足道。

  2. 这个我们无法回答。这取决于你如何对待你的模型。它还取决于您是否希望人们能够通过 DataReader 进行读取,这需要与数据库的活动连接。这也会影响诸如延迟加载之类的事情,它需要活动会话,这对于数据绑定来说是一场噩梦。

  3. 以下是我为 NH 创建存储库模式所想到的一切:创建一个通用的、可扩展的 NHiberate 存储库版本 2

Don't code the creation of singletons yourself (ie the singleton pattern itself), use an IOC framework like StructureMap to handle the Lifecycle management of objects.

  1. This we can't answer. If it's singleton it must be thread safe in regards to the resources it manages internally (like a connection pool of DB live connections). Threadsafe code isn't trivial.

  2. This we can't answer. It depends on how you act with your model. It also depends whether you want people to be able to read through a DataReader which requires an active connection to the database. This also affects things like lazy loading which requires active sessions which becomes a nightmare with databinding.

  3. Here's everything I've come up with in regards to creating a repository pattern for NH: Creating a common generic and extensible NHiberate Repository version 2

雪花飘飘的天空 2024-10-27 08:31:07

第一个问题,它必须必须是NHibernate吗?为什么不看看将 EF4 与 IoC 结合使用,我最喜欢的是 StructureMap,那么你就不用再担心了关于使存储库成为单例,StructureMap 提供了通过请求、HttpContext、Hybrid 保持范围打开的选项。当然,您可以选择在存储库中使用单例模式,但在这种情况下,我只是不相信它是一个可行的选择。

希望这对您有帮助,而不是让您感到困惑。

First question, does it have to be NHibernate? Why not take a look at using EF4 with an IoC, my favorite is StructureMap, then you non longer have to worry about making your repository singleton as StructureMap gives options to keep the scope open by request, by HttpContext, by Hybrid. You of course have the option to use the Singleton pattern with your Repository I'm just not sold on it being a viable option in a case like this.

Hope that helps more than it confuses you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文