2
votes

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

4
What you are asking for doesn't really make sense (although I do understand what you are trying to accomplish as I've been there). It may be doable, but it will potentially be difficult to understand the implementation details and it provides very little value in return. My recommendation: Allow your UI layer to know about NHibernate, but minimize the usage of NHibernate in that layer. - Michael Maddox

4 Answers

2
votes

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.

1
votes

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.

1
votes

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

0
votes

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