C# NHibernate架构,三层应用
我需要一些关于如何解耦表示层中的 nHibernate 依赖关系的建议。目前,我们有一个三层 C# winforms 应用程序,由(简化的)以下层组成;
- 用户界面 (UI)
- 业务逻辑 (BAL)
- 数据访问逻辑 (DAL)
我们正在将此应用程序迁移到 ORM (nHibernate),并且理想情况下希望只有 DAL 引用 nHibernate。我们还希望利用 nHibernate 中包含的“工作单元”功能,采用“每个会话会话”方法。
为了实现这一点,我们需要在 UI 中创建并打开一个会话,将会话通过 BAL 传递到 DAL,但是如果不在 BAL 和 DAL 中创建对 nHibernate 的依赖关系,我们就无法实现这一目标。
任何建议将不胜感激。我们应该如何构建架构以避免在 UI 和 BAL 中引用 nHibernate。有什么想法吗?
我还必须补充一点,我们也不希望 UI 引用 DAL。
用户界面 =>巴尔=>达尔
I need some advice how we can decouple nHibernate dependencies in the presentation layer. Currently we have a three tier C# winforms application consisting (simplified) of the following layers;
- User Interface (UI)
- Business Logic (BAL)
- Data Access Logic (DAL)
We are migrating this application to an ORM (nHibernate) and would ideally like to have only the DAL referencing nHibernate. We also want to employ the "Unit of Work" functionality which is included in nHibernate, adopting a "Session per conversation" methodology.
To achieve this we need to create and open a session in the UI, pass the session through the BAL to the DAL however we cannot achieve this without creating a dependency to nHibernate in both the BAL and DAL.
Any advice would be appreciated. How should we structure the architecture to avoid any references to nHibernate in the UI and BAL. Any ideas?
I must also add that we do not want the UI to have a reference to the DAL either.
UI => BAL => DAL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不可能那样做,因为 UnitOfWork 模式是由 NHibernate 的 Session 对象实现的。
然而,您只想从 DAL 引用 NHibernate,这是非常无用的,因为您的 DAL 对应用程序的上下文一无所知,而这个上下文对于使用 UnitOfWork 是必需的。
Impossible to do it that way, since the UnitOfWork pattern is implemented by NHibernate's Session object.
However, you only want to reference NHibernate from your DAL, which is quite useless, since your DAL doesn't know anything about the application's context, and this context is necessary in order to use the UnitOfWork.
看一下 NHibernate 3.0 Cookbook 我发现它对于获取掌握 NHibernate。
您将需要提取实体并创建 POCO(普通旧 CLR 对象)。您的 UI 不需要任何 NHibernate 知识。您将在数据层中创建方法来操作数据。
Take a look at the NHibernate 3.0 Cookbook I found it quite useful for getting to grips with NHibernate.
You will need to extract your entities and create POCOs (Plain Old CLR Objects). Your UI will not require any knowledge of NHibernate. You will create methods in your Data Layer to manipulate your data.
在单独的类库中配置 IoC 容器,例如使用此处描述的“GuyWire”模式:
http://nhforge.org/blogs/nhibernate/archive/2009/11/07/nhibernate-and-wpf-the-guywire.aspx
Configure your IoC container in a separate class library, for example by using the "GuyWire" pattern described here:
http://nhforge.org/blogs/nhibernate/archive/2009/11/07/nhibernate-and-wpf-the-guywire.aspx
我最近在 asp.net mvc 应用程序中使用 nhibernate 构建了一个解耦架构的示例。它使用存储库模式和单独的工作单元。大多数这些概念也应该可以在胖客户端中重用。这是我博客上的搜索链接,其中包含可能有趣的帖子。
http://blog.bobcravens.com/?s=Nhibernate
希望这可以帮助您入门。如果您有疑问,请告诉我。
鲍勃
I recently built an example of a decoupled architecture using nhibernate in an asp.net mvc application. It uses a repository pattern and a separate unit of work. Most of these concepts should be reusable in a thick client also. Here is a search link on my blog with the posts that may be interesting.
http://blog.bobcravens.com/?s=Nhibernate
Hope this gets you started. Let me know if you have questions.
Bob