<?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>Brian Hartsock&#039;s Blog &#187; .NET</title>
	<atom:link href="http://blog.brianhartsock.com/category/programming/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.brianhartsock.com</link>
	<description>The exciting life of a software developer and nerd</description>
	<lastBuildDate>Thu, 29 Jul 2010 02:40:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>LINQ bites back</title>
		<link>http://blog.brianhartsock.com/2008/08/07/linq-bites-back/</link>
		<comments>http://blog.brianhartsock.com/2008/08/07/linq-bites-back/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 19:59:31 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/?p=114</guid>
		<description><![CDATA[Today I found some odd behavior when working with LINQ. It is hard to describe, so here is the code. public void Foo&#40;IEnumerable&#60;string&#62; strings&#41; &#123; IEnumberable&#60;string&#62; stringsInDb = strings.Where&#40;s =&#62; this.QueryTheDbAndTakeALongTime&#40;s&#41;&#41;; &#160; foreach&#40;string str in stringsInDb&#41; &#123; //Where clause in LINQ is actually executed &#125; &#160; foreach&#40;string str in stringsInDb&#41; &#123; //Where clause in LINQ [...]]]></description>
			<content:encoded><![CDATA[<p>Today I found some odd behavior when working with LINQ.  It is hard to describe, so here is the code.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Foo<span style="color: #000000;">&#40;</span>IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> strings<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	IEnumberable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> stringsInDb <span style="color: #008000;">=</span> strings.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>s <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">QueryTheDbAndTakeALongTime</span><span style="color: #000000;">&#40;</span>s<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> str <span style="color: #0600FF;">in</span> stringsInDb<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">//Where clause in LINQ is actually executed </span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> str <span style="color: #0600FF;">in</span> stringsInDb<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">//Where clause in LINQ is actually executed  AGAIN</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Luckily, some unit tests detected this behavior.  It actually isn&#8217;t surprising when you think about it though.  The proper thing to do for this case is actually call <em>ToList()</em> after the <em>Where()</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">IEnumberable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> stringsInDb <span style="color: #008000;">=</span> strings.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>s <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">QueryTheDbAndTakeALongTime</span><span style="color: #000000;">&#40;</span>s<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Don&#8217;t let this behavior bite you.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+LINQ+bites+back+http://bit.ly/eTQXA" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2008/08/07/linq-bites-back/&amp;title=LINQ+bites+back" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2008/08/07/linq-bites-back/&amp;t=LINQ+bites+back" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2008/08/07/linq-bites-back/&amp;title=LINQ+bites+back" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2008/08/07/linq-bites-back/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>LINQ&#8217;d up</title>
		<link>http://blog.brianhartsock.com/2008/07/08/linqd-up/</link>
		<comments>http://blog.brianhartsock.com/2008/07/08/linqd-up/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 01:27:48 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/?p=107</guid>
		<description><![CDATA[I just used LINQ for the first time ever in code. Pretty exciting. For me, it doesn&#8217;t get much better than transforming some mundane 6 liner into an elegant one liner. This&#8230; int count = 0; foreach &#40;string email in this.emails&#41; &#123; if &#40;email.EndsWith&#40;&#34;@mydomain.com&#34;&#41;&#41; count++; &#125; return count; becomes this&#8230; return this.emails.Count&#40;email =&#62; email.EndsWith&#40;&#34;@mydomain.com&#34;&#41;&#41;; In [...]]]></description>
			<content:encoded><![CDATA[<p>I just used <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx">LINQ </a> for the first time ever in code.  Pretty exciting.  For me, it doesn&#8217;t get much better than transforming some mundane 6 liner into an elegant one liner.</p>
<p>This&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">int</span> count <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> email <span style="color: #0600FF;">in</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">emails</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>email.<span style="color: #0000FF;">EndsWith</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@mydomain.com&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> count<span style="color: #008000;">++;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">return</span> count<span style="color: #008000;">;</span></pre></div></div>

<p>becomes this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">emails</span>.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span>email <span style="color: #008000;">=&gt;</span> email.<span style="color: #0000FF;">EndsWith</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@mydomain.com&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>In some weird kind of way, I feel like an emo Ruby developer.  Maybe I&#8217;ll start wearing some tighter clothes.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+LINQ%E2%80%99d+up+http://bit.ly/FcQki" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2008/07/08/linqd-up/&amp;title=LINQ%E2%80%99d+up" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2008/07/08/linqd-up/&amp;t=LINQ%E2%80%99d+up" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2008/07/08/linqd-up/&amp;title=LINQ%E2%80%99d+up" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2008/07/08/linqd-up/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NUnit and RowTest</title>
		<link>http://blog.brianhartsock.com/2008/06/19/nunit-and-rowtest/</link>
		<comments>http://blog.brianhartsock.com/2008/06/19/nunit-and-rowtest/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 03:05:45 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/?p=102</guid>
		<description><![CDATA[I have been researching different unit testing tools for .NET lately. Obviously, NUnit was the first I came across, but MbUnit had the RowTest attribute, which allows parameters to be added to tests. This saves a lot of code and is really a must have feature. Luckily, I found an NUnit addin for RowTest here. [...]]]></description>
			<content:encoded><![CDATA[<p>I have been researching different unit testing tools for .NET lately.  Obviously, <a href="http://www.nunit.org/index.php">NUnit</a> was the first I came across, but <a href="http://www.mbunit.com/">MbUnit</a> had the RowTest attribute, which allows parameters to be added to tests.  This saves a lot of code and is really a must have feature.  Luckily, I found an NUnit addin for RowTest <a href="http://www.andreas-schlapsi.com/projects/rowtest-extension-for-nunit/">here</a>.  The latest version here is linked to NUnit 2.4.6, so I rebuilt it from source to use NUnit 2.4.7.</p>
<p>To install for <a href="http://www.testdriven.net/">TestDriven.NET</a>, just create an addins folder in the <em>%install root%\TestDriven.NET 2.0\NUnit\2.4\</em> and copy NUnitExtension.RowTest.AddIn.dll to the newly created folder.  Then in your test project, just reference NUnitExtensino.RowTest and you are set to go.</p>
<p>Here is an example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> TestExample
<span style="color: #000000;">&#123;</span>
     <span style="color: #000000;">&#91;</span>TestFixture<span style="color: #000000;">&#93;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Tests
     <span style="color: #000000;">&#123;</span>
          <span style="color: #000000;">&#91;</span>RowTest<span style="color: #000000;">&#93;</span>
          <span style="color: #000000;">&#91;</span>Row<span style="color: #000000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">4</span>,<span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
          <span style="color: #000000;">&#91;</span>Row<span style="color: #000000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">5</span>,<span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
          <span style="color: #000000;">&#91;</span>Row<span style="color: #000000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">6</span>,<span style="color: #FF0000;">6</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
          <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> TestAddEqualsZero<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> x, <span style="color: #FF0000;">int</span> y<span style="color: #000000;">&#41;</span>
          <span style="color: #000000;">&#123;</span>
               Assert.<span style="color: #0000FF;">IsTrue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">+</span> y<span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+NUnit+and+RowTest+http://bit.ly/aidHx" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2008/06/19/nunit-and-rowtest/&amp;title=NUnit+and+RowTest" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2008/06/19/nunit-and-rowtest/&amp;t=NUnit+and+RowTest" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2008/06/19/nunit-and-rowtest/&amp;title=NUnit+and+RowTest" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2008/06/19/nunit-and-rowtest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Destructors and IDisposable</title>
		<link>http://blog.brianhartsock.com/2008/06/19/c-destructors-and-idisposable/</link>
		<comments>http://blog.brianhartsock.com/2008/06/19/c-destructors-and-idisposable/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 01:52:33 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/?p=101</guid>
		<description><![CDATA[This week I found myself somewhat confused on when to use destructor. Originally I assumed that anytime you implement IDisposable you should have a destructor that calls Dispose(). This is not the case though. The only time you really need a destructor is when a class directly contains unmanaged resources, like file handles. A good [...]]]></description>
			<content:encoded><![CDATA[<p>This week I found myself somewhat confused on when to use destructor.  Originally I assumed that anytime you implement <em>IDisposable</em> you should have a destructor that calls <em>Dispose()</em>.</p>
<p>This is not the case though.  The only time you really need a destructor is when a class directly contains unmanaged resources, like file handles.</p>
<p>A good example of when not to use a destructor is a wrapper class.  For instance, if you have a database connection class wrapped with some sort of adapter class, the adapter class doesn&#8217;t need a destructor.  It still needs to implement <em>IDisposable</em> to enable early disposal of resources, but the destructor of the underlying database connection class will handle any potential leak cases if <em>Dispose()</em> isn&#8217;t called.</p>
<p>Here is a quote from a very good <a href="http://www.ondotnet.com/pub/a/dotnet/2002/02/11/csharp_traps.html">article</a> on C# traps:</p>
<blockquote><p>
The destructor should only release unmanaged resources that your object holds on to, and it should not reference other objects. If you have only managed references you do not need to (and should not) implement a destructor. You want this only for handling unmanaged resources. Because there is some cost to having a destructor, you ought to implement this only on methods that consume valuable, unmanaged resources.
</p></blockquote>
<p>The really powerful statement is &#8220;it should not reference other objects&#8221; which basically backs up my previous statement on wrapper classes.</p>
<p>Here is another good <a href="http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx">article</a> on destructors and <em>IDisposable</em>.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+C%23+Destructors+and+IDisposable+http://bit.ly/KC1XC" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2008/06/19/c-destructors-and-idisposable/&amp;title=C%23+Destructors+and+IDisposable" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2008/06/19/c-destructors-and-idisposable/&amp;t=C%23+Destructors+and+IDisposable" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2008/06/19/c-destructors-and-idisposable/&amp;title=C%23+Destructors+and+IDisposable" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2008/06/19/c-destructors-and-idisposable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to actually use trigonometry in web applications</title>
		<link>http://blog.brianhartsock.com/2008/06/18/how-to-actually-use-trigonometry-in-web-applications/</link>
		<comments>http://blog.brianhartsock.com/2008/06/18/how-to-actually-use-trigonometry-in-web-applications/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 14:37:47 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/?p=100</guid>
		<description><![CDATA[Disclaimer: my teammate Dellanoce actually coded this, but is too cool to blog, so I am blogging for him. Mailtrust has many different customers, some of which like to have their own branded control panel or webmail interface. We allow them to customize many different facets of the user interface, including colors. This has some [...]]]></description>
			<content:encoded><![CDATA[<p>Disclaimer: my teammate Dellanoce actually coded this, but is too cool to blog, so I am blogging for him.</p>
<p><a href="http://www.mailtrust.com">Mailtrust</a> has many different customers, some of which like to have their own branded control panel or webmail interface.  We allow them to customize many different facets of the user interface, including colors.  This has some unintended consequences though.  Sometimes, a user might select a white font color on top of a white background.  This in turns causes support to get involved in fixing the problem, which wastes time on such a trivial problem.</p>
<p>But there is a better way.  Use trigonometry, or more specifically the Pythagorean theorem!  I had no idea how this worked, but Dellanoce coded up a few lines of C# to compare two colors.  It returns a distance value which can be compared to a threshold value you can set.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> ColorDifference<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> htmlColor1, <span style="color: #FF0000;">string</span> htmlColor2<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Color color1 <span style="color: #008000;">=</span> ColorTranslator.<span style="color: #0000FF;">FromHtml</span><span style="color: #000000;">&#40;</span>htmlColor1<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Color color2 <span style="color: #008000;">=</span> ColorTranslator.<span style="color: #0000FF;">FromHtml</span><span style="color: #000000;">&#40;</span>htmlColor2<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #FF0000;">double</span> rDiff <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToDouble</span><span style="color: #000000;">&#40;</span>color1.<span style="color: #0000FF;">R</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">-</span> Convert.<span style="color: #0000FF;">ToDouble</span><span style="color: #000000;">&#40;</span>color2.<span style="color: #0000FF;">R</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">double</span> gDiff <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToDouble</span><span style="color: #000000;">&#40;</span>color1.<span style="color: #0000FF;">G</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">-</span> Convert.<span style="color: #0000FF;">ToDouble</span><span style="color: #000000;">&#40;</span>color2.<span style="color: #0000FF;">G</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">double</span> bDiff <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToDouble</span><span style="color: #000000;">&#40;</span>color1.<span style="color: #0000FF;">B</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">-</span> Convert.<span style="color: #0000FF;">ToDouble</span><span style="color: #000000;">&#40;</span>color2.<span style="color: #0000FF;">B</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> Math.<span style="color: #0000FF;">Sqrt</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>rDiff <span style="color: #008000;">*</span> rDiff<span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>gDiff <span style="color: #008000;">*</span> gDiff<span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>bDiff <span style="color: #008000;">*</span> bDiff<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>I was quite impressed with the simplicity and elegance this solution provided.  I should probably go give my, or his, trig teacher a hug.  </p>
<p>If you want to learn more about how powerful Pythagorean theorem is to compare distance values of really any type of object, check this <a href="http://betterexplained.com/articles/measure-any-distance-with-the-pythagorean-theorem/">article</a> out.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+How+to+actually+use+trigonometry+in+web+applications+http://bit.ly/2kTrvq" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2008/06/18/how-to-actually-use-trigonometry-in-web-applications/&amp;title=How+to+actually+use+trigonometry+in+web+applications" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2008/06/18/how-to-actually-use-trigonometry-in-web-applications/&amp;t=How+to+actually+use+trigonometry+in+web+applications" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2008/06/18/how-to-actually-use-trigonometry-in-web-applications/&amp;title=How+to+actually+use+trigonometry+in+web+applications" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2008/06/18/how-to-actually-use-trigonometry-in-web-applications/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>CallContext isn&#8217;t the greatest thing since sliced bread</title>
		<link>http://blog.brianhartsock.com/2008/06/12/callcontext-isnt-the-greatest-thing-since-sliced-bread/</link>
		<comments>http://blog.brianhartsock.com/2008/06/12/callcontext-isnt-the-greatest-thing-since-sliced-bread/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 14:57:55 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/?p=99</guid>
		<description><![CDATA[In ASP.NET, it is quite common to need to store information in the context of a single web request. For example, a NHibernate session should usually exist for one web request. HttpContext is the tool for accomplishing this. But what happens when you have code embedded in business logic that might not always be running [...]]]></description>
			<content:encoded><![CDATA[<p>In ASP.NET, it is quite common to need to store information in the context of a single web request.  For example, a NHibernate session should usually exist for one web request.  <em>HttpContext</em> is the tool for accomplishing this.</p>
<p>But what happens when you have code embedded in business logic that might not always be running in an ASP.NET web application?  <em>HttpContext</em> isn&#8217;t valid, so you can&#8217;t use it.  Why not use <em>CallContext</em>, since <em>HttpContext</em> uses this anyways? </p>
<p>I was starting to convert some of my code to use this, when I found this <a href="http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html">article</a>.  Apparently doing this works most of the time but can cause some nasty bugs since ASP.NET can switch <em>HttpContext</em>&#8216;s without switching <em>CallContext</em>&#8216;s.</p>
<p>Instead, code that could be called from ASP.NET and other applications should first check for context.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>HttpContext.<span style="color: #0000FF;">Current</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #008080; font-style: italic;">//Using CallContext example since not in ASP.NET</span>
     CallContext.<span style="color: #0000FF;">SetData</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;blah&quot;</span>, <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #FF0000;">object</span> contextData <span style="color: #008000;">=</span> CallContext.<span style="color: #0000FF;">GetData</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;blah&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">else</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #008080; font-style: italic;">//Using HttpContext example since in ASP.NET</span>
     HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;blah&quot;</span>, <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #FF0000;">object</span> contextData <span style="color: #008000;">=</span> HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Items</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;blah&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>In fact, I will probably end up writing a static class to implement this so code doesn&#8217;t have to constantly check whether or not in an ASP.NET session.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+CallContext+isn%E2%80%99t+the+greatest+thing+since+sliced+bread+http://bit.ly/d5q0R" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2008/06/12/callcontext-isnt-the-greatest-thing-since-sliced-bread/&amp;title=CallContext+isn%E2%80%99t+the+greatest+thing+since+sliced+bread" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2008/06/12/callcontext-isnt-the-greatest-thing-since-sliced-bread/&amp;t=CallContext+isn%E2%80%99t+the+greatest+thing+since+sliced+bread" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2008/06/12/callcontext-isnt-the-greatest-thing-since-sliced-bread/&amp;title=CallContext+isn%E2%80%99t+the+greatest+thing+since+sliced+bread" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2008/06/12/callcontext-isnt-the-greatest-thing-since-sliced-bread/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using NMS</title>
		<link>http://blog.brianhartsock.com/2008/06/04/using-nms/</link>
		<comments>http://blog.brianhartsock.com/2008/06/04/using-nms/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 16:04:42 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/?p=98</guid>
		<description><![CDATA[NMS is the .NET client library for using ActiveMQ, as well as other messaging systems. Unfortunately, it is the only complete one that I found, so using it isn&#8217;t really a choice (although I recommend coding your own STOMP client). I encountered many problems using it, but after working through them, everything seems to work. [...]]]></description>
			<content:encoded><![CDATA[<p>NMS is the .NET client library for using ActiveMQ, as well as other messaging systems.  Unfortunately, it is the only complete one that I found, so using it isn&#8217;t really a choice (although I recommend coding your own STOMP client).  I encountered many problems using it, but after working through them, everything seems to work.</p>
<h4>Problem #1 &#8211; Downloading</h4>
<p>Almost all the links to NMS point to <a href="http://www.springframework.net/downloads/Spring.Messaging.Nms/">Sprint.NET&#8217;s NMS</a> build.  If you notice the date in the link, the last time it looks to have been built is March 20, 2007.  Also, almost all the links are broken on this site, which is never a good sign.</p>
<p>Instead, just download from <a href="http://activemq.apache.org/nms.html">Apache NMS </a>.  Since they don&#8217;t have releases, just checkout from SVN (https://svn.apache.org/repos/asf/activemq/activemq-dotnet/).  Note that the link on their website points to (https://svn.apache.org/repos/asf/activemq/trunk/) which isn&#8217;t NMS.</p>
<h4>Problem #2 &#8211; Application Hang</h4>
<p>One issue that exists with the NMS library is a race condition when sending synchronous messages.  Basically, when sending a synchronous message, if the transport connection dies after sending, the application waits forever.  Luckily I found this <a href="http://www.nabble.com/NMS-library-hangs-when-connection-to-ActiveMQ-is-broken-td16452218s2354.html">article</a> which tells you how to prevent it.  The Apache NMS <a href="http://activemq.apache.org/activemq-4-connection-uris.html">documentation</a> doesn&#8217;t  mention it, but there is a query parameter that can be added to the connection URI that will timeout synchronous calls.  So the URI would look like <em>tcp://myactivemqserver.com:61616/?transport.requesttimeout=10000</em>.</p>
<p>Even though this fixed the problem, it annoys me because it is very easy to fix without having to use a timeout parameter.  I am thinking about trying to get a patch applied to the library but until then, just email me if you want to know the fix.</p>
<h4>Problem #3 &#8211; Transport layer death</h4>
<p>Even though request timeout fixes multitudes of problems, I want to make sure that the connection is destroyed in the event of any error.  It isn&#8217;t documented very well, but this <em>ExceptionListener</em> event will bubble up anytime an error occurs in the transport thread and the socket gets closed.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//Create connection</span>
IConnectionFactory factory <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ConnectionFactory<span style="color: #000000;">&#40;</span>uri<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">connection</span> <span style="color: #008000;">=</span> factory.<span style="color: #0000FF;">CreateConnection</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">connection</span>.<span style="color: #0000FF;">ExceptionListener</span> <span style="color: #008000;">+=</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">connection</span>.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">connection</span> <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>There are two problems with doing this though.   One, it isn&#8217;t very thread safe because the delegate <strong>WILL</strong> get called from a different thread.  NMS is a multi-threaded library and there is no way around this.  Thread safety should be handled better, but this is a trivial example to show the concept of the <em>ExceptionListener</em> event.  Two, this event will get called if ActiveMQ returns an Exception message, which sometimes aren&#8217;t fatal.  Normally, I have only seen these when disconnecting so just detach the event handler from the event before closing the connection.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+Using+NMS+http://bit.ly/gr69Z" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2008/06/04/using-nms/&amp;title=Using+NMS" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2008/06/04/using-nms/&amp;t=Using+NMS" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2008/06/04/using-nms/&amp;title=Using+NMS" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2008/06/04/using-nms/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Clash of the Titans</title>
		<link>http://blog.brianhartsock.com/2008/03/06/clash-of-the-titans/</link>
		<comments>http://blog.brianhartsock.com/2008/03/06/clash-of-the-titans/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 01:16:49 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/2008/03/06/clash-of-the-titans/</guid>
		<description><![CDATA[Ten years ago, Java was the next big thing. It had portability and was purely object oriented, both of which were needed at the time. Today I found an article discussing Sun&#8217;s recent acquisition of two Python developers. Their main goal is making Python work on the JVM. This go me thinking, is Java trying [...]]]></description>
			<content:encoded><![CDATA[<p>Ten years ago, <a href="http://en.wikipedia.org/wiki/Java_%28programming_language%29">Java</a> was the next big thing.  It had portability and was purely object oriented, both of which were needed at the time.  Today I found an <a href="http://blog.internetnews.com/apatrizio/2008/03/sun-is-continuing-its-acquisit.html">article</a> discussing Sun&#8217;s recent acquisition of two Python developers.  Their main goal is making Python work on the <a href="http://en.wikipedia.org/wiki/Jvm">JVM</a>.  This go me thinking, is Java trying to play catch-up to <a href="http://en.wikipedia.org/wiki/.NET_Framework">.NET</a>?</p>
<p>Don&#8217;t get my wrong, I know Java is a very widely used platform, but what has really changed in the past few years with Java?  I might just be ignorant on the matter, but I haven&#8217;t heard of much.  .NET on the other hand seems to be the face paced new comer when it comes to platforms for development.  The feature-set in .NET Framework 3.5 is quite simply amazing.  Not to mention, it supports a multitude of languages, soon to be including <a href="http://en.wikipedia.org/wiki/IronPython">Python</a> and <a href="http://en.wikipedia.org/wiki/IronRuby">Ruby</a>.  </p>
<p>But, I can&#8217;t entirely say I support .NET.  The one thing that kills me is its ties to Windows.  I know, Microsoft doesn&#8217;t have much of a reason to port it to other operating systems, but this gives Java a huge leg up.  Even though <a href="http://www.mono-project.com/Main_Page">Mono</a> exists, it is barely keeping up with .NET Framework releases.</p>
<p>No matter which doctrine you subscribe to, competition is a good thing and will only make each platform better.  I look forward to watching both closely over the next few years.</p>
<p>To all my Java and Linux friends, feel free to leave flaming comments.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+Clash+of+the+Titans+http://bit.ly/zsOhZ" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2008/03/06/clash-of-the-titans/&amp;title=Clash+of+the+Titans" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2008/03/06/clash-of-the-titans/&amp;t=Clash+of+the+Titans" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2008/03/06/clash-of-the-titans/&amp;title=Clash+of+the+Titans" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2008/03/06/clash-of-the-titans/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NHibernate O/R Mapper: Part 2</title>
		<link>http://blog.brianhartsock.com/2008/02/10/nhibernate-or-mapper-part-2/</link>
		<comments>http://blog.brianhartsock.com/2008/02/10/nhibernate-or-mapper-part-2/#comments</comments>
		<pubDate>Mon, 11 Feb 2008 00:32:01 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/2008/02/10/nhibernate-or-mapper-part-2/</guid>
		<description><![CDATA[In a previous post I talked about my first impressions of NHibernate. Not a whole lot has changed since then, except I have learned a lot more. But, I said I would discuss what I didn&#8217;t like about NHibernate so here it goes. Session Management The biggest thing that I don&#8217;t like is the session [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous <a href="http://blog.brianhartsock.com/2007/12/10/nhibernate-or-mapper-part-1/">post</a> I talked about my first impressions of <a href="http://www.nhibernate.org/">NHibernate</a>.  Not a whole lot has changed since then, except I have learned a lot more.  But, I said I would discuss what I didn&#8217;t like about NHibernate so here it goes.</p>
<h4>Session Management</h4>
<p>The biggest thing that I don&#8217;t like is the session management aspect.  Although I have come to understand why NHibernate sessions are so valuable and critical to its functionality, it doesn&#8217;t stop the fact that persistent objects are tied to a single database.  For a project that follows the patterns and practices set forth in ivory towers, this works great.  All business entities are stored in a database in 6th <a href="http://en.wikipedia.org/wiki/Database_normalization#Normal_forms">normal form</a> and everyone is happy.  Well, when working with legacy and enterprise systems, that usually isn&#8217;t the case.  There are many different databases that are used and objects are stored across a variety of them.</p>
<p>In order to get my system to use NHibernate, the first challenge was incorporating multiple sessions.  A session is basically a connection to a single database that manages persistent objects.  This <a href="http://www.codeproject.com/KB/aspnet/NHibernateMultipleDBs.aspx">article</a> describes a really complex way to accomplish it, but all you really need to pay attention to is the <em>NHibernateSessionModule</em> and <em>NHibernateSessionManager.</em>  Using the outline described in the article, I successfully got multiple sessions to work fairly painlessly.</p>
<p>The second challenge was storing a persistent object in multiple database.  Since persistent objects in NHibernate can&#8217;t span multiple sessions, some data massaging has to be performed.  As I see it, there are two possibilities for storing an object in multiple database. </p>
<ol>
<li>Different objects for each database with type conversion operators</li>
<li>The same object for all databases with extra session logic</li>
</ol>
<p>The latter is obviously easier from a domain model perspective, but not from an implementation perspective.  For a persistent object from one session to be stored in another session, all references must be removed from the session.  This means unproxying the object and unpersisting any collections contained within the object.  Looking through the NHibernate source code, I was able to accomplish this with only a few lines of code.</p>
<h4>Setup &#038; Documentation</h4>
<p>Most of the getting started guides for NHibernate are no where near complete or up-to-date.  It took a couple days to get my environment up and running.  This isn&#8217;t really a problem with the library itself, but is still a problem for first time users.  In all reality, there seems to be a lack of documentation in general.  Three example mappings in the documentation is no where near enough for what is marketed as an enterprise ORM solution.</p>
<h4>Conclusion</h4>
<p>NHibernate is awesome.  Even with these minor issues, it is one of the greatest tools for web development I have ever used.  I hope that the development of this library continues to improve, and hopefully some of these issues will be resolved.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+NHibernate+O%2FR+Mapper%3A+Part+2+http://bit.ly/2jl38j" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2008/02/10/nhibernate-or-mapper-part-2/&amp;title=NHibernate+O%2FR+Mapper%3A+Part+2" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2008/02/10/nhibernate-or-mapper-part-2/&amp;t=NHibernate+O%2FR+Mapper%3A+Part+2" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2008/02/10/nhibernate-or-mapper-part-2/&amp;title=NHibernate+O%2FR+Mapper%3A+Part+2" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2008/02/10/nhibernate-or-mapper-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Debugging NUnit</title>
		<link>http://blog.brianhartsock.com/2007/12/31/debugging-nunit/</link>
		<comments>http://blog.brianhartsock.com/2007/12/31/debugging-nunit/#comments</comments>
		<pubDate>Mon, 31 Dec 2007 17:57:25 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/2007/12/31/debugging-nunit/</guid>
		<description><![CDATA[Today I ran into a few problems while writing test cases for NUnit. I kept receiving reflection errors and the exceptions were too vague to help me fix the problems. Luckily, I found an article on debugging while testing with NUnit. Works like a charm. Update&#8230; If you use TestDriven.NET you can just click on [...]]]></description>
			<content:encoded><![CDATA[<p>Today I ran into a few problems while writing test cases for NUnit.  I kept receiving reflection errors and the exceptions were too vague to help me fix the problems.  Luckily, I found an <a href="http://www.informit.com/articles/article.aspx?p=30335&#038;seqNum=3">article</a> on debugging while testing with NUnit.  Works like a charm.</p>
<p><b>Update&#8230;</b><br />
If you use <a href="http://www.testdriven.net">TestDriven.NET</a> you can just click on a project and use <em>Test With->Debugger</em>.  It has the same effect and is much easier than the above article.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+Debugging+NUnit+http://bit.ly/2dLePP" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2007/12/31/debugging-nunit/&amp;title=Debugging+NUnit" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2007/12/31/debugging-nunit/&amp;t=Debugging+NUnit" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2007/12/31/debugging-nunit/&amp;title=Debugging+NUnit" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2007/12/31/debugging-nunit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
