<?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; c#</title>
	<atom:link href="http://blog.ovesens.net/tag/c/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, 19 Jan 2012 11:55:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using OpenCover and NUnit with MSBuild</title>
		<link>http://blog.ovesens.net/2011/11/using-opencover-and-nunit-with-msbuild/</link>
		<comments>http://blog.ovesens.net/2011/11/using-opencover-and-nunit-with-msbuild/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 08:23:07 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[msbuild]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[open cover]]></category>
		<category><![CDATA[unit test]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=442</guid>
		<description><![CDATA[I am working on a project that has a large code base with a lot of logic. For this project I am trying to adopt a more TDD oriented development approach. This includes unit- and integration testing, as well as code &#8230; <a href="http://blog.ovesens.net/2011/11/using-opencover-and-nunit-with-msbuild/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I am working on a project that has a large code base with a lot of logic. For this project I am trying to adopt a more TDD oriented development approach. This includes unit- and integration testing, as well as code coverage, and yes I do know that a high coverage percentage is not the same as good tests, but on the other hand, a low coverage indicates that more tests need to be written.</p>
<p>We are currently using NUnit.2.5.10.11092 and the code coverage tools I was able to find was <a href="http://www.ncover.com">NCover</a>, <a href="http://sourceforge.net/projects/partcover/">PartCover</a> and <a href="https://github.com/sawilde/opencover">OpenCover</a>. NCover is no longer free, and from what I can read it seems like OpenCover is the better choice compared to PartCover.</p>
<p>This <a href="http://www.palmmedia.de/Blog/2011/6/21/code-coverage-testing-with-opencover-and-partcover">blog post</a> describes well some of the differences and features of PartCover vs. OpenCover.</p>
<h2>MSBuild</h2>
<p>This is the MSBuild configuration file that we are currently using to run unit test and code coverage.</p>
<pre class="brush: xml;">&lt;Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"&gt;

  &lt;!-- Application Configurations --&gt;
  &lt;PropertyGroup&gt;
    &lt;NUnit-ToolPath&gt;..\packages\NUnit.2.5.10.11092\tools\&lt;/NUnit-ToolPath&gt;
    &lt;OpenCover-ToolPath&gt;..\packages\OpenCover.1.0.719\&lt;/OpenCover-ToolPath&gt;
    &lt;ReportGenerator-ToolPath&gt;..\packages\ReportGenerator.1.2.1.0\&lt;/ReportGenerator-ToolPath&gt;
    &lt;MSBuildCommunityTasksPath&gt;$(MSBuildProjectDirectory)\..\Libs\msbuild\&lt;/MSBuildCommunityTasksPath&gt;

    &lt;!-- Add paths to assemblies to test --&gt;
    &lt;TestAssemblies&gt;..\Tests\Core.Tests\bin\Debug\Core.Tests.dll ..\Tests\Model.Tests\bin\Debug\Model.Tests.dll&lt;/TestAssemblies&gt;

  &lt;/PropertyGroup&gt;

  &lt;ItemGroup&gt;
    &lt;AllProjects Include="..\Tests\**\*.csproj" /&gt;
  &lt;/ItemGroup&gt;

  &lt;Target Name="Clean"&gt;
    &lt;MSBuild Projects="@(AllProjects)" Targets="Clean" /&gt;
  &lt;/Target&gt;

  &lt;Target Name="Compile"&gt;
    &lt;MSBuild Projects="@(AllProjects)" Targets="Build" Properties="WarningLevel=1" /&gt;
  &lt;/Target&gt;

  &lt;Target Name="Test" DependsOnTargets="Clean;Compile"&gt;
    &lt;!-- /domain=single is not support by the NUnit msbuild task, and it is required for OpenCover coverage test --&gt;
    &lt;Exec Command="$(NUnit-ToolPath)nunit-console-x86.exe $(TestAssembliesPath) /nologo /noshadow /domain=single /output=test-results.xml" /&gt;
  &lt;/Target&gt;

  &lt;Target Name="Coverage" DependsOnTargets="Clean;Compile"&gt;

    &lt;!-- /domain=single is not support by the NUnit msbuild task, and it is required for OpenCover coverage test --&gt;
    &lt;Exec Command="$(OpenCover-ToolPath)OpenCover.Console.exe -register:user -target:"$(NUnit-ToolPath)nunit-console-x86.exe" -targetargs:"/noshadow $(TestAssembliesPath) /domain:single" -filter:"+[Model]* +[Core]* +[Datalayer]*" -output:coverage.xml" /&gt;
    &lt;Delete Files=".\coveragereport" /&gt;
    &lt;Exec Command="$(ReportGenerator-ToolPath)ReportGenerator.exe coverage.xml "coveragereport" html" /&gt;
    &lt;Exec Command="$(ReportGenerator-ToolPath)ReportGenerator.exe coverage.xml "coveragereport" xmlsummary" /&gt;
    &lt;Delete Files="coverage.xml" /&gt;
  &lt;/Target&gt;

  &lt;Target Name="Build" DependsOnTargets="Clean;Compile;Test;" /&gt;

&lt;/Project&gt;</pre>
<p>Running the different targets from command prompt is easy, just type &#8220;msbuild [MSBUILD CONFIG FILE].xml /t:[TARGET]&#8221;</p>
<p>[MSBUILD CONFIG FILE] should be the name of the file above, [TARGET] can be any of the following Clean, Compile, Test and Coverage.</p>
<p>The Coverage target generates a fine HTML report as well as a XML summary.</p>
<p>Please note that I have not used the NUnit task from MSBuild community as the /domain=single is important, and as it is currently not supported by that task.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2011/11/using-opencover-and-nunit-with-msbuild/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cuda v3.2 template project using C++</title>
		<link>http://blog.ovesens.net/2011/05/cuda-v3-2-template-project-using-cpp/</link>
		<comments>http://blog.ovesens.net/2011/05/cuda-v3-2-template-project-using-cpp/#comments</comments>
		<pubDate>Wed, 18 May 2011 16:59:27 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[cuda]]></category>
		<category><![CDATA[gpgpu]]></category>
		<category><![CDATA[gpu]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[vs2008]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=388</guid>
		<description><![CDATA[Important The tutorial is missing some compilation, linker, library settings steps. However you can download the template project, it works and has everything setup. Introduction Download template project source Download executable I am a developer who has been developing software &#8230; <a href="http://blog.ovesens.net/2011/05/cuda-v3-2-template-project-using-cpp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Important</h2>
<p>The tutorial is missing some compilation, linker, library settings steps. However you can download the template project, it works and has everything setup.</p>
<h1>Introduction</h1>
<ul>
<li><a href="/wp-content/uploads/2011/05/CudaTemplate_Cuda_3.2.zip">Download template project source</a></li>
<li><a href="/wp-content/uploads/2011/05/CudaTemplate_exe_Cuda_3.2.zip">Download executable</a></li>
</ul>
<h2><span style="font-size: 16px; color: #444444; line-height: 24px;">I am a developer who has been developing software using .NET and C# for several years. I have never used C or C++, and it has never been required.</span></h2>
<p>I like to investigate new technologies, mainly because I am curious, but also because it could make my daily development work easier or smarter.</p>
<p>Recently my focus has been directed towards GPGPU on the Nvidia Cuda platform.</p>
<p>The programming language for Cuda is called &#8220;Cuda C&#8221;. The name implies that knowledge of C indeed is required for using GPGPU on the Cuda platform.</p>
<p>I discovered that there exist .NET bindings to the Cuda platform and drivers. However, I find their usage complicated and insufficient, and further more kernel development will still have to done in Cuda C.</p>
<p>These fact made me realise that I would have to learn a bit of C and C++ to use Cuda as it was actually intended by Nvidia. Nvidia provides many samples and suggest that a Cuda development environment on Windows could use Visual Studio and Nvidia Parallel NSight for debugging, profiling etc.</p>
<p>As my knowledge of C and C++ development was severely limited, so was the setting up and configuration of Visual Studio 2008 for Cuda C development.</p>
<p>I have read &#8220;Cuda by example&#8230;&#8221; (<a href="http://developer.nvidia.com/object/cuda-by-example.html">http://developer.nvidia.com/object/cuda-by-example.html</a>), &#8220;Programming Massively Parallel Processors&#8230;&#8221; (<a href="http://www.nvidia.com/object/io_1264656303008.html">http://www.nvidia.com/object/io_1264656303008.html</a>) and &#8220;C Programming Language, 2. edition&#8221; (<a href="http://www.pearsonhighered.com/educator/product/C-Programming-Language/9780131103627.page">http://www.pearsonhighered.com/educator/product/C-Programming-Language/9780131103627.page</a>). These books have given me the foundation to start developing using Cuda C and GPGPU.</p>
<p>Setting up Visual Studio 2008 and making the compiler work required some work, but here is what I did.</p>
<h2>1. Download and install driver and toolkit</h2>
<p>Download Cuda toolkit and the developer driver and install. A restart is probably required. (<a href="http://developer.nvidia.com/object/gpucomputing.html">http://developer.nvidia.com/object/gpucomputing.html</a>)</p>
<h2>2. Start Visual Studio 2008, and create a new project of type Win32 Console Application</h2>
<p>Give the project and solution a name (here called Cuda_Template).</p>
<p><a href="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_2_create_project..png"><img class="alignnone size-full wp-image-389" title="Step 2 - Create solution and project" src="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_2_create_project..png" alt="" width="804" height="572" /></a></p>
<h2>3. Click Next</h2>
<p><a href="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_3_click_next.png"><img class="alignnone size-full wp-image-398" title="Step 3 - Click next" src="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_3_click_next.png" alt="" width="625" height="530" /></a></p>
<h2>4. Select Console application and check the empty project, then click Finish</h2>
<p><a href="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_4_project_settings.png"><img class="alignnone size-full wp-image-399" title="Step 4 - Project settings" src="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_4_project_settings.png" alt="" width="625" height="530" /></a></p>
<h2>5. Add new item called main.cpp of type C++ File</h2>
<p><a href="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_5_create_cpp_file.png"><img class="alignnone size-full wp-image-400" title="Step 5 - Create C++ file" src="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_5_create_cpp_file.png" alt="" width="804" height="514" /></a></p>
<h2>6. Add the following code the file</h2>
<pre class="brush: cpp;">#include &lt;stdio.h&gt; 

int main() {

    printf("Hello world...\n);
    return 0;

}</pre>
<h2>7. Build and try to run the exe file. The output should be</h2>
<p><a href="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_7_exe_output.png"><img class="alignnone size-full wp-image-404" title="Step 7 - Hello world output" src="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_7_exe_output.png" alt="" width="709" height="359" /></a></p>
<h2>8. Select Project -&gt; Custom Build Rules&#8230;</h2>
<p><a href="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_8_custom_build_rules.png"><img class="alignnone size-full wp-image-405" title="Step 8 - Custom build rules" src="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_8_custom_build_rules.png" alt="" width="1280" height="760" /></a></p>
<h2>9. Select Cuda Runtime API build rule (v3.2)</h2>
<p><a href="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_9_cuda_build_rules.png"><img class="alignnone size-full wp-image-406" title="Step 9 - Cuda build rule" src="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_9_cuda_build_rules.png" alt="" width="676" height="433" /></a></p>
<h2>10. Add a new file called kernel.cu</h2>
<p><a href="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_10_create_kernel.png"><img class="alignnone size-full wp-image-407" title="Step 10 - Create kernel" src="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_10_create_kernel.png" alt="" width="804" height="514" /></a></p>
<h2>11. Add the following to the file kernel.cu</h2>
<pre class="brush: c;">/* power: raise base to n-th power; n &gt;= 0 */
__device__ int devicePower(int base, int n) {

    int p = 1;

    for (int i = 1; i &lt;= n; ++i) {
        p = p * base;
    }

    return p;
}

__global__ void power( int *base, int *n, int *output, int threadMax ) {

    int tid = threadIdx.x + blockIdx.x * blockDim.x;

    if (tid &lt; threadMax) {
        output[tid] = devicePower(base[tid], n[tid]);
    }

}</pre>
<h2>12. Right click the newly created file and select properties</h2>
<p><a href="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_12_kernel_properties.png"><img class="alignnone size-full wp-image-414" title="Step 12 - Kernel properties" src="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_12_kernel_properties.png" alt="" width="1280" height="760" /></a></p>
<h2>13. Set the &#8220;Exclude From Build&#8221; and make sure that the project still builds</h2>
<p><a href="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_13_kernel_exclude_from_build.png"><img class="alignnone size-full wp-image-415" title="Step 13 - Exclude kernel from build" src="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_13_kernel_exclude_from_build.png" alt="" width="759" height="531" /></a></p>
<h2>14. Create a new file called call_kernel.cu</h2>
<p><a href="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_14_create_kernel_call_file..png"><img class="alignnone size-full wp-image-416" title="Step 14 - Create C++ kernel calling file" src="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_14_create_kernel_call_file..png" alt="" width="804" height="514" /></a></p>
<h2>15. Add the following to the file call_kernel.cu</h2>
<pre class="brush: c">#include &lt;cuda_runtime_api.h&gt;
#include "main.h"

// includes, kernels
#include &lt;kernel.cu&gt;

void call_kernel_power(int *base, int *n, int *output, int elementCount) {

    int *dev_base, *dev_n, *dev_output;
    int gridX = (elementCount+ThreadsPerBlock-1)/ThreadsPerBlock;

    cudaMalloc( (void**)&amp;dev_base, elementCount * sizeof(int) );
    cudaMalloc( (void**)&amp;dev_n, elementCount * sizeof(int) );
    cudaMalloc( (void**)&amp;dev_output, elementCount * sizeof(int) );

    cudaMemcpy( dev_base, base, elementCount * sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy( dev_n, n, elementCount * sizeof(int), cudaMemcpyHostToDevice);

    power&lt;&lt;&lt;gridX,ThreadsPerBlock&gt;&gt;&gt;(dev_base, dev_n, dev_output, elementCount);

    cudaMemcpy( output, dev_output, elementCount * sizeof(int), cudaMemcpyDeviceToHost);

    cudaFree( dev_base );
    cudaFree( dev_n );
    cudaFree( dev_output );
}</pre>
<h2>16. Create a new header file called main.h</h2>
<p><a href="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_16_create_main_header.png"><img class="alignnone size-full wp-image-419" title="Step 16 - Create header main.h file" src="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_16_create_main_header.png" alt="" width="804" height="514" /></a></p>
<h2>17. Add the following content to the file</h2>
<pre class="brush: cpp">#define ThreadsPerBlock 128

#include &lt;stdio.h&gt;

void call_kernel_power(int *base, int *n, int *output, int elementCount);</pre>
<h2>18. Update the main.cpp file with the following:</h2>
<pre class="brush: cpp">#include "main.h"

#define N   80000

int main() {

    printf("Power Cuda kernel test from C++\n");
    printf("Testing %d elements\n", N);

    int base[N], n[N], output[N];

    for(int i = 0; i &lt; N; i++) {
          base[i] = 2;
          n[i] = i+1;
          output[i] = 0;
      }

    call_kernel_power(base, n, output, N);

    for(int i = 0; i &lt; N &amp;&amp; i &lt; 15; i++) {

          printf("%d^%d = %d\n", base[i], n[i], output[i]);

      }

      printf("Done\n");

    return 0;
}</pre>
<h2>19. That should be it&#8230;</h2>
<p>You now have a template that you can work from. When you build the file and run it, I get this output:</p>
<p><a href="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_19_exe_output.png"><img class="alignnone size-full wp-image-417" title="Step 19 - Output" src="http://blog.ovesens.net/wp-content/uploads/2011/05/CudaTemplate_19_exe_output.png" alt="" width="661" height="487" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2011/05/cuda-v3-2-template-project-using-cpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NHibernate CompositeId and GetHashCode SELECT N+1 problem</title>
		<link>http://blog.ovesens.net/2011/02/nhibernate-compositeid-and-gethashcode-select-n1-problem/</link>
		<comments>http://blog.ovesens.net/2011/02/nhibernate-compositeid-and-gethashcode-select-n1-problem/#comments</comments>
		<pubDate>Mon, 28 Feb 2011 20:34:59 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=337</guid>
		<description><![CDATA[I was looking in my NHProf log and got a lot of SELECT request. To many and I thought I was having a SELECT N+1 problem. Using the &#8220;Stack Trace&#8221; in NHprof I identified a call being made to GetHashCode() &#8230; <a href="http://blog.ovesens.net/2011/02/nhibernate-compositeid-and-gethashcode-select-n1-problem/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was looking in my NHProf log and got a lot of SELECT request. To many and I thought I was having a SELECT N+1 problem.</p>
<p>Using the &#8220;Stack Trace&#8221; in NHprof I identified a call being made to GetHashCode() of the class (<em>MyClass</em>) holding the composite id. <em>MyClass</em> looked like this:</p>
<pre class="brush: csharp;">public class MyClass
{
    public virtual Key1Class Key1 { get; set; }
    public virtual Key2Class Key2 { get; set; }

    /*
     * Other virtual properties
     * ...
     */

    public override int GetHashCode()
    {
        unchecked
        {
            return ((Key1 != null ? Key1.GetHashCode() : 0) * 397) ^ (Key2 != null ? Key2.GetHashCode() : 0);
        }
    }
}</pre>
<p>Note that Key1 and Key2 are both references, and please take a closer look at the GetHashCode method.</p>
<h2>The problem</h2>
<p>The problem I was facing&#8230; NHibernate called the GetHashCode() of <em>MyClass</em> when doing the query. And the call to <em>MyClass</em>&#8216;s GetHashCode method subsequentily called GetHashCode() of both the Key1 and Key2 classes.</p>
<p>Key1 and Key2 were loaded as proxies and hence a call to something else than theirs Id resulted in a select being fired.</p>
<h2>The solution</h2>
<p>The solution was to rewrite the GetHashCode method to this:</p>
<pre class="brush: csharp;">public override int GetHashCode()
{
    unchecked
    {
        return ((Key1 != null ? Key1.Id : 0) * 397) ^ (Key2 != null ? Key2.Id : 0);
    }
}</pre>
<p>Note that both Id&#8217;s of KeyClass1 and KeyClass2 are integers.</p>
<p>Now a call to <em>MyClass</em> GetHashCode does not result in individual SELECT statements being fired, even though the Keys area lazy loaded.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2011/02/nhibernate-compositeid-and-gethashcode-select-n1-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IDrive .NET backup library</title>
		<link>http://blog.ovesens.net/2011/02/idrive-net-backup-library/</link>
		<comments>http://blog.ovesens.net/2011/02/idrive-net-backup-library/#comments</comments>
		<pubDate>Tue, 22 Feb 2011 20:05:07 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[idrive]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=329</guid>
		<description><![CDATA[What does IDriveLibrary it do? IDrive EVS gives you 5GB of free backup space on the IDrive platform. IDrive EVS makes it possible via a kind of web http interface to upload files and more. The library I have written &#8230; <a href="http://blog.ovesens.net/2011/02/idrive-net-backup-library/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>What does IDriveLibrary it do?</h2>
<p><a href="http://evs.idrive.com/" target="_blank">IDrive EVS</a> gives you 5GB of free backup space on the IDrive platform. IDrive EVS makes it possible via a kind of web http interface to upload files and more.</p>
<p>The library I have written is called IDriveLibrary and can be found here <a href="https://bitbucket.org/ovesen/idrivelibrary">https://bitbucket.org/ovesen/idrivelibrary</a>. My thought about the project, was to be able to write a backup routine in C# for my servers.</p>
<p>Unfortunately IDrive EVS seems buggy and slow. I have in an earlier review of backup providers deemed IDrive buggy (<a href="http://blog.ovesens.net/2010/01/test-of-online-backup-providers/">http://blog.ovesens.net/2010/01/test-of-online-backup-providers/</a>). Nothing has changed.</p>
<p>During the development of the library I have had to contact IDrive 5 times, either because documented features simply did not work or because of errors in the documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2011/02/idrive-net-backup-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sample TwitterTest authentication and console application</title>
		<link>http://blog.ovesens.net/2011/02/sample-twittertest-authentication-and-console-application/</link>
		<comments>http://blog.ovesens.net/2011/02/sample-twittertest-authentication-and-console-application/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 16:52:49 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=300</guid>
		<description><![CDATA[How to make your application or service tweet My case: I have a windows service and I want to be able to know the internal state of this service and e.g. when an exception was thrown. The obviusl answer for &#8230; <a href="http://blog.ovesens.net/2011/02/sample-twittertest-authentication-and-console-application/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>How to make your application or service tweet</h2>
<p>My case: I have a windows service and I want to be able to know the internal state of this service and e.g. when an exception was thrown. The obviusl answer for this is logging. But I already use Log4Net, and I want to be able to access this info without having to remote to the server.<br />
Others have solved this issue by making e.g. Kayak part of the service and expose that state as JSON. That do sound tempting, however I am more to some kind of logging mechanism. I know that there exists Twitter Appenders for Log4Net, but by using something existing I will not learn anything <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
So what I want to implement is custom Twitter logging, and here is how.</p>
<ol>
<li>Register your application with Twitter (http://dev.twitter.com/apps/new)</li>
<li>Save your consumerKey and consumerSecret</li>
<li>Run TwitterAuthenticator to retrieve the Access token. Save Token and TokenSecret</li>
<li>Use the TweetConsole as a sample for Tweeting. Use the consumerKey, consumerSecret and the Access Token details</li>
</ol>
<p>That is it&#8230;</p>
<p>You can find the source code here: <a href="https://bitbucket.org/ovesen/twittertest">https://bitbucket.org/ovesen/twittertest</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2011/02/sample-twittertest-authentication-and-console-application/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>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 &#8230; <a href="http://blog.ovesens.net/2009/01/using-multiple-configuration-files-for-nhibernate-hybridsessionbuilder/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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 (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 &#8230; <a href="http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-hybridsessionbuilder/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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>2</slash:comments>
		</item>
		<item>
		<title>How will you parallelize your existing codebase? Try R.A.S.P</title>
		<link>http://blog.ovesens.net/2008/12/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p/</link>
		<comments>http://blog.ovesens.net/2008/12/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 19:08:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[thread]]></category>
		<category><![CDATA[threading]]></category>

		<guid isPermaLink="false">/post/2008/12/24/How-will-you-parallelize-your-existing-codebase-Try-RASP.aspx</guid>
		<description><![CDATA[Original post: http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- How will you parallelize your existing codebase? Try R.A.S.P. There has been much talk of how we will be writing all of our new code with parallelization in mind.&#160; However, what of our existing code?&#160; It&#8217;s &#8230; <a href="http://blog.ovesens.net/2008/12/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>
Original post:
</p>
<p>
<a href="http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx">http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx</a>
</p>
<p>
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-
</p>
<h2>How will you parallelize your existing codebase? Try R.A.S.P.</h2>
<p>
There<br />
has been much talk of how we will be writing all of our new code with<br />
parallelization in mind.&nbsp; However, what of our existing code?&nbsp; It&rsquo;s<br />
unlikely that everyone will just suddenly dump decades of existing code<br />
and write everything from scratch.&nbsp; In this article I&rsquo;m going to<br />
provide a simple methodology for how we might deal with the ever<br />
building problem of parallelizing our existing mountains of code.&nbsp;<br />
Comments and contributions are welcome.
</p>
<p>
&nbsp;
</p>
<h3>Methodologies of the Past</h3>
<p>
From<br />
the STL to .NET, the frameworks we have constructed our applications<br />
around have been heavily dependent on the idea of an application having<br />
a single thread.&nbsp; Given that the foundation of what we have all been<br />
using for a very long time was constructed around this preconception,<br />
it&rsquo;s unreasonable to expect that much of our existing code will ever be<br />
fully parallel.&nbsp; Even those that wrote code on top of a thread safe<br />
framework may find that years of patches and and poor design decisions<br />
make ground up parallelization impossible.
</p>
<p>
If we set our<br />
expectations reasonably, we see that we should instead focus on<br />
leveraging parallelism to improve the performance of the slowest parts<br />
of our software.&nbsp; From this viewpoint, parallelization is an<br />
optimization problem.&nbsp; Like all optimization, the difficulty of<br />
parallelizing code will have much to do with the methodologies which<br />
were used to write it.&nbsp;&nbsp;
</p>
<p>
Of course, object-oriented code being written with modern <a href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod">S.O.L.I.D. principles</a><br />
and will be easier to parallelize than older procedural code.&nbsp; At the<br />
same time, a poorly organized codebase or poorly written code will<br />
always make change difficult and so also hard to parallelize.&nbsp; This is<br />
why well written code is worth the investment.&nbsp; We will see the<br />
investment paying off in spades for the companies who have bothered to<br />
care about code quality.&nbsp; Others who find they have spaghetti code<br />
under the hood will find they will need to deeply segregate and<br />
modularize before parallelization is possible.
</p>
<p>
&nbsp;
</p>
<h3>A Methodology for Revisiting the Past</h3>
<p>
In<br />
most cases it would be a poor choice to implement your own threading<br />
API.&nbsp; Efficient and easy to use parallelization APIs are coming to (or<br />
already part of) every commonly used language and framework.&nbsp; Most of<br />
these APIs are not only built on top of <a href="http://lacl.univ-paris12.fr/gava/PAPP2009/">years of research</a>,<br />
they also have been written and debugged by a large number of people<br />
with specific expertise.&nbsp; These APIs are a godsend because they will<br />
allow most developers to parallelize existing software with a minimum<br />
amount of pain.&nbsp; The parallelization of existing code bases will be<br />
much the same as any other kind of performance tuning.&nbsp;
</p>
<p>
The<br />
key will be using a profiler to identify places in the code that would<br />
be sped up by parallelization and leveraging these new APIs to take<br />
advantage of the available hardware.&nbsp; The exciting part is that this<br />
can be done with any existing profiler and many existing APIs.&nbsp; The<br />
unfortunate part is that because memory sharing is such a big issue,<br />
parallelization requires a degree of separation beyond other types of<br />
optimization and so is likely to require some amount of refactoring.
</p>
<p>
Not<br />
all types of performance problems are conducive to being solved by<br />
parallelization, careful evaluation of the problem at hand is<br />
required.&nbsp; Also, as with anything that requires significant code<br />
change, building a solid test fixture is key to introducing as few bugs<br />
as possible.&nbsp; By leveraging the ideas of <a href="http://prematureoptimization.org/blog/archives/26">avoiding premature optimization</a>, <a href="http://dotnet.sys-con.com/node/133775">pragmatic unit testing</a>, <a href="http://msdn.microsoft.com/en-us/magazine/cc163329.aspx">using existing APIs</a>, and <a href="http://www.ryanlowe.ca/blog/archives/001048_be_mindful_of_code_entropy.php">mindful refactoring</a> it will be possible to introduce parallelization into many already existing projects with a manageable amount of risk.
</p>
<p>
&nbsp;
</p>
<h3>What is RASP?</h3>
<p>
While not included in the acronym, the first step in any kind of optimization is <strong><a href="http://en.wikipedia.org/wiki/Performance_analysis">profiling</a></strong>.&nbsp;<br />
Before you can begin to parallelize your code, you must determine where<br />
the bottlenecks might be.&nbsp; A broadly defined list of parallelizable<br />
things to look for would be, to quote <a href="http://loufranco.com/blog/files/20-Days-of-Clojure-Day-12.html">Rich Hickey</a>,<br />
&ldquo;independent data/work, moderate-to-course-grained work units and/or<br />
complex coordination logic that would be simplified with threads&rdquo;.&nbsp; A<br />
couple quick examples of low hanging fruit to be on the lookout for<br />
would be slow iterative loops and blocking I/O.&nbsp; It is important to<br />
note that as a general guideline it would be wrong to parallelize<br />
anything if it would not significantly increase the speed of your<br />
software.
</p>
<p>
For each of the bottlenecks found while profiling, parallelization is best separated into four steps:
</p>
<table border="0" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td><strong>Review</strong>: </td>
<td>Review code to determine if it is a good candidate for parallelization.</td>
</tr>
<tr>
<td><strong>Anchor:</strong></td>
<td>Create a unit test fixture to ensure that the behavior of the to be parallelized code does not change.</td>
</tr>
<tr>
<td><strong>Separate</strong>: </td>
<td>Ensure that the to be parallelized code has no shared memory constraints, and if it does, factor them out if possible.</td>
</tr>
<tr>
<td><strong>Parallelize</strong>: </td>
<td>Minimally refactor for parallelization while leveraging an existing API to do the heavy lifting.</td>
</tr>
</tbody>
</table>
<p>
As<br />
the specifics of what each of these would entail depends greatly on<br />
exactly which platform and language is in use, I will not go into them<br />
deeply now.&nbsp; Overall, it&rsquo;s a simple methodology but I think both<br />
sufficient for the task at hand and broadly applicable.&nbsp;
</p>
<p>
&nbsp;
</p>
<h3>Conclusion </h3>
<p>
Review,<br />
Anchor, Separate, Parallelize.&nbsp; It&rsquo;s not intended to be a difficult<br />
concept but instead to provide a simple path to parallelization.&nbsp; I<br />
would be very interested in hearing about any opinions on what RASP<br />
might be missing or how it may be better clarified.&nbsp; While I didn&rsquo;t<br />
have time to discuss them deeply in this post, <a href="http://www.cs.uiuc.edu/homes/snir/PPP/">parallelization patterns</a><br />
are also a key concept in using RASP as if you can&rsquo;t easily identify<br />
what can be parallelized than it would be impossible to use any<br />
parallelization methodology.&nbsp; In the future I hope flush out RASP<br />
further as well as discuss parallelization patterns in depth.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2008/12/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

