I've already developed some projects with NHibernate and I can say that NHibernate is a real time saver. But I must admit that it takes some time to get used to NHibernate. As an ORM NHibernate has fantastic features but if you want to develop successfull projects with NHibernate you must know that NHibernate is only one part (ORM) of a successfull layered architecture. In the attached solution I tried to give you the basic idea of a layered architecture and how NHibernate can be used within such an architecture

Attached solution includes the following projects

  • Core: Our domain objects (entities) reside in this layer. Also DAO (Data Access Objects) interfaces are defined in this layer.
  • DAL (Data Access Layer): Default implementation of DAO interfaces reside in this layer.
  • BLL (Business Logic Layer): Business logic code resides in this layer.
  • Common: Some utility calsses reside in this project
  • Tests.Disconnected: Includes unit tests for domain objects and business logic. Notice that unit tests in this project do not require domain objects to be persisted (NHibernate behaviour will not be tested), so the need for a database connection is eliminated which in turn makes our unit tests fast. Notice how we replaced our default DAL with mocked one.
  • Tests.Connected: This project includes unit tests too, but this time we want to test how our domain objects and bussiness logic perform NHibernate. NHibernate provides very cool features like native sql, lazy loading and cascading and we will likely want to test how our domain objects behave when armored with these cool features of NHibernate. It is also very likely that we will have some mapping errors (typos likely) in our Core, these tests will help us catch these errors. Performance bottlenecks possibly caused by our NHibernate mappings (for example we may discover that we need to make a child collection to be lazy loaded) can also be identified with help of these tests.

In the sample solution you can also find a simple usage of Castle Windsor Inversion of Control (IoC) container. We use IoC to be able to load different implementations of our DAL (DAO implementations). 

Another very important point you need to understand really well is session management of NHibernate when used in web applications. Thanks to Burrow contribution project this task is made very simple, you do not even need to write single line of NHibernate session management code. You only need to inherit your DAO implementation classes from GenericDao<T> class and you are ready to go. 

Additional Notes

  • Database script is included in Tests.Connected project under DBScript folder. 
  • Sample project uses NHibernate  2.0 Aplha1 and NHibernate.Burrow is also Alpha1.
  • ASP .NET MVC Preview 3 to run Sales.MVC sample
  • TestDriven .NET to run NUnt tests

Suggested Readings

Downloads

Update History

  • 09 June 2008
    • Castle files under Libs folder updated to Castle RC3
    • SQLite references removed
    • TestFixtureTearDown override of CustomTestBase class in Sales.Tests.Connected assembly commented out

Posted in: NHibernate  Tags:

Comments


 Scott
June 8. 2008 00:56
Scott
Thank you so much for this - I've been a little nervous this last about the correct way of managing the NHibernate session in ASP.NET MVC - and your example has pointed me in the right direction.

   no site


June 9. 2008 08:51
Ali Özgür
You are welcome Scott.
Latest NHibernate (1.2) includes some built-in support for session management too. I recommend you to check out the "2.3. Contextual Sessions" section found in documentation from NHibernate site.

   http://www.pragmasql.com/


People's Republic of China Leo Xue
August 22. 2008 03:32
Leo Xue
Thank you for your sample.
I open the solution in VSTS 2008. All the projects are loaded successfully except Sales.Mvc. I am not sure what's the problem.

How could I do to correct it?

Thanks again.

   no site


August 22. 2008 09:13
aliozgur
Leo you must download and install ASP.NET MVC Preview. You can find the link in ths post.

   http://blog.pragmasql.com/


People's Republic of China Leo Xue
August 22. 2008 13:50
Leo Xue
thank you for response so quickly.

I have downloaded the preview 4 and compiled the solution. Everything is OK.

Great job!

   no site


People's Republic of China Leo Xue
August 22. 2008 16:02
Leo Xue
One more question. What's the version of your visual studio? I use VS 2008 to run your sample and get an error as : Could not load file or assembly 'System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.

   no site


August 22. 2008 16:37
aliozgur
I use VS 2008.
NHibernate.Burrow.WebUtil.dll depends on this assembly. Possible solutions are

1- Download and install ASP.NET 2.0 AJAX Extensions suit which is freely provided by Microsoft

2- Download NHibernate.Burrow source code and open with VS 2008. Remove System.Web.Extensions ( version 1.0.61025.0) reference from NHibernate.Burrow.WebUtil project and add reference to the latest System.Web.Extensions.dll (Version 3.5.0.0, this comes ready with .NET 3.5 while 1.0.61025.0 version was installed as a seperate extension) and build it with VS 2008. Then copy paste all assemblies to Nh.Burrow folder which is under Libs folder.

   http://blog.pragmasql.com/


People's Republic of China Leo Xue
August 23. 2008 08:57
Leo Xue
Done.
Thank you again!

   no site


United States Joao Araujo
September 20. 2008 02:08
Joao Araujo
DId anyone tried VS 2008 and IIS7?  

I tried many options and versions, still my connections do not happen.
It is interesting but the only problem with connections is when I use burrow.
Any sample that I have using burrow can not connect to the database.
does anyone know what the reason?

Any hint ?


BTW, even when I use debug I still can not connect . I get the following error after pressing the button on the first page.
"A network-related or instance-specific error occurred while establishing a connection to SQL Server ...."
"\DesenvTools\NHibernate\NHibernate 2.0\NHibernate.Burrow-1.0.0.CR1-src\src\NHibernate.Burrow\Impl\TransactionImpl.cs    Line:  25
......



Thanks for helping,

Joao,


   no site


September 20. 2008 21:43
aliozgur

   http://blog.pragmasql.com/


December 30. 2008 02:09
Speed Dating
I am pretty new to web development. I have heard some good things about NHibernate. I think your project looks pretty awesome - I hope to be able to get to the level you are at now.

   http://www.nyminutedating.com/


United States Nima
January 4. 2009 07:43
Nima
Can use burrow long conversation in MVC ?
Thanks

   no site


January 6. 2009 11:53
aliozgur
Thanks for your comment.
I'm not sure. You can check if long conversations work by modifying the sample project.

   http://blog.pragmasql.com/

Comments are closed