C# NHibernate architecture, three tier application
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开发者_StackOverflow社区 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
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.
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.
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
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
精彩评论