<?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; Development</title>
	<atom:link href="http://blog.ovesens.net/category/development/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>Async Httphandlers in ASP.NET</title>
		<link>http://blog.ovesens.net/2010/05/async-httphandlers-in-asp-net/</link>
		<comments>http://blog.ovesens.net/2010/05/async-httphandlers-in-asp-net/#comments</comments>
		<pubDate>Thu, 27 May 2010 11:57:44 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[async]]></category>
		<category><![CDATA[httphandler]]></category>
		<category><![CDATA[thread]]></category>
		<category><![CDATA[threading]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=235</guid>
		<description><![CDATA[What is the correct way to create a httphandler that potentially can take time and resources to complete.
Well this article explains it very well:
 http://msdn.microsoft.com/en-us/magazine/cc164128.aspx
If you don&#8217;t want to read all, you find the best solution in the bottom of the article.
But you can also find the code here, download this library C# Threadpool.
AsyncRequest
class AsyncRequest
{
  [...]]]></description>
			<content:encoded><![CDATA[<p>What is the correct way to create a httphandler that potentially can take time and resources to complete.</p>
<p>Well this article explains it very well:<br />
<a href="http://msdn.microsoft.com/en-us/magazine/cc164128.aspx" target="_blank"> http://msdn.microsoft.com/en-us/magazine/cc164128.aspx</a></p>
<p>If you don&#8217;t want to read all, you find the best solution in the bottom of the article.</p>
<p>But you can also find the code here, download this library <a href="http://blog.ovesens.net/wp-content/uploads/2010/05/threadpool.zip">C# Threadpool</a>.</p>
<h2>AsyncRequest</h2>
<pre id="ctl00_MTContentSelector1_mainContentContainer_ctl33_ctl00_ctl00_code">class AsyncRequest
{
  private AsyncRequestState _asyncRequestState;

  public AsyncRequest(AsyncRequestState ars)
  {
    _asyncRequestState = ars;
  }

  public void ProcessRequest()
  {
    // This is where the non-cpu-bound activity would take place, such as
    // accessing a Web Service, polling a slow piece of hardware, or
    // performing a lengthy database operation. I put the thread to sleep
    // for two seconds to simulate a lengthy operation.
    Thread.Sleep(2000);

    _asyncRequestState._ctx.Response.Output.Write(
            "AsyncThread, {0}",
            AppDomain.GetCurrentThreadId());

    // tell asp.net I am finished processing this request
    _asyncRequestState.CompleteRequest();
  }
}</pre>
<h2>AsyncHandler</h2>
<pre id="ctl00_MTContentSelector1_mainContentContainer_ctl35_ctl00_ctl00_code">namespace EssentialAspDotNet.HttpPipeline
{
  // AsyncRequestState and AsyncRequest remain the same
  // as in the previous example

  public class AsyncHandler : IHttpAsyncHandler
  {
    static DevelopMentor.ThreadPool _threadPool;

    static AsyncHandler()
    {
      _threadPool =
        new DevelopMentor.ThreadPool(2, 25, "AsyncPool");
      _threadPool.PropogateCallContext = true;
      _threadPool.PropogateThreadPrincipal = true;
      _threadPool.PropogateHttpContext = true;
      _threadPool.Start();
    }

    public void ProcessRequest(HttpContext ctx)
    {
     // not used
    }

    public bool IsReusable
    {
      get { return false;}
    }

    public IAsyncResult BeginProcessRequest(HttpContext ctx,
                     AsyncCallback cb, object obj)
    {
      AsyncRequestState reqState =
                     new AsyncRequestState(ctx, cb, obj);
      _threadPool.PostRequest(
                     new DevelopMentor.WorkRequestDelegate(ProcessRequest),
                     reqState);

      return reqState;
    }

    public void EndProcessRequest(IAsyncResult ar)
    {
    }

    void ProcessRequest(object state, DateTime requestTime)
    {
      AsyncRequestState reqState = state as AsyncRequestState;

      // Take some time to do it
      Thread.Sleep(2000);

      reqState._ctx.Response.Output.Write(
                   "AsyncThreadPool, {0}",
                    AppDomain.GetCurrentThreadId);

      // tell asp.net you are finished processing this request
      reqState.CompleteRequest();
    }

  }
}</pre>
<h2>AsyncPage</h2>
<pre>namespace EssentialAspDotNet.HttpPipeline
{
 public class AsyncPage : Page, IHttpAsyncHandler
  {
    static protected DevelopMentor.ThreadPool _threadPool;

    static AsyncPage()
    {
      _threadPool =
       new DevelopMentor.ThreadPool(2, 25, "AsyncPool");
      _threadPool.PropogateCallContext = true;
      _threadPool.PropogateThreadPrincipal = true;
      _threadPool.PropogateHttpContext = true;
      _threadPool.Start();
    }

    public new void ProcessRequest(HttpContext ctx)
    {
      // not used
    }

    public new bool IsReusable
    {
      get { return false;}
    }

    public IAsyncResult BeginProcessRequest(HttpContext ctx,
                                            AsyncCallback cb, object obj)
    {
      AsyncRequestState reqState =
             new AsyncRequestState(ctx, cb, obj);
      _threadPool.PostRequest(
             new DevelopMentor.WorkRequestDelegate(ProcessRequest),
             reqState);

      return reqState;
    }

    public void EndProcessRequest(IAsyncResult ar)
    {
    }

    void ProcessRequest(object state, DateTime requestTime)
    {
      AsyncRequestState reqState = state as AsyncRequestState;

      // Synchronously call base class Page.ProcessRequest
      // as you are now on a thread pool thread.
      base.ProcessRequest(reqState._ctx);

      // Once complete, call CompleteRequest to finish
      reqState.CompleteRequest();
    }
  }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2010/05/async-httphandlers-in-asp-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manually start Visual Studio dev. web server</title>
		<link>http://blog.ovesens.net/2010/03/manually-start-visual-studio-dev-web-server/</link>
		<comments>http://blog.ovesens.net/2010/03/manually-start-visual-studio-dev-web-server/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 10:58:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[vs]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=228</guid>
		<description><![CDATA[I needed to manually start the Visual Studio dev. web server for profililng purposes&#8230;
The web server is located here:
C:\Program Files (x86)\Common Files\microsoft shared\DevServer\9.0\WebDev.WebServer.EXE
Arguments



/port:&#60;port number&#62;
Optional, an unused port number between 1 and 65535.


/path:&#60;physical path&#62;
A valid directory where the web application is rooted.


/vpath:&#60;virtual path&#62;
Optional, the virtual path or application root in the form of &#8216;/&#60;app name&#62;&#8217;.



Example
C:\Program Files [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to manually start the Visual Studio dev. web server for profililng purposes&#8230;</p>
<pre>The web server is located here:
C:\Program Files (x86)\Common Files\microsoft shared\DevServer\9.0\WebDev.WebServer.EXE</pre>
<h2>Arguments</h2>
<table border="0" cellspacing="1" cellpadding="3">
<tbody>
<tr>
<td>/port:&lt;port number&gt;</td>
<td>Optional, an unused port number between 1 and 65535.</td>
</tr>
<tr>
<td>/path:&lt;physical path&gt;</td>
<td>A valid directory where the web application is rooted.</td>
</tr>
<tr>
<td>/vpath:&lt;virtual path&gt;</td>
<td>Optional, the virtual path or application root in the form of &#8216;/&lt;app name&gt;&#8217;.</td>
</tr>
</tbody>
</table>
<h2>Example</h2>
<pre>C:\Program Files (x86)\Common Files\microsoft shared\DevServer\9.0&gt;
WebDev.WebServer.EXE /port:8080 /path:"C:\Users\Administrator\Documents\Visual Studio 2008\Projects\WebApplication\Compiled" \vpath:"\"</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2010/03/manually-start-visual-studio-dev-web-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Sitemap .NET links</title>
		<link>http://blog.ovesens.net/2010/03/google-sitemap-net-links/</link>
		<comments>http://blog.ovesens.net/2010/03/google-sitemap-net-links/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 19:40:44 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[sitemap]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=226</guid>
		<description><![CDATA[Some links with information about creating a Google Sitemap either directly in XML or via some .NET code.
http://gerardmcgarry.com/blog/creating-a-google-sitemap-aspnet-website
http://www.codeproject.com/KB/aspnet/GoogleSiteMapProvider.aspx
]]></description>
			<content:encoded><![CDATA[<p>Some links with information about creating a Google Sitemap either directly in XML or via some .NET code.</p>
<p><a href="http://gerardmcgarry.com/blog/creating-a-google-sitemap-aspnet-website" target="_blank">http://gerardmcgarry.com/blog/creating-a-google-sitemap-aspnet-website</a></p>
<p><a href="http://www.codeproject.com/KB/aspnet/GoogleSiteMapProvider.aspx" target="_blank">http://www.codeproject.com/KB/aspnet/GoogleSiteMapProvider.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2010/03/google-sitemap-net-links/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.NET Resource localization</title>
		<link>http://blog.ovesens.net/2010/02/net-resource-localization/</link>
		<comments>http://blog.ovesens.net/2010/02/net-resource-localization/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 17:17:45 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[localization]]></category>
		<category><![CDATA[resources]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/2010/02/net-resource-localization/</guid>
		<description><![CDATA[ResourceBlender is open source and works for .NET:
http://www.resourceblender.com/
]]></description>
			<content:encoded><![CDATA[<p>ResourceBlender is open source and works for .NET:</p>
<p><a href="http://www.resourceblender.com/" target="_blank">http://www.resourceblender.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2010/02/net-resource-localization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IETester 0.4.2</title>
		<link>http://blog.ovesens.net/2010/02/ietester-0-4-2/</link>
		<comments>http://blog.ovesens.net/2010/02/ietester-0-4-2/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 15:02:25 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[ietester]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=212</guid>
		<description><![CDATA[Usually when I need to test a HTML design, I use Spoon browsers&#8230; but I saw this IE Tester 0.4.2 tool.
This is just to remind me, don&#8217;t install, it made the JavaScript of Gmail and other sites fail.
So, stick to Spoon, don&#8217;t use IETester 0.4.2.
]]></description>
			<content:encoded><![CDATA[<p>Usually when I need to test a HTML design, I use Spoon browsers&#8230; but I saw this IE Tester 0.4.2 tool.</p>
<p>This is just to remind me, don&#8217;t install, it made the JavaScript of Gmail and other sites fail.</p>
<p>So, stick to Spoon, don&#8217;t use IETester 0.4.2.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2010/02/ietester-0-4-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visual Studio 2008 debugging and breakpoints</title>
		<link>http://blog.ovesens.net/2010/01/visual-studio-2008-debugging-and-breakpoints/</link>
		<comments>http://blog.ovesens.net/2010/01/visual-studio-2008-debugging-and-breakpoints/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 08:38:32 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[debugger]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[vs2008]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=207</guid>
		<description><![CDATA[My development tools are currently Visual Studio 2008, Resharper 4.5, Gallio and Testdriven.NET. But for as long as I have used these tools, my Visual Studio debugger has only worked partly.
The problem
When I inserted some breakpoints in my code, the first one was almost all the time hit. However, when I tried to step through the [...]]]></description>
			<content:encoded><![CDATA[<p>My development tools are currently <a href="http://msdn.microsoft.com/en-us/vstudio/default.aspx" target="_blank">Visual Studio 2008</a>, <a href="http://www.jetbrains.com/resharper/index.html" target="_blank">Resharper 4.5</a>, <a href="http://www.gallio.org/" target="_blank">Gallio</a> and <a href="http://www.testdriven.net/" target="_blank">Testdriven.NET</a>. But for as long as I have used these tools, my Visual Studio debugger has only worked partly.</p>
<h2>The problem</h2>
<p>When I inserted some breakpoints in my code, the first one was almost all the time hit. However, when I tried to step through the code, it worked fine for a bout 3-6 steps. But then Visual Studio debugger decided that was enough, and just completed the code.</p>
<p>The good thing was, it forced me to write many unit test cases, but sometimes I really think it is nice to visually see the state of your objects, in the code.</p>
<p>I have spoken to colleagues, and tested with different combinations of Gallio, Testdriven.NET and Re-sharper enabled, as I thought they cased the problem. But the debugger just kept bugging me. I had actually given up finding a solution to this problem, thinking that Visual Studio 2010 would fix it.</p>
<h2>The solution</h2>
<p>So finally, in a complete other context, I stumbled upon this page:</p>
<p><a href="http://code.msdn.microsoft.com/KB957912" target="_blank">http://code.msdn.microsoft.com/KB957912</a></p>
<p>And thought&#8230;. that title sounds interesting&#8230; I quickly moved to the <a href="http://support.microsoft.com/kb/957912" target="_blank">KB article</a>, and started to read. I thought, that sounds exactly as my problem.</p>
<p>With nothing to loose I installed the update, and started to test the debugger&#8230; until now it seems to work <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><em>- please note, I do not use the debugger that often as I write very reliable code <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2010/01/visual-studio-2008-debugging-and-breakpoints/feed/</wfw:commentRss>
		<slash:comments>2</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>Memoryleaks with WinDbg</title>
		<link>http://blog.ovesens.net/2009/06/memoryleaks-with-windbg/</link>
		<comments>http://blog.ovesens.net/2009/06/memoryleaks-with-windbg/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 12:39:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[memoryleaks]]></category>

		<guid isPermaLink="false">/post/2009/06/12/Memoryleaks-with-WinDbg.aspx</guid>
		<description><![CDATA[I have experienced the first major project with a memory leak problem&#8230; and how do one get started with Memory leak finding.
It is a tough task and the learning curve is quite steep.
You have to get to know the WinDbg. You can start here: http://blogs.msdn.com/tess/pages/net-debugging-demos-information-and-setup-instructions.aspx
Tess Ferrandez has some very nice &#8220;Debugging labs&#8221;, well take a look [...]]]></description>
			<content:encoded><![CDATA[<p>I have experienced the first major project with a memory leak problem&#8230; and how do one get started with Memory leak finding.</p>
<p>It is a tough task and the learning curve is quite steep.</p>
<p>You have to get to know the WinDbg. You can start here: <a href="http://blogs.msdn.com/tess/pages/net-debugging-demos-information-and-setup-instructions.aspx">http://blogs.msdn.com/tess/pages/net-debugging-demos-information-and-setup-instructions.aspx</a></p>
<p>Tess Ferrandez has some very nice &#8220;Debugging labs&#8221;, well take a look at: <a href="http://blogs.msdn.com/tess/">http://blogs.msdn.com/tess/</a></p>
<p>If you have a problem with a memory leak, take a look at his one:</p>
<p><a href="http://blogs.msdn.com/tess/archive/2005/11/25/496899.aspx">http://blogs.msdn.com/tess/archive/2005/11/25/496899.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/06/memoryleaks-with-windbg/feed/</wfw:commentRss>
		<slash:comments>5</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>
	</channel>
</rss>
