In this post I’m going to explain how I managed to make these projects to work together using NHibernate events instead of the classic interceptors. Through these events, we tell NHibernate.Search to index the elements marked for that purpose without touching the session. What we need to perform a search in the index is the id and we get it from NHibernate.Burrow.
The first thing we need is to configure the application properly (App.config
):
NHibernate.Search does not manage the configuration section properly, so it looks for a section called nhs-configuration
, so don’t change the name. On the other side, we should provide a valid hibernate.cfg.xml
with NHibernate configuration. For example, for testing you can use an in-memory SQLite database:
Don’t forget to add all the assemblies in the mapping-assembly
sections.
After that, the most interesting part is Burrow session initialization in order to add reference to the events. For example, this is what I have at the beginning of each test case that hits the database:
The last line is important because it updates the session with the changes made. Ok, this may not be the best approach and adding the events in the nhibernate.cfg.xml would be better. It’s up to you. It should be something like this (the code goes into the session-factory section):
With this code you don’t need to rebuild the session factory.
With this configuration, the indexable entities -see Dario Quintana’s post on NHibernate.Search- are managed automatically by the library without doing anything manually and in a transparent manner.
Finally, you only need to test the search over the indexes. In the simple test below, the code creates a NHibernate.Search session and makes a search over an indexed entity, following Dario’s example:
You must take in account on important thing: NHibernate.Search uses the current transaction if there’s one and NHibernate.Burrow makes use of them, so if you want to persist the indexes before making the search, it’s not enough evicting the entity. You should do the following:
With this piece of code we finish Burrow’s pending transaction and create a new one in order to persist all the index changes.
And this is all. With this samples you can integrate in a simple manner two interesting projects on top of NHibernate, that can be a bit tricky if you need to deal directly with the session and are using Burrow. As well, using the new event system introduced in the 2.0 branch of NHibernate avoids the use of interceptors that in many cases was painful.
Note that both projects were compiled from the trunk of nhcontrib against NHibernate 2.1.0GA.
I’ll be waiting for your comments.