ASP.NET MVP 与 NHibernate 和 Spring.NET。 OSIV有必要吗?
我正在尝试将 NHibernate 集成到现有的 ASP.NET 项目中,该项目已经使用 Spring.NET 和 MVP 模式来实现关注点的清晰分离。换句话说,在我的任何演示者/视图中都没有数据访问代码或数据访问代码,我希望保持这种状态。
然而,Spring.NET 源附带的 Spring.Northwind.Web 示例让每个 Web 控制器(即 Presenter)将 NHibernate ISessionFactory 对象作为构造函数依赖项。根据我有限的阅读量,如果想使用 OSIV,这种耦合似乎是必要的。假设我不想使用 OSIV(仍在阅读此内容),我可以安全地从表示层中删除 ISessionFactory 依赖项吗?如果更有经验的 NHibernate 能够参与讨论不使用 OSIV 或任何其他与此堆栈相关的问题的优缺点和后果,我将非常感激。
非常感谢。
I'm trying to integrate NHibernate into an existing ASP.NET project which is already using Spring.NET and the MVP pattern to achieve a clean separation of concerns. In other words, there is no data access code, or awareness thereof, in any of my Presenters/Views and I'd like to keep it that way.
However, the Spring.Northwind.Web example that ships with the Spring.NET source has each Web Controller (i.e. Presenter) taking an NHibernate ISessionFactory object as a constructor dependency. Based on my limited amount of reading, it looks like this coupling is necessary if one wants to use OSIV. Assuming I don't want to OSIV (still reading up on this), can I safely remove the ISessionFactory dependency from my Presentation tier? I would really appreciate it if more experienced NHibernaters could chime in with pros/cons and consequnces of not using OSIV or any other gotchas with this stack.
Many Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您做出的以下假设是不正确的:
示例项目中有一个“控制器”:
NHibernateCustomerEditController
。这个 确实注入了会话工厂。但是,这不是您所假设的“演示者”意义上的控制器:它是一个用于管理用户会话中的CurrentCustomer
的实用程序类 - 它演示了会话范围对象的使用。这个例子没有使用 MVP 模式;对服务和存储库 (DAO) 的依赖项直接注入到页面上。
Open session in view (OSIV) 是作为一个 http 模块实现的,因此它不需要在每个页面(或页面演示器,如果您应用 MVP)上注入会话工厂 - “它就在 http 上下文中” 。
在您的情况下(我假设)服务被注入到您的演示者中;这些服务可能依赖于需要
SessionFactory
的 DAO。 没有要求在每个演示者或页面上注入SessionFactory
:您可以继续“正常”di 配置,无论是否应用 OSIV。您的表示层不需要依赖于任何类型的SessionFactory
。I think the following assumption you're making is not correct:
There is one "controller" in the example project: the
NHibernateCustomerEditController
. And this indeed is injected with a session factory. However, this isn't a controller in the "presenter" sense you're assuming: it's a utility class to manage theCurrentCustomer
in the user's session - it demonstrates the use of session scoped objects.This example doesn't use the MVP pattern; dependencies on services and repositories (DAO's) are injected directly onto the page.
Open session in view (OSIV) is implemented as an http module and as such it does not require the session factory being injected on each and every page (or page presenter, if you apply MVP) - "it's just there in the http context".
In your situation (I assume) services are injected onto your presenter; these services might depend on DAO's that require a
SessionFactory
. There is no requirement to inject theSessionFactory
on each and every presenter or page: you can proceed with your "normal" di config, with or without applying OSIV. Your presentation layer does not need to depend on aSessionFactory
of any kind.坦率地说,我不会以这种方式结合
NHibernate
和ASP.NET MVP
,前者是一个 ORM,后者是一个表示层框架,不应绑定和依赖于任何特定的数据访问技术。在这里阅读我的答案: MVC3 和实体框架 作为一般规则,这是有效的任何 UI 技术或 ORM / DAL 技术,其想法都是以 UI(MVP、MVC、WebForms、WPF、Jquery、Windows Forms、 SL...) 不依赖和了解特定的数据访问和数据库引擎细节。
frankly I would not couple
NHibernate
andASP.NET MVP
in that way, the former is an ORM and the second is a Presentation Layer framework which should not be bound and dependent to any specific data access technology.read my answer here: MVC3 and Entity Framework this is valid as general rule with any UI technology or ORM / DAL technology, the idea is to layer things in a way that UI (MVP, MVC, WebForms, WPF, Jquery, Windows Forms, SL...) does not depend and know about the specific data access and database engine details.