I have been using Castle Windsor and NHibernate for some months now, so I am a bit new and still learning. But then again, it is learning developers like me, that asks all the questions.
This post is also written because of a simple question: Does the HybridSessionBuilder support multiple configurations?
Why this question? Well, I have a project where some repositories uses one database and some other uses another database.
My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:
The original implementation used a static session factory and session, so this is does not support multiple configurations.
I then found out that the CodeCampServer also was using the HSB. Hoping to find an implementation that supported my request, I then started to read the source code.
But by viewing the source code for CodeCampServer I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa
Furthermore I found out that the HSB now is a part of a collection of libraries called Tarantino.
I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.
Test web application
nhibernate.cfg.xml used this connection string:
Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true
nhibernate2.cfg.xml used this connection string:
Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true
My windsor.config.xml looks like this:
It basically just configures the two repositories with each own configuration.
But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:
Only the default NHibernate configuration file was used.
Bug or feature?
That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.
I was expecting an object structure like this:
But I got something like this:
The problem I identified is line #5. It returns the existing session based on the default configuration, no matter what you specify in the configurationFile parameter.
I changed it to:
The second repository now uses the second configuration file
You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.
First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.
Second of all, as NHibernate and Castle Windsor is still a bit new to me, one of the best ways to learn its capabilities and how to use it, is to dig in deep. And that is exactly what I have done.
And BTW – I have not just learned about HSB, but also CodeCampServer, Tarantino, S#arp and how it handles multiple configurations, and how it all works together.