Hello NHibernate! Quickstart with NHibernate (Part 1)

When I first learned NHibernate, I could best describe the experience as less of a learning curve and more like a learning cliff.  A large part of that was the availability of tutorials. 

In this first of a series of articles, I will be taking a crack at providing people new to NHibernate the information they need to quickly ramp up with NHibernate.

For the first article, I've decided to address the gap of just giving folks enough code to get started.  No UI, no fluff - just enough to connect to a database and do some basic CRUD operations.  In future articles, I will discuss a repository pattern for NHibernate, parent-child relationships, and other more advanced topics.

You can find the entire article via this Google Docs link:

http://docs.google.com/Doc?docid=0AUP-rKyyUMKhZGczejdxeHZfOGMydHNqdGc0&hl=en

Enjoy!
-Bob

Print | posted on Sunday, April 11, 2010 2:46 PM

Comments on this post

# re: Hello NHibernate! Quickstart with NHibernate (Part 1)

Requesting Gravatar...
"The only concession we need to make for NHibernate is that any property we wish to persist must be virtual." is not true.

Any POCO enabled to lazy load must have ALL public methods (including automatic getters and setters) virtual.

Well done, good intro.
Left by Andrea Balducci on Apr 11, 2010 6:47 PM

# re: Hello NHibernate! Quickstart with NHibernate (Part 1)

Requesting Gravatar...
Thank you very much for the feedback! I've modified the article to clarify the distinction on non-virtual properties and lazy loading:

"In the case of NHibernate, no special decoration is required to allow our objects to interact with our persistence layer. The only concession we need to make for NHibernate is that all properties we wish to persist must be virtual, and if lazy loading is enabled, all public methods in our class must be virtual even if they are not included in our mapping file.

As a general rule of thumb, it's best to make all of your properties virtual, this way your bases are covered whether you use lazy loading or not."

Left by Bob Palmer on Apr 11, 2010 10:35 PM

# re: Hello NHibernate! Quickstart with NHibernate (Part 1)

Requesting Gravatar...
Good intro. I might suggest, though, that for beginners it might help to include the code for the namespaces to reduce frustration when creating a project based on your instructions:
---
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
---
Left by Maik Both on Apr 26, 2010 9:20 AM

# re: Hello NHibernate! Quickstart with NHibernate (Part 1)

Requesting Gravatar...
Great point! I actually had one of my team members comment on that as they were trying it out (they were new to NHibernate so they made for an excellent guinea pig)

I'll update the docs tonight to reflect the namespaces

Thanks for the feedback!
-BOb
Left by Bob Palmer on Apr 26, 2010 10:38 AM

# re: Hello NHibernate! Quickstart with NHibernate (Part 1)

Requesting Gravatar...
i'm need are public calculated non virtual property in my class.
how i can do that?
Left by Alex on Apr 19, 2011 4:51 AM

Your comment:

 (will show your gravatar)