<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog of developer Mikkel Ovesen &#187; nhibernate</title>
	<atom:link href="http://blog.ovesens.net/tag/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ovesens.net</link>
	<description>My thoughts, stuff I need to remember or things I just want to share with the world</description>
	<lastBuildDate>Thu, 27 May 2010 12:02:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Must read and references</title>
		<link>http://blog.ovesens.net/2010/01/must-read-and-references/</link>
		<comments>http://blog.ovesens.net/2010/01/must-read-and-references/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 13:56:11 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[read]]></category>
		<category><![CDATA[reference]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/2010/01/must-read-and-references/</guid>
		<description><![CDATA[Read:
http://davybrion.com/blog/2009/12/using-nhibernate-in-your-service-layer/
Reference:
http://west-wind.com/weblog/posts/132081.aspx
]]></description>
			<content:encoded><![CDATA[<p>Read:<br />
<a href="http://davybrion.com/blog/2009/12/using-nhibernate-in-your-service-layer/" target="_blank">http://davybrion.com/blog/2009/12/using-nhibernate-in-your-service-layer/</a></p>
<p>Reference:<br />
<a href="http://west-wind.com/weblog/posts/132081.aspx" target="_blank">http://west-wind.com/weblog/posts/132081.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2010/01/must-read-and-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NHibernate Fluent Criteria</title>
		<link>http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/</link>
		<comments>http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 09:08:10 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[fluent]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/</guid>
		<description><![CDATA[&#8211; Updated on the 2. January 2010
I have been using NHibernate for some time now, and I am very satisfied with the functionality it provides. However, I do really not like &#8220;magic strings&#8221; as these makes the code resistent to refactoring.
NHibernate makes use of &#8220;magic strings&#8221; when doing a query using HQL or the Criteria [...]]]></description>
			<content:encoded><![CDATA[<p><em>&#8211; Updated on the 2. January 2010</em></p>
<p>I have been using NHibernate for some time now, and I am very satisfied with the functionality it provides. However, I do really not like &#8220;magic strings&#8221; as these makes the code resistent to refactoring.</p>
<p>NHibernate makes use of &#8220;magic strings&#8221; when doing a query using HQL or the Criteria API. I tend to use the Criteria API as this, opposed to HQL, limits the logic encompassed in &#8220;magic strings&#8221;.</p>
<p>As I mostly use the Criteria API, I thought, why not do something about it <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  So I did. Very quickly a Fluent Criteria interface was implemented. Here is how it is used:</p>
<pre class="brush:csharp">var email = "email@email.com";
var customerType = CustomerType.Default;

var result = nhSession.Criteria&lt;Customer&gt;()
             .Add.Equals(x =&gt; x.Email, email)
             .Add.Equals(x =&gt; x.Type, customerType)
             .Order.Desc(x =&gt; x.Created)
             .NHCriteria
                 .SetMaxResults(1)
                 .UniqueResult&lt;Customer&gt;();</pre>
<p>In this line we call the extension method Criteria with Customer as a generic argument:</p>
<pre class="brush:csharp">var result = nhSession.Criteria&lt;Customer&gt;()</pre>
<p>By doing so we of the alternate Fluent Criteria API I have implemented. Currently it is only some of the Restrictions and Order classes that have been implemented, but you are free to use these classes and extend them as you wish.</p>
<p>By calling the NHCriteria property, you get the normal NHibernate criteria API, so here you can set the cache options, max results, etc.</p>
<p>Download the source code here <a href="http://ovesen.codeplex.com/wikipage?title=NHibernateFluentCriteria&amp;referringTitle=Home">http://ovesen.codeplex.com/wikipage?title=NHibernateFluentCriteria&amp;referringTitle=Home</a></p>
<p>Happy New Year btw <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Multiple Configuration files for NHibernate (HybridSessionBuilder)</title>
		<link>http://blog.ovesens.net/2009/01/using-multiple-configuration-files-for-nhibernate-hybridsessionbuilder/</link>
		<comments>http://blog.ovesens.net/2009/01/using-multiple-configuration-files-for-nhibernate-hybridsessionbuilder/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 23:31:55 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nhibernate]]></category>

		<guid isPermaLink="false">/post/2009/01/25/Using-Multiple-Configuration-files-for-NHibernate-(HybridSessionBuilder).aspx</guid>
		<description><![CDATA[&#160;
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 [...]]]></description>
			<content:encoded><![CDATA[<h2>&#160;</h2>
<p>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.</p>
<p>This post is also written because of a simple question: <em>Does the HybridSessionBuilder support multiple configurations</em>?</p>
<p>Why this question? Well, I have a project where some repositories uses one database and some other uses another database.</p>
<h2>Original implementation</h2>
<p>My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:</p>
<p><a title="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/" href="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/">http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/</a></p>
<p>But as you can see:<a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="283" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb.png" width="544" border="0" /></a></p>
<p>The original implementation used a static session factory and session, so this is does not support multiple configurations.</p>
<h2>CodeCampServer/Tarantino</h2>
<p>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.</p>
<p>But by viewing the source code for <a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a> I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Furthermore I found out that the HSB now is a part of a collection of libraries called <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino</a>.</p>
<p>I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.</p>
<h2>Test web application </h2>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_4.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="156" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_1.png" width="198" align="right" border="0" /></a> Then I build a small test web application project. It used Castle Windsor (for dependency injection) and NHibernate. I quickly configured Castle Windsor and made two NHibernate configuration files.</p>
<p>nhibernate.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true</em></p>
<p>nhibernate2.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true</em></p>
<p>My windsor.config.xml looks like this:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;</span><span style="color: #800000">component</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;firstRepository&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span>hibernate.cfg.xml<span style="color: #0000ff">&lt;/</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">component</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;</span><span style="color: #800000">component</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;secondRepository&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span>hibernate2.cfg.xml<span style="color: #0000ff">&lt;/</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">component</span><span style="color: #0000ff">&gt;</span></pre>
</p></div>
</div>
<p><font face="Lucida Sans Unicode"></font></p>
<p>It basically just configures the two repositories with each own configuration.</p>
<p>But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_6.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="42" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_2.png" width="534" border="0" /></a> </p>
<p>Only the default NHibernate configuration file was used.</p>
<h2>Bug or feature?</h2>
<p>That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.</p>
<p>I was expecting an object structure like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_12.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="178" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_5.png" width="534" border="0" /></a> </p>
<p>But I got something like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_10.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="185" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_4.png" width="534" border="0" /></a>&#160; I identified the following method to be the central, when the session was instantiated:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">private</span> ISession getExistingOrNewSession(ISessionFactory factory, <span style="color: #0000ff">string</span> configurationFile)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span>         {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span>             <span style="color: #0000ff">if</span> (HttpContext.Current != <span style="color: #0000ff">null</span>)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span>             {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   5:</span>                 var session = GetExistingWebSession();</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   7:</span>                 <span style="color: #0000ff">if</span> (session == <span style="color: #0000ff">null</span> || !session.IsOpen)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span>                 {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   9:</span>                     session = openSessionAndAddToContext(factory, configurationFile);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span>                 }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  11:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  12:</span>                 <span style="color: #0000ff">return</span> session;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  13:</span>             }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  14:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  15:</span>             var currentSession = _currentSessions.ContainsKey(configurationFile)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  16:</span>                                     ? _currentSessions[configurationFile]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  17:</span>                                     : <span style="color: #0000ff">null</span>;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  18:</span>             <span style="color: #0000ff">if</span> (currentSession == <span style="color: #0000ff">null</span> || !currentSession.IsOpen)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  19:</span>             {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  20:</span>                 _currentSessions[configurationFile] = OpenSession(factory);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  21:</span>             }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  22:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  23:</span>             <span style="color: #0000ff">return</span> _currentSessions[configurationFile];</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  24:</span>         }</pre>
</p></div>
</div>
<h2>Patch/solution</h2>
<p>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.</p>
<p>I changed it to:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> var session = GetExistingWebSession(configurationFile);</pre>
</p></div>
</div>
<p>&#160;</p>
<p>And tadaaa:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_14.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="42" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_6.png" width="534" border="0" /></a> </p>
<p>The second repository now uses the second configuration file <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.</p>
<p>First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.</p>
<p>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.</p>
<p>And BTW &#8211; 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.</p>
<h3>NHibernateSessionModule</h3>
<p>Please note. Before my change is used, you need to also update the NHibernateSessionModule. It does currently not dispose all the sessions that are opened.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/01/using-multiple-configuration-files-for-nhibernate-hybridsessionbuilder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple Configurations for NHibernate using HybridSessionBuilder</title>
		<link>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-using-hybridsessionbuilder/</link>
		<comments>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-using-hybridsessionbuilder/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 00:29:33 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[nhibernate]]></category>

		<guid isPermaLink="false">/post/2009/01/25/Multiple-Configurations-for-NHibernate-using-HybridSessionBuilder.aspx</guid>
		<description><![CDATA[ 
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 [...]]]></description>
			<content:encoded><![CDATA[<h2> </h2>
<p>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.</p>
<p>This post is also written because of a simple question: <em>Does the HybridSessionBuilder support multiple configurations</em>?</p>
<p>Why this question? Well, I have a project where some repositories uses one database and some other uses another database.</p>
<h2>Original implementation</h2>
<p>My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:</p>
<p><a title="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/" href="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/">http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/</a></p>
<p>But as you can see:<a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_2.png"><img style="border-width: 0px;" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb.png" border="0" alt="image" width="544" height="283" /></a></p>
<p>The original implementation used a static session factory and session, so this is does not support multiple configurations.</p>
<h2>CodeCampServer/Tarantino</h2>
<p>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.</p>
<p>But by viewing the source code for <a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a> I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Furthermore I found out that the HSB now is a part of a collection of libraries called <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino</a>.</p>
<p>I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.</p>
<h2>Test web application</h2>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_4.png"><img style="border-width: 0px;" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_1.png" border="0" alt="image" width="198" height="156" align="right" /></a> Then I build a small test web application project. It used Castle Windsor (for dependency injection) and NHibernate. I quickly configured Castle Windsor and made two NHibernate configuration files.</p>
<p>nhibernate.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true</em></p>
<p>nhibernate2.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true</em></p>
<p>My windsor.config.xml looks like this:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">component</span> <span style="color: #ff0000;">id</span><span style="color: #0000ff;">="firstRepository"</span> <span style="color: #ff0000;">type</span><span style="color: #0000ff;">="HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest"</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">parameters</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ConfigurationFile</span><span style="color: #0000ff;">&gt;</span>hibernate.cfg.xml<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ConfigurationFile</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">parameters</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">component</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">component</span> <span style="color: #ff0000;">id</span><span style="color: #0000ff;">="secondRepository"</span> <span style="color: #ff0000;">type</span><span style="color: #0000ff;">="HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest"</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">parameters</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ConfigurationFile</span><span style="color: #0000ff;">&gt;</span>hibernate2.cfg.xml<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ConfigurationFile</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">parameters</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">component</span><span style="color: #0000ff;">&gt;</span></pre>
</div>
</div>
<p><span style="font-family: Lucida Sans Unicode;"> </span></p>
<p>It basically just configures the two repositories with each own configuration.</p>
<p>But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_6.png"><img style="border-width: 0px;" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_2.png" border="0" alt="image" width="534" height="42" /></a></p>
<p>Only the default NHibernate configuration file was used.</p>
<h2>Bug or feature?</h2>
<p>That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.</p>
<p>I was expecting an object structure like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_12.png"><img style="border-width: 0px;" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_5.png" border="0" alt="image" width="534" height="178" /></a></p>
<p>But I got something like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_10.png"><img style="border-width: 0px;" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_4.png" border="0" alt="image" width="534" height="185" /></a>  I identified the following method to be the central, when the session was instantiated:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   1:</span> <span style="color: #0000ff;">private</span> ISession getExistingOrNewSession(ISessionFactory factory, <span style="color: #0000ff;">string</span> configurationFile)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   2:</span>         {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   3:</span>             <span style="color: #0000ff;">if</span> (HttpContext.Current != <span style="color: #0000ff;">null</span>)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   4:</span>             {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   5:</span>                 var session = GetExistingWebSession();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   6:</span> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   7:</span>                 <span style="color: #0000ff;">if</span> (session == <span style="color: #0000ff;">null</span> || !session.IsOpen)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   8:</span>                 {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   9:</span>                     session = openSessionAndAddToContext(factory, configurationFile);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  10:</span>                 }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  11:</span> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  12:</span>                 <span style="color: #0000ff;">return</span> session;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  13:</span>             }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  14:</span> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  15:</span>             var currentSession = _currentSessions.ContainsKey(configurationFile)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  16:</span>                                     ? _currentSessions[configurationFile]</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  17:</span>                                     : <span style="color: #0000ff;">null</span>;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  18:</span>             <span style="color: #0000ff;">if</span> (currentSession == <span style="color: #0000ff;">null</span> || !currentSession.IsOpen)</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  19:</span>             {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  20:</span>                 _currentSessions[configurationFile] = OpenSession(factory);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  21:</span>             }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  22:</span> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  23:</span>             <span style="color: #0000ff;">return</span> _currentSessions[configurationFile];</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">  24:</span>         }</pre>
</div>
</div>
<h2>Patch/solution</h2>
<p>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.</p>
<p>I changed it to:</p>
<div>
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060;">   1:</span> var session = GetExistingWebSession(configurationFile);</pre>
</div>
</div>
<p> </p>
<p>And tadaaa:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_14.png"><img style="border-width: 0px;" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_6.png" border="0" alt="image" width="534" height="42" /></a></p>
<p>The second repository now uses the second configuration file <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.</p>
<p>First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.</p>
<p>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.</p>
<p>And BTW &#8211; 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.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-using-hybridsessionbuilder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple Configurations for NHibernate (HybridSessionBuilder)</title>
		<link>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-hybridsessionbuilder/</link>
		<comments>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-hybridsessionbuilder/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 19:31:04 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">/post/2009/01/24/Multiple-Configurations-for-NHibernate-(HybridSessionBuilder).aspx</guid>
		<description><![CDATA[&#160;
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 [...]]]></description>
			<content:encoded><![CDATA[<h2>&#160;</h2>
<p>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.</p>
<p>This post is also written because of a simple question: <em>Does the HybridSessionBuilder support multiple configurations</em>?</p>
<p>Why this question? Well, I have a project where some repositories uses one database and some other uses another database.</p>
<h2>Original implementation</h2>
<p>My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:</p>
<p><a title="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/" href="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/">http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/</a></p>
<p>But as you can see:<a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="283" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb.png" width="544" border="0" /></a></p>
<p>The original implementation used a static session factory and session, so this is does not support multiple configurations.</p>
<h2>CodeCampServer/Tarantino</h2>
<p>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.</p>
<p>But by viewing the source code for <a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a> I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Furthermore I found out that the HSB now is a part of a collection of libraries called <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino</a>.</p>
<p>I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.</p>
<h2>Test web application </h2>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_4.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="156" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_1.png" width="198" align="right" border="0" /></a> Then I build a small test web application project. It used Castle Windsor (for dependency injection) and NHibernate. I quickly configured Castle Windsor and made two NHibernate configuration files.</p>
<p>nhibernate.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true</em></p>
<p>nhibernate2.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true</em></p>
<p>My windsor.config.xml looks like this:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;</span><span style="color: #800000">component</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;firstRepository&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span>hibernate.cfg.xml<span style="color: #0000ff">&lt;/</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">component</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;</span><span style="color: #800000">component</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;secondRepository&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span>hibernate2.cfg.xml<span style="color: #0000ff">&lt;/</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">component</span><span style="color: #0000ff">&gt;</span></pre>
</p></div>
</div>
<p><font face="Lucida Sans Unicode"></font></p>
<p>It basically just configures the two repositories with each own configuration.</p>
<p>But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_6.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="42" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_2.png" width="534" border="0" /></a> </p>
<p>Only the default NHibernate configuration file was used.</p>
<h2>Bug or feature?</h2>
<p>That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.</p>
<p>I was expecting an object structure like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_12.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="178" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_5.png" width="534" border="0" /></a> </p>
<p>But I got something like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_10.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="185" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_4.png" width="534" border="0" /></a>&#160; I identified the following method to be the central, when the session was instantiated:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">private</span> ISession getExistingOrNewSession(ISessionFactory factory, <span style="color: #0000ff">string</span> configurationFile)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span>         {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span>             <span style="color: #0000ff">if</span> (HttpContext.Current != <span style="color: #0000ff">null</span>)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span>             {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   5:</span>                 var session = GetExistingWebSession();</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   7:</span>                 <span style="color: #0000ff">if</span> (session == <span style="color: #0000ff">null</span> || !session.IsOpen)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span>                 {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   9:</span>                     session = openSessionAndAddToContext(factory, configurationFile);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span>                 }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  11:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  12:</span>                 <span style="color: #0000ff">return</span> session;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  13:</span>             }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  14:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  15:</span>             var currentSession = _currentSessions.ContainsKey(configurationFile)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  16:</span>                                     ? _currentSessions[configurationFile]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  17:</span>                                     : <span style="color: #0000ff">null</span>;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  18:</span>             <span style="color: #0000ff">if</span> (currentSession == <span style="color: #0000ff">null</span> || !currentSession.IsOpen)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  19:</span>             {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  20:</span>                 _currentSessions[configurationFile] = OpenSession(factory);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  21:</span>             }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  22:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  23:</span>             <span style="color: #0000ff">return</span> _currentSessions[configurationFile];</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  24:</span>         }</pre>
</p></div>
</div>
<h2>Patch/solution</h2>
<p>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.</p>
<p>I changed it to:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> var session = GetExistingWebSession(configurationFile);</pre>
</p></div>
</div>
<p>&#160;</p>
<p>And tadaaa:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_14.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="42" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_6.png" width="534" border="0" /></a> </p>
<p>The second repository now uses the second configuration file <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.</p>
<p>First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.</p>
<p>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.</p>
<p>And BTW &#8211; 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.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-hybridsessionbuilder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NHibernate &#8211; many-to-many relationship with data attached</title>
		<link>http://blog.ovesens.net/2009/01/nhibernate-many-to-many-relationship-with-data-attached/</link>
		<comments>http://blog.ovesens.net/2009/01/nhibernate-many-to-many-relationship-with-data-attached/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 15:09:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">/post/2009/01/07/NHibernate-many-to-many-relationship-with-data-attached.aspx</guid>
		<description><![CDATA[
I just found a good blog post about the may-to-many relationship in NHibernate.


The configuration sis done in Fluent NHibernate.

http://codebetter.com/blogs/kyle.baley/archive/2008/12/24/many-to-many-relationships-with-data-attached-in-nhibernate.aspx
]]></description>
			<content:encoded><![CDATA[<p>
I just found a good blog post about the may-to-many relationship in NHibernate.
</p>
<p>
The configuration sis done in Fluent NHibernate.
</p>
<p>http://codebetter.com/blogs/kyle.baley/archive/2008/12/24/many-to-many-relationships-with-data-attached-in-nhibernate.aspx</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/01/nhibernate-many-to-many-relationship-with-data-attached/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ayende explains cascading with NHibernate</title>
		<link>http://blog.ovesens.net/2008/12/ayende-explains-cascading-with-nhibernate/</link>
		<comments>http://blog.ovesens.net/2008/12/ayende-explains-cascading-with-nhibernate/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 22:12:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">/post/2008/12/18/Ayende-explains-cascading-with-NHibernate.aspx</guid>
		<description><![CDATA[
To continue my little &#34;post with good links&#34;&#8230; here is a good article by Ayende, explaining cascadin with NHibernate:


http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx


Here is the basics:


Here is what each cascade option means:


none &#8211; do not do any cascades, let the users handles them by themselves.
save-update &#8211; when the object is saved/updated, check the assoications and save/update any object that [...]]]></description>
			<content:encoded><![CDATA[<p>
To continue my little &quot;post with good links&quot;&#8230; here is a good article by Ayende, explaining cascadin with NHibernate:
</p>
<p>
<a href="http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx">http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx</a>
</p>
<p>
Here is the basics:
</p>
<p>
Here is what each cascade option means:
</p>
<ul>
<li>none &#8211; do not do any cascades, let the users handles them by themselves.</li>
<li>save-update &#8211; when the object is saved/updated, check the assoications and save/update any object that require it (including save/update the assoications in many-to-many scenario).</li>
<li>delete &#8211; when the object is deleted, delete all the objects in the assoication.</li>
<li>delete-orphans &#8211; when the object is deleted, delete all the objects in the assoication. In addition to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.</li>
<li>all &#8211; when an object is save/update/delete, check the assoications and save/update/delete all the objects found.</li>
<li>all-delete-orhpans &#8211; when an object is save/update/delete, check the assoications and save/update/delete all the objects found. In additional to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.</li>
</ul>
<p>
&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2008/12/ayende-explains-cascading-with-nhibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning NHibernate</title>
		<link>http://blog.ovesens.net/2008/11/learning-nhibernate/</link>
		<comments>http://blog.ovesens.net/2008/11/learning-nhibernate/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 21:30:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">/post/2008/11/17/Learning-NHibernate.aspx</guid>
		<description><![CDATA[
NHibernate is a port of Hibernate Core for Java to the .NET Framework. It handles persisting plain .NET objects to and from an underlying relational database. Given an XML description of your entities and relationships, NHibernate automatically generates SQL for loading and storing the objects. Optionally, you can describe your mapping metadata with attributes in [...]]]></description>
			<content:encoded><![CDATA[<p>
NHibernate is a port of <a href="http://www.hibernate.org/344.html">Hibernate Core</a> for Java to the .NET Framework. It handles persisting plain .NET objects to and from an underlying relational database. Given an XML description of your entities and relationships, NHibernate automatically generates SQL for loading and storing the objects. Optionally, you can describe your mapping metadata with attributes in your source code.
</p>
<p>
Update 18-11-2008, well it turs out there infect is a NHibernate 2.0documentation here:
</p>
<p>
<a href="http://nhforge.org/doc/nh/en/index.html">http://nhforge.org/doc/nh/en/index.html</a>
</p>
<p>
&nbsp;
</p>
<p>
A very good place to start is here:
</p>
<p>
<a href="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/" title="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/">http://www.hibernate.org/hib_docs/v3/reference/en-US/html/</a>
</p>
<h2>Properties</h2>
<p>
Chapter 5 explains setting up properties in the mapping file, tags and attributes.
</p>
<p>
<a href="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/mapping.html" title="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/mapping.html">http://www.hibernate.org/hib_docs/v3/reference/en-US/html/mapping.html</a>
</p>
<h2>Collections</h2>
<p>
Chapter 6 is about collections and lists
</p>
<p>
<a href="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/collections.html" title="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/collections.html">http://www.hibernate.org/hib_docs/v3/reference/en-US/html/collections.html</a>
</p>
<h2>Many-to-many relations</h2>
<p>
Handling many-to-many relations can be difficult, especially when it comes to cascading and data integrity. This blog post explains one way of handling it.
</p>
<p>
<a href="http://codebetter.com/blogs/peter.van.ooijen/archive/2008/05/29/nhibernate-many-to-many-collections-or-mapping-is-not-one-table-one-class.aspx" title="http://codebetter.com/blogs/peter.van.ooijen/archive/2008/05/29/nhibernate-many-to-many-collections-or-mapping-is-not-one-table-one-class.aspx">http://codebetter.com/blogs/peter.van.ooijen/archive/2008/05/29/nhibernate-many-to-many-collections-or-mapping-is-not-one-table-one-class.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2008/11/learning-nhibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
