Book Review: NHibernate IN ACTION

NHibernate IN ACTION


Pierre Henri Kuate, Christian Bauer, Gavin King, Tobin Harris

My Rating

star star star star

As part of my day job, I use Castle Active Record, which builds upon and abstracts away some features of NHibernate. Everything I had learned had been by asking colleagues and generally toying with settings. This left me with a lot of intrigue, and a tool I use as prominently as NHibernate needs to be better understood.

Researching NHibernate was not an option. The of book, however, was. It was either this one, which covers NHibernate 1.2, or the new cook book, which covers NHibernate 3.0.

My decision to choose this book was that it appeared to delve deeper into the workings of NHibernate and provide insights into the whole notion of ORM solutions. And after all, I love reading books - I can read the cook book next.

Another benefit of this book, is that by understanding the fundamentals of ORM, my powers of comparison with other ORM’s, e.g. Entity Framework (code only), could be strengthened.

Chapters in the book

NHibernate IN ACTION comes in three sections:

1.       Discovering ORM with NHibernate
2.       NHibernate Deep Dive
3.       NHibernate in the Real World

Spanning all three sections is an example application used to demonstrate the features of NHibernate. This application is called CaveatEmptor – it is basically an ebay replica.

Discovering ORM with NHibernate

In this short, two-chapter, initial section of the book, the fundamentals of ORM (object relational mapping) are explained. Central to these fundamentals is the theme of the object/relational impedance mismatch. This explains the problem that ORM tools aim to solve – converting database records into instances of object-oriented classes.

During this initial section of the book, simplicity is maintained when explaining all the concepts. In essence, the groundwork is laid well, because all the knowledge expressed here is essential to understanding everything else in the rest of the book.

Another feature of the opening section is the recommendation of layered architectures and relevant design patterns (e.g. unit of work) – with the recommendations to use them only when necessary.

Chapter two completes this section with a simple “Hello World” application, showing the absolute basics that are required to get cracking with NHibernate.

NHibernate Deep Dive

As the heading suggests, this section of the book is the most complex and is the most significant in terms of quantity.

For me, the most important chapter were 4, 5 and 6.

Chapter 4 talks about the concept of sessions in NHibernate. It goes deep into the inner-workings and explains well their importance. With this education in sessions, it then enables the authors to talk about entity lifecycles – the difference between “transient” and “persistent” which is an essential concept when working with NHibernate.

In chapter 4, the cascade is also introduced. This is a feature I had been looking to learn more about. Essentially, it involves an owning object taking responsibility for the life cycle of a child object. To understand this, the CaveatEmptor example uses the entities Item and Bid. With different cascade settings, an Item can have varying control over a Bid up to the point where if the Item is deleted, then all its Bids will automatically be deleted.

I said chapter 5 was also important, and that is because it introduces transactions. Again the level of details is very good, going in as much details as to explain the ACID acronym for database transactions. This relates to database transactions in general, and not just NHibernate.

Chapter 5 also has the responsibility of explaining the two-level caching system employed by NHibernate. While the first-level is easy-to-understand and managed by NHibernate, the second-level cache is a complex topic. A number of options are explored, with a balanced view provided. I hope I never have to worry about the second-level cache!

Finally, the last important chapter is chapter 6 – the focus again being on mapping, but this time in more depth. What makes it important is firstly that mapping is the main area of NHibernate you control and thus need to understand it well.

Secondly, there is a multitude of mapping options, including a number of alternatives for achieving the same end results – this relates mainly to collections and the different types: bags, lists and maps; distinct by their ability to hold duplicates and be ordered.

Thirdly, you get taught about the SQL generated by each approach and what consequences it will have on your application. You are also shown how to fine-tune the generation of SQL if you have a specific need to.

In the other chapters of this section you will meet the options for querying in NHibernate:

  •     HQL – SQL’ish
  •     Criteria-based – using objects
  •     Raw SQL – as it says on the tin

The pro’s and con’s of each querying mechanism are explained along with scenarios in which each might be applicable.

NHibernate in the Real World

Concluding the book are a few chapters on how to implement NHibernate in your enterprise applications. The general theme is about how to use good coding practices like separation of concerns via layered architechture, Domain Drive Design, and the repository pattern to keep your code flexible and maintainable.

In this section of the book, amongst a few others, is a section on how to create an almost full ORM solution when you have only the POCO’s, mappings or a pre-existing database.

Also along the way in the final few chapters, you will meet a few best practices and what I call “life-savers”: find out about them before they kill you.

If you’re a developer responsible for major architectural decisions or designing the data-persistence strategy then this is not optional reading. But we’re coders, we love our job and we love analysing the tools we use. You should be reading this anyway.


As per my initial hopes, I now understand better both the general concept of ORM and more specifically the abstractions Castle Active Record provides over NHibernate.

I am also now able to purchase the NHibernate cookbook that covers the latest version (3) and learn the latest features to build upon the knowledge gained from this book.

It is hard to recommend this book, without knowing how much is out of date and how much of the fundamentals are omitted from the NHibernate 3.0 Cookbook. I’m expecting that to understand the fundamentals of ORM and design rationales of NHibernate, you will need to read NHibernate IN ACTION.

Finally, I’ll finish by saying that I always rate a book in terms of readability, grammar and punctuation. Despite the fact this book is co-authored by four people it performs excellently well in these respects and in terms of consistency.

You may also like...

Pro ASP.NET MVC 2 Framework

Pro C# 2008 and the .NET 3.5 Platform

Beginning ASP.NET 3.5 in C# and VB

My Books

Designing Autonomous Teams and Services
Patterns, Principles and Practices of Domain-Driven Design
The Strategic Practices of Domain-Driven Design