Dec 01, 2008 / nhibernate ~ .net
Short n' Sweet NHibernate Sample

People often have the perception that NHibernate is some honking great behemoth of a library that should only used in massive enterprise apps.

To prove this isn't true, it I've made a tiny four-file NHibernate sample with just over 100 lines of code.

Download .NET Visual Studio Solution (includes libs).

You don't see many of these tiny NHibernate samples out there on the web, so hopefully this might help someone dip their toes into NHibernate without having to wade through architecture or infrastructure. You can just open the solution and have a peek at the few files in there.

The files in the solution are:

Classes.cs
    Contains a couple of entities that you can save and load

PersistentTestFixture.cs   
    NUnit test fixture demonstrating create, save and query.

hibernate.cfg.xml                
    A minimal NHibernate config file.

mappings.hbm.xml
    The XML to map the entities to the database

Also note:

  • This requires virtually zero setup. The test fixture will even create a 'ClubTest' database on your local SQL Server if there isn't one there already (and create tables too).
  • The mapping files are simple and very minimal, so they rely on the NHibernate sensible defaults.

Bear in mind that this is a very simple way of seeing some NHibernate code. It's not an industrial strength scalable code base! If you want to see recommended real-world samples then I'd point you at the Cuyahoga CMS, Sharp Architecture, nhforge.org or Summer of NHibernate (to name a few).


You may also like...