1

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.

Mitch A
  • 2,050
  • 1
  • 21
  • 41

2 Answers2

1

I think the following assumption you're making is not correct:

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.

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 the CurrentCustomer 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 the SessionFactory 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 a SessionFactory of any kind.

Marijn
  • 10,367
  • 5
  • 59
  • 80
0

frankly I would not couple NHibernate and ASP.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.

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • I agree completely. However, my question was whether this de-coupling can be done without the loss of NHibernate features. The loss of "Open Session In View" appears to be the main casualty. Is this correct? – Mitch A Nov 15 '11 at 22:09