Review: NHibernate 3.0 Cookbook

A few weeks ago we got a copy of the NHibernate 3.0 Cookbook by Jason Dentler, which I was really looking forward to because of the scattered nature the existing documentation for NHibernate.

The word Cookbook really says a lot of how the book is structured, and how it should be used. The book consists of a bunch of recipes on how to cook with NHibernate – terrible wordplay, I know, and I am sorry 🙂 So basically it can be a good idea with a quick readthrough, and then it will serve as a place to look up solutions to various scenarios you will be dealing with when using NHibernate.

If you are new to NHibernate you will of course also gain an overview of the different topics you need to know about and as an experienced user you might stumble upon some features you didn’t know about.

Basic modelling

Some of the first chapters of the book is around some of the modelling choices that you have when building your model, and how these can be mapped. In these chapters Persistent Object Identifiers (POID’s), inheritence, relationships, collectiontypes and versioning are covered.

There are some good takeaways around the different types of POID’s, and which drawbacks it has to use id’s generated by the database – as most prople probably do. Overall this is one of the things that are pretty well covered.

The part about inheritence gives you what you need, but for some reason I didn’t fint it to be the most clear chapter to read. Cascading updates/deletes are not really covered in detail, so this is for me one of the parts in the book that could use some more work.

Keeping in mind that it is a cookbook it does an ok job of giving you the short version of optimistic/pessimistic concurrency and versioning. On the upside it is very direct and to the point, but again it is not as thorough as it could be.

Configuration of mappings

The following chapters are around different ways to configure mappings, where the default XML configurations are quickly illustrated and there are samples on working with Fluent NHibernate and ConfORM.

This in my opinion should be one of the most extensive parts of the book. I think so simply because it is very important to know the choices you have and the impact it will have on your work with NHibernate afterwards. Because of this and even keeping the “Cookbook theme” in mind, I think this part of the book is too slim.

The good thing is that I actually didn’t know ConfORM existed before reading it. On the flipside I have no clue why it might be a better choise than Fluent NHibernate after reading it.

One point that often comes up when people first start off with NHibernate is what an inverse one-to-many relationship is and what you accomplish by marking a relationship as inverse. This is one of the chapters that shine because it gives you a good sample on how having an inverse relationship can influence if NHibernate ends up sending one or two update-statements to the database.

Strategies – how to deal with the session

Another important aspect of working with NHibernate, and one that is important for your overall design and the amount of pain you will be submitted to is your strategy for handeling the session.

The book gives good examples on Session per Request, Presenter pattern, Session Action Attribute and Conversation per Business Transaction. So this actually gives you some good choices depending on if you are building an MVC, WinForm, WebForm or whatever application. As a guy who loves his MVC, there is a bonus which is very interesting link on working with MVC viewmodels.

Besides that general strategy you choose for the application there is also the option of using StatelessSessionProvider in some special cases. StatelessSessionProvider is shortly put NHibernate without first-level cache, which in scenarios like bulk inserts can be advantageous, since NHibernates handeling of updates on changed objects might get in the way.

Querying – bread and butter of any ORM

At the end of the day what makes or breakes an ORM is probably which options you have to query data. The book covers all the basic API’s for querying with NHibernate.

It covers using all the basic API’s like HQL, the Criteria API, Named Queries, QueryOver and LINQ with short samples, that should still be enough to get you started. There is also included a link to the hql addin that can give you intelllisence as all we .NET developers love. Building on that it covers MultiCriteria, MultiQuery and Futures, and shows how these API’s can be used to reduce the number of calles to the database.

Another key scenario that most developers run into fairly often is that they need to build a query based on user input, which they wish to do in some part of the application where they don’t want to deal with the session. This is where Detached Queries come in, because they give you just that option. This is also a topic where the Cookbook format wins out, because it gives you what you need while being short and to the point.

One of the things I was happy to see was well covered, and where I took something away from reading it was around testing. The book shows how to use SQLite if you wish to have tests run against a database that you restore to some known state.

Another important thing that I have seen go wrong a few times, and where I have also learned my lesson is when mapping nullables and enums. When doing this you risk running into a problem if NHibernate does some mapping reading from the database. The problem is that NHibernate might not know how to compare with the data in the database, which in turn makes it issue unnecessary update statements. To test for this Jason covers covers what he calles the “Ghost buster test”, which can very well save you from an unforseen painful scenario.

DDD-style repositories

To round off the stuff you will need to know about to be able to build a real life application the book covers DDD-style repositories. The recipe includes shows a nice way to build a repository that itself is a factory for query-objects. The idea is to let you do queries this way:

var query = bookRepository.CreateQuery<IBookWithISBN>();
query.ISBN = “123”;
var result = query.Execute();

The downside to this pattern is also covered, which is that you will not have the option of refreshing or doing merges to or from the database, without introducing a leaky abstraction or at least breaking the repository abstraction.

Miscellaneous a.k.a “all the other stuff”

The last chapters of the book covers some more advanced scenarios and some of the third-party stuff you can find around the internet.

The last chapters are actually not my favorite part of the book to be honest. I like that it gives some inspiration where to find addins and contribution projects, but some of the stuff covered is just not something most developers will need. Keeping in line with the format of a Cookbook, I would have prefered these chapters to be reduced, and more focus being on some of the stuff that I mentioned earlier I feel is missing.

That being said we all probably will need  to work with concepts like well known instance types, auditting and validation – so these recipes are relevant and make sence to have in the book. The parts I am skeptical about are dealing with spatial data and doing sharding. To me they just stick out like a sore thumb, because these are not subjects most developers will need. Even if spatial data or sharding is something you need, you won’t really feel helped by the one small chapter the book gives you on each topic.

Conclusion

As you can probably guess from my review I think it is a book that will give you lots of value, and that covers important aspects of working with NHibernate. This is something that I have heard from a fair amount of developers has been missing, to help people get off the ground with NHibernate.  It is a big plus that it is a quick readthrough so you get the overview that will allow you to use it for lookups.

The idea of writing it as a cookbook with a lot of recipies really works well most of the time. There are some areas that I think could use some more lovin’ – as well as others where the thought “kill your darlings” could have beneficial to the writer.

All in all I’ll give it a big 4 out of 6 stars/hats/chairs or what ever the popular scale is these days.

Christian Holm Nielsen
Vertica A/S

Kategorier: Udvikling

Tagged as: ,

1 kommentar »

  1. Now, there are different factors involved in this process, which determines the friendliness of a website. In order to gain online visibility it is recommended to use a good SEO services provider. Every website on the internet is aiming for long-term promotion and visibility on search results, and hiring a competent SEO consultant which uses white-hat techniques can certainly help in achieving that goal.

Skriv et svar

Udfyld dine oplysninger nedenfor eller klik på et ikon for at logge ind:

WordPress.com Logo

Du kommenterer med din WordPress.com konto. Log Out / Skift )

Twitter picture

Du kommenterer med din Twitter konto. Log Out / Skift )

Facebook photo

Du kommenterer med din Facebook konto. Log Out / Skift )

Google+ photo

Du kommenterer med din Google+ konto. Log Out / Skift )

Connecting to %s