<?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/"
	>

<channel>
	<title>Graeme Hill's Dev Blog</title>
	<atom:link href="http://graemehill.ca/feed" rel="self" type="application/rss+xml" />
	<link>http://graemehill.ca</link>
	<description></description>
	<pubDate>Tue, 06 Dec 2011 01:04:47 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>C++ const correctness beats the .NET/Java alternatives</title>
		<link>http://graemehill.ca/c-const-correctness-beats-the-netjava-alternatives</link>
		<comments>http://graemehill.ca/c-const-correctness-beats-the-netjava-alternatives#comments</comments>
		<pubDate>Sun, 04 Dec 2011 05:29:58 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[VB.NET]]></category>

		<guid isPermaLink="false">http://graemehill.ca/?p=407</guid>
		<description><![CDATA[The C++ const keyword is a powerful tool.  It basically allows you to pass a pointer or reference that can only be used to call const methods.  ie: You can implement a mutable class and then pass an immutable reference to that class (the keyword also does other things, but that&#8217;s not what [...]]]></description>
			<content:encoded><![CDATA[<p>The C++ <code>const</code> keyword is a powerful tool.  It basically allows you to pass a pointer or reference that can only be used to call <code>const</code> methods.  ie: You can implement a mutable class and then pass an immutable reference to that class (the keyword also does other things, but that&#8217;s not what I&#8217;m writing about).  This defines a much stronger contract.  When a function asks for a pointer to an instance of <code>Foobar</code> it can promise that it will not change that object.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;string&gt;</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #666666;">// Here's a piece of a class with one const method and one non-const method</span>
<span style="color: #0000ff;">class</span> Person
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> getName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> name<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">void</span> setName<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> newName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> name <span style="color: #000080;">=</span> newName<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
	std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> name<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// This function asks for a references to a person but promises not to change the object</span>
<span style="color: #0000ff;">void</span> doSomethingWithPerson<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Person <span style="color: #000040;">&amp;</span>person<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> person.<span style="color: #007788;">getName</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// &lt;-- this is fine</span>
	person.<span style="color: #007788;">setName</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;hello&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// &lt;-- COMPILE ERROR</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Person myPerson<span style="color: #008080;">;</span>
	myPerson.<span style="color: #007788;">setName</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Foobar&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	doSomethingWithPerson<span style="color: #008000;">&#40;</span>myPerson<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">EXIT_SUCCESS</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I love code like this.  When I call <code>doSomethingWithPerson()</code> I <strong>know</strong> for sure that it will not change the object (assuming I have properly labelled my <code>const</code> methods).  This is an area where I generally think of languages like Java or C# as being superior to C++.  We commonly assume that these higher level languages are better at protecting programmers from themselves (or other programmers) but here it is not the case.  You can achieve a similar result by creating an immutable class, but what if you do want to change it in other cases?  Maybe you want to pass an immutable instance to one function and a mutable instance to another.  This can surely be implemented, but it likely involves creating a second class (maybe a read-only subclass) which is kind of a pain.  In reality developers often do not bother to create read-only versions of classes where it is warranted, but they will take the time to put <code>const</code> where it is needed.</p>
<p>After a lot of time just working in .NET I have returned to doing some C++ programming on a hobby project.  Suddenly I feel dirty when I go back to .NET and start passing objects as function parameters willy nilly without using a <code>const</code> reference.  It really is amazing how programming in different languages can change the way you think.</p>
]]></content:encoded>
			<wfw:commentRss>http://graemehill.ca/c-const-correctness-beats-the-netjava-alternatives/feed</wfw:commentRss>
		</item>
		<item>
		<title>Exporting armature animation with the Blender 2.5 Python API</title>
		<link>http://graemehill.ca/exporting-armature-animation-with-the-blender-25-python-api</link>
		<comments>http://graemehill.ca/exporting-armature-animation-with-the-blender-25-python-api#comments</comments>
		<pubDate>Fri, 28 Oct 2011 03:07:58 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
		
		<category><![CDATA[Blender]]></category>

		<category><![CDATA[Game development]]></category>

		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://graemehill.ca/?p=364</guid>
		<description><![CDATA[It took me forever to get this right so I thought I&#8217;d post my solution.  Most of the examples I found were for previous versions of Blender (the API changed a lot in Blender 2.5).  The sample scripts I did find never seemed to work and the official documentation seemed to be somewhat [...]]]></description>
			<content:encoded><![CDATA[<p>It took me forever to get this right so I thought I&#8217;d post my solution.  Most of the examples I found were for previous versions of Blender (the API changed a lot in Blender 2.5).  The sample scripts I did find never seemed to work and the official documentation seemed to be somewhat insufficient.</p>
<p>The first thing you need to understand is that there are bones, and then there are pose bones.  They are completely different objects:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Get the armature (this technique assumes it is selected)</span>
armature = bpy.<span style="color: black;">context</span>.<span style="color: black;">active_object</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Get bone and pose bone data</span>
bones = armature.<span style="color: black;">data</span>.<span style="color: black;">bones</span>
pose_bones = armature.<span style="color: black;">pose</span>.<span style="color: black;">bones</span></pre></div></div>

<p>Pose bones contain pose data for the current frame, while regular bone objects store the default state of the bone (ie: as it appears in edit mode).  In order to get a pose at a specific frame you actually need to set the frame:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">scene = bpy.<span style="color: black;">context</span>.<span style="color: black;">scene</span>
armature = bpy.<span style="color: black;">context</span>.<span style="color: black;">active_object</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Loop through every frame in the scene and get bone pose at each one</span>
<span style="color: #ff7700;font-weight:bold;">for</span> frame <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>scene.<span style="color: black;">frame_end</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    scene.<span style="color: black;">frame_set</span><span style="color: black;">&#40;</span>frame<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> pose_bone <span style="color: #ff7700;font-weight:bold;">in</span> armature.<span style="color: black;">pose</span>.<span style="color: black;">bones</span>:
        do_something_with_pose_bone<span style="color: black;">&#40;</span>pose_bone<span style="color: black;">&#41;</span></pre></div></div>

<p>We can now get each pose bone object at each frame.  All that&#8217;s left is figuring out how to extract the bone rotation and position info.  In my case I assumed that the bone does not move, it only rotates around its pivot point, which is called the &#8220;head&#8221; of the bone in Blender.  Here&#8217;s how we get the head location of the bone:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">bone = armature.<span style="color: black;">data</span>.<span style="color: black;">bones</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
head_position = armature.<span style="color: black;">matrix_world</span> <span style="color: #66cc66;">*</span> bone.<span style="color: black;">head_local</span></pre></div></div>

<p>That&#8217;s pretty easy, you just take the local position of the bone and translate it to world coordinates.  Rotation gets a little more complicated.  Since I set my rotations with quaternions in blender I thought it made sense to just use <code>pose_bone.rotation_quaternion</code>.  That expression yielded the exact value that is displayed in the editor while in pose mode so I figured I was good to go.  However, on closer inspection, the value returned by <code>rotation_quaternion</code> most definitely does not represent the rotation of that bone relative to its parent.  In order to get that value I eventually found this technique:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> get_pose_bone_matrix<span style="color: black;">&#40;</span>pose_bone<span style="color: black;">&#41;</span>:
    local_matrix = pose_bone.<span style="color: black;">matrix_channel</span>.<span style="color: black;">to_3x3</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> pose_bone.<span style="color: black;">parent</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> local_matrix
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> pose_bone.<span style="color: black;">parent</span>.<span style="color: black;">matrix_channel</span>.<span style="color: black;">to_3x3</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">inverted</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> local_matrix</pre></div></div>

<p>Note that you can easily convert between matrices and quaternions using the <code>to_quaternion()</code> and <code>to_matrix()</code> methods.  The idea here is to get the local matrix and then basically undue the rotation from the parent.  Originally I thought I would have to keep going up the tree to the root bone, but apparently not.  Going one level up seems to be enough.</p>
<p>I hope this saves someone from ripping their hair out like I did!</p>
]]></content:encoded>
			<wfw:commentRss>http://graemehill.ca/exporting-armature-animation-with-the-blender-25-python-api/feed</wfw:commentRss>
		</item>
		<item>
		<title>.NET reflection: don&#8217;t rely on the call stack, it&#8217;s a trap!</title>
		<link>http://graemehill.ca/net-reflection-dont-rely-on-the-call-stack-its-a-trap</link>
		<comments>http://graemehill.ca/net-reflection-dont-rely-on-the-call-stack-its-a-trap#comments</comments>
		<pubDate>Thu, 27 Oct 2011 01:48:38 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[VB.NET]]></category>

		<guid isPermaLink="false">http://graemehill.ca/?p=359</guid>
		<description><![CDATA[In recent memory I have twice tried to write reflection code that seemed totally badass at the time but turned out to be just plain bad.  One case involved the use of GetCallingAssembly() and the other involved navigating the stack trace with stackTrace.GetFrame(1).  While there is nothing inherently wrong with these functions (ie: [...]]]></description>
			<content:encoded><![CDATA[<p>In recent memory I have twice tried to write reflection code that seemed totally badass at the time but turned out to be just plain bad.  One case involved the use of <code>GetCallingAssembly()</code> and the other involved navigating the stack trace with <code>stackTrace.GetFrame(1)</code>.  While there is nothing inherently wrong with these functions (ie: they can be used perfectly fine for logging or debugging) you absolutely cannot depend on a specific result due to runtime optimizations by the .NET JIT compiler.  For example, imagine A calls B and B calls C. If C runs <code>GetCallingAssembly()</code> then you would expect to get the assembly of B; however, if function B gets inlined in A (which is not uncommon) then technically A is directly calling C, and you will actually get A as a result!  In fact, if you check the <a href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getcallingassembly.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getcallingassembly.aspx');">MSDN documentation</a> they even warn you about this: </p>
<blockquote><p>If the method that calls the GetCallingAssembly method is expanded inline by the just-in-time (JIT) compiler, or if its caller is expanded inline, the assembly that is returned by GetCallingAssembly may differ unexpectedly.</p></blockquote>
<p>The documentation for <code>GetExecutingAssembly()</code> has no such warning, but just to be safe I prefer to be explicit with something like this: <code>myObject.GetType().Assembly</code></p>
]]></content:encoded>
			<wfw:commentRss>http://graemehill.ca/net-reflection-dont-rely-on-the-call-stack-its-a-trap/feed</wfw:commentRss>
		</item>
		<item>
		<title>Are you sure you want List?  Maybe you really want HashSet</title>
		<link>http://graemehill.ca/are-you-sure-you-want-list-maybe-you-really-want-hashset</link>
		<comments>http://graemehill.ca/are-you-sure-you-want-list-maybe-you-really-want-hashset#comments</comments>
		<pubDate>Mon, 07 Feb 2011 00:31:27 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[VB.NET]]></category>

		<guid isPermaLink="false">http://graemehill.ca/?p=347</guid>
		<description><![CDATA[I often find that I get so used to using List&#60;T&#62; for my collections that I forget to consider whether it is really the right tool for the job.  If the collection is not ordered, and you don&#8217;t want duplicates then there are a lot of benefits to using HashSet&#60;T&#62;.  For example with [...]]]></description>
			<content:encoded><![CDATA[<p>I often find that I get so used to using <code>List&lt;T&gt;</code> for my collections that I forget to consider whether it is really the right tool for the job.  If the collection is not ordered, and you don&#8217;t want duplicates then there are a lot of benefits to using <code>HashSet&lt;T&gt;</code>.  For example with <code>List&lt;T&gt;</code> you&#8217;ll find yourself writing code like this:</p>
<pre lang="c#">
if (!myCollection.Contains(newString))
{
    myCollection.Add(newString);
}
</pre>
<p>This is a little verbose and it&#8217;s also inefficient.  Every time you call <code>Contains()</code> the entire collection has to be scanned.  With a <code>HashSet&lt;T&gt;</code> set operations are optimized and the code is simpler:</p>
<pre lang="c#">
myHashSet.Add(newString);
</pre>
<p>The other benefit is that you actually enforce the no duplicate rule since adding the same element more than once will have no effect.  Here&#8217;s what the MSDN documentation says about <code>HashSet&lt;T&gt;</code>:</p>
<blockquote><p>The HashSet(Of T) class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.</p></blockquote>
<p>In practice I am finding that there are actually a lot of scenarios when I really want a set and not a list.  So the next time you create a collection, think about whether you can use a <code><a href="http://msdn.microsoft.com/en-us/library/bb359438.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://msdn.microsoft.com/en-us/library/bb359438.aspx');">HashSet</a></code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://graemehill.ca/are-you-sure-you-want-list-maybe-you-really-want-hashset/feed</wfw:commentRss>
		</item>
		<item>
		<title>Converting from svn to git: another reason not to use external references</title>
		<link>http://graemehill.ca/converting-from-svn-to-git-another-reason-not-to-use-external-references</link>
		<comments>http://graemehill.ca/converting-from-svn-to-git-another-reason-not-to-use-external-references#comments</comments>
		<pubDate>Thu, 04 Nov 2010 04:27:25 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
		
		<category><![CDATA[Git]]></category>

		<category><![CDATA[SVN]]></category>

		<guid isPermaLink="false">http://graemehill.ca/?p=343</guid>
		<description><![CDATA[Usually it is easy to import a subversion repository into git.  You just do this:

mkdir my_project
cd my_project
git svn init svn://repo.url/goes/here
git svn fetch

This will always fetch the repository you specified, but sometimes in svn your project actually depends on multiple repositories (ie: you use external references).  The external reference will not be fetched and [...]]]></description>
			<content:encoded><![CDATA[<p>Usually it is easy to import a subversion repository into git.  You just do this:</p>
<pre>
mkdir my_project
cd my_project
git svn init svn://repo.url/goes/here
git svn fetch
</pre>
<p>This will always fetch the repository you specified, but sometimes in svn your project actually depends on multiple repositories (ie: you use external references).  The external reference will not be fetched and you will have to manually add it in somehow.</p>
<p>Did you really think it would be that easy?</p>
]]></content:encoded>
			<wfw:commentRss>http://graemehill.ca/converting-from-svn-to-git-another-reason-not-to-use-external-references/feed</wfw:commentRss>
		</item>
		<item>
		<title>The trouble with properties</title>
		<link>http://graemehill.ca/the-trouble-with-properties</link>
		<comments>http://graemehill.ca/the-trouble-with-properties#comments</comments>
		<pubDate>Sat, 02 Oct 2010 00:00:13 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://graemehill.ca/?p=328</guid>
		<description><![CDATA[Properties are supposed to provide encapsulation for your getter and setter logic, but they really don&#8217;t enforce it.  Here&#8217;s what I mean:

private String _title;
&#160;
public String Title
&#123;
    get
    &#123;
        // do custom getter stuff here
        [...]]]></description>
			<content:encoded><![CDATA[<p>Properties are supposed to provide encapsulation for your getter and setter logic, but they really don&#8217;t enforce it.  Here&#8217;s what I mean:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">String</span> _title<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> Title
<span style="color: #000000;">&#123;</span>
    get
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// do custom getter stuff here</span>
        <span style="color: #0600FF;">return</span> _title<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    set
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// do custom setter stuff here</span>
        _title <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The private field <code>_title</code> is the <em>backing store</em> for the property <code>Title</code>.  The issue is that I don&#8217;t want to manually create this backing store, and I <em>really</em> don&#8217;t want it to be available anywhere in the class.  I want to force anyone who edits this class to modify the value using the property, not the backing store.  You don&#8217;t have to create a backing store with a default property like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> Title <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span></pre></div></div>

<p>This is convenient, but it beats the point entirely.  I specifically want to avoid the backing store when I have custom logic in either the getter or the setter.  Currently in C# and VB (and every other language I can think of for that matter) there is no way to force the use of the property internally.  I think it would be ideal to have an automatic backing store that is only accessible within the getter and setter.</p>
<p>Am I crazy?</p>
]]></content:encoded>
			<wfw:commentRss>http://graemehill.ca/the-trouble-with-properties/feed</wfw:commentRss>
		</item>
		<item>
		<title>A lesson on using closed source libraries</title>
		<link>http://graemehill.ca/a-lesson-on-using-closed-source-libraries</link>
		<comments>http://graemehill.ca/a-lesson-on-using-closed-source-libraries#comments</comments>
		<pubDate>Sat, 19 Jun 2010 19:23:57 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[LINQ To SQL]]></category>

		<category><![CDATA[NHibernate]]></category>

		<category><![CDATA[Open source]]></category>

		<guid isPermaLink="false">http://graemehill.ca/?p=318</guid>
		<description><![CDATA[A challenging question that programmers are often faced with when chosing a library is whether to go with an open source option or a closed source product that may come with professional support.  The answer of course is &#8220;it depends&#8221; and the quality of each library will often be more important than whether or [...]]]></description>
			<content:encoded><![CDATA[<p>A challenging question that programmers are often faced with when chosing a library is whether to go with an open source option or a closed source product that may come with professional support.  The answer of course is &#8220;it depends&#8221; and the quality of each library will often be more important than whether or not it is open source.  In the past I have not concerned myself too much with whether or not the source code is available to me because I normally don&#8217;t plan on ever touching it.  I don&#8217;t use an existing library because I want to spend weeks digging through source code and internals, I use them because I don&#8217;t want to spend much time on that feature.  I want it to just work.</p>
<p>In recent projects I have looked closely at both LINQ to SQL and NHibernate for my ORM needs.  Since LINQ to SQL is easy to use and meets my requirements it seemed like a no brainer to use a product from Microsoft over NHibernate since I never planned to edit the NHibernate source.  However, a bug that I recently found in LINQ to SQL has made me think differrently (I wrote about the bug <a href="http://graemehill.ca/linq-to-sql-gotcha-2-getchangeset-weirdness" >here</a>).  I don&#8217;t blame Microsoft for having a bug in their code since all software has bugs, but when I found out that Microsoft does not plan to fix the issue, I suddenly realized that the team had hit a brick wall.  We had already made a big committment to LINQ to SQL.  Now there is a bug that Microsoft won&#8217;t fix, but they also won&#8217;t let anyone else fix it!  If NHibernate had been chosen then the worst case scenario is that you have to fix it yourself (though it is likely that someone else will fix it first).</p>
<p>The lesson: proprietary libraries are not the <em>safe</em> option.  The only way to ensure bugs will be fixed is if you can fix them yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://graemehill.ca/a-lesson-on-using-closed-source-libraries/feed</wfw:commentRss>
		</item>
		<item>
		<title>LINQ queries return queries not data</title>
		<link>http://graemehill.ca/linq-queries-return-queries-not-data</link>
		<comments>http://graemehill.ca/linq-queries-return-queries-not-data#comments</comments>
		<pubDate>Thu, 20 May 2010 03:15:01 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[LINQ]]></category>

		<category><![CDATA[VB.NET]]></category>

		<guid isPermaLink="false">http://graemehill.ca/?p=297</guid>
		<description><![CDATA[The title of this article is a pretty obvious statement, but it&#8217;s actually pretty easy to forget and it can lead to some painful bugs.  Here&#8217;s a code snippet whose output may seem surprising:

Module Module1
&#160;
    Sub Main()
        Dim query = 
    [...]]]></description>
			<content:encoded><![CDATA[<p>The title of this article is a pretty obvious statement, but it&#8217;s actually pretty easy to forget and it can lead to some painful bugs.  Here&#8217;s a code snippet whose output may seem surprising:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">Module Module1
&nbsp;
    <span style="color: #000080;">Sub</span> Main()
        <span style="color: #000080;">Dim</span> query = 
            From name <span style="color: #000080;">In</span> {&quot;one&quot;, &quot;two&quot;, &quot;three&quot;}
            <span style="color: #000080;">Select</span> <span style="color: #000080;">New</span> User(name)
&nbsp;
        <span style="color: #000080;">Dim</span> x = query.First
        <span style="color: #000080;">Dim</span> y = query.First
&nbsp;
        Console.WriteLine(x <span style="color: #000080;">Is</span> y)
&nbsp;
        Console.ReadKey()
    <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">End</span> Module
&nbsp;
<span style="color: #000080;">Public</span> Class User
    <span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> <span style="color: #000080;">New</span>(<span style="color: #000080;">ByVal</span> username <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>)
        Console.WriteLine(&quot;Creating user: &quot; &amp; username)
    <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
<span style="color: #000080;">End</span> Class</pre></div></div>

<p>At first glance it looks like <code>x</code> and <code>y</code> are the same object, but since <code>query</code> is just a query, not an actual collection, the result will be fetched independently each time you call <code>First</code>.  When the code is run, the console output looks like this:</p>
<pre>
Creating user: one
Creating user: one
False
</pre>
<p>This shows that the constructor was called twice for the same string, which explains why <code>x</code> and <code>y</code> are actually different objects.  In a case like this it is better to put <code>ToList</code> at the end of the query:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">        <span style="color: #000080;">Dim</span> users =
            (From name <span style="color: #000080;">In</span> {&quot;one&quot;, &quot;two&quot;, &quot;three&quot;}
             <span style="color: #000080;">Select</span> <span style="color: #000080;">New</span> User(name)).ToList()</pre></div></div>

<p>Bugs like this can be particularly problematic when you pass around an object of type <code>IEnumerable</code> and the programmer assumes that they are dealing with a collection, when they are really dealing with a query.  So&#8230; watch out!</p>
]]></content:encoded>
			<wfw:commentRss>http://graemehill.ca/linq-queries-return-queries-not-data/feed</wfw:commentRss>
		</item>
		<item>
		<title>Visual Basic 10 properties still lag behind C#</title>
		<link>http://graemehill.ca/visual-basic-10-properties-still-lag-behind-c</link>
		<comments>http://graemehill.ca/visual-basic-10-properties-still-lag-behind-c#comments</comments>
		<pubDate>Fri, 02 Apr 2010 19:14:38 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[VB.NET]]></category>

		<guid isPermaLink="false">http://graemehill.ca/?p=289</guid>
		<description><![CDATA[Up until Visual Studio 2010, simple property definitions were always ridiculously verbose.  For example:

    Private _title As String
    Public Property Title() As String
        Get
            Return _title
     [...]]]></description>
			<content:encoded><![CDATA[<p>Up until Visual Studio 2010, simple property definitions were always ridiculously verbose.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">    <span style="color: #000080;">Private</span> _title <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
    <span style="color: #000080;">Public</span> <span style="color: #000080;">Property</span> Title() <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
        <span style="color: #000080;">Get</span>
            Return _title
        <span style="color: #000080;">End</span> <span style="color: #000080;">Get</span>
        <span style="color: #000080;">Set</span>(<span style="color: #000080;">ByVal</span> value <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>)
            _title = value
        <span style="color: #000080;">End</span> <span style="color: #000080;">Set</span>
    <span style="color: #000080;">End</span> <span style="color: #000080;">Property</span></pre></div></div>

<p>That&#8217;s <em>9 lines</em> of code just to make a simple string property.  Luckily, in VB 10 we will be able to write these simple properties in one line:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">    <span style="color: #000080;">Public</span> <span style="color: #000080;">Property</span> Title <span style="color: #000080;">As</span> <span style="color: #000080;">String</span></pre></div></div>

<p>This is great, but what if things get a little more complicated?  Obviously this syntax just uses the default getter and setter, both of which have the same scope.  When I write my properties I like avoid using <code>ReadOnly</code> and instead make the setter private so that I can still encapsulate the setting code within the class.  With VB 10 you still have to define the property the old way in order to do this:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">    <span style="color: #000080;">Private</span> _title <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
    <span style="color: #000080;">Public</span> <span style="color: #000080;">Property</span> Title() <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
        <span style="color: #000080;">Get</span>
            Return _title
        <span style="color: #000080;">End</span> <span style="color: #000080;">Get</span>
        <span style="color: #000080;">Private</span> <span style="color: #000080;">Set</span>(<span style="color: #000080;">ByVal</span> value <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>)
            _title = value
        <span style="color: #000080;">End</span> <span style="color: #000080;">Set</span>
    <span style="color: #000080;">End</span> <span style="color: #000080;">Property</span></pre></div></div>

<p>But now it&#8217;s back to 9 lines just because I wanted the setter to be private.  In C#, programmers have the luxury of writing properties like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Title <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Furthermore, neither language allows you to use a default getter and custom setter (or vice versa) probably because it would make it difficult for them both to use the same backing store, but that&#8217;s a whole other tangent.  Overall, the new abbreviated syntax for properties in VB 10 is great for the simple cases, but you have to revert to the old, verbose method if you want to deviate from the default in even the slightest way.</p>
]]></content:encoded>
			<wfw:commentRss>http://graemehill.ca/visual-basic-10-properties-still-lag-behind-c/feed</wfw:commentRss>
		</item>
		<item>
		<title>LINQ to SQL Gotcha #6: Delete, Save, Insert, CRASH</title>
		<link>http://graemehill.ca/linq-to-sql-gotcha-6-delete-save-insert-crash</link>
		<comments>http://graemehill.ca/linq-to-sql-gotcha-6-delete-save-insert-crash#comments</comments>
		<pubDate>Fri, 02 Apr 2010 04:18:03 +0000</pubDate>
		<dc:creator>Graeme</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[LINQ]]></category>

		<category><![CDATA[LINQ To SQL]]></category>

		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://graemehill.ca/?p=273</guid>
		<description><![CDATA[If you keep the same entity around after it has been deleted and SubmitChanges() is called then you can run into an InvalidOperationException if you try to insert it again.

var data = new DataClasses1DataContext&#40;&#41;;
var user = new User&#40;&#41; &#123; userName = &#34;foo&#34;, password = &#34;bar&#34; &#125;;
&#160;
data.Users.InsertOnSubmit&#40;user&#41;;
data.SubmitChanges&#40;&#41;;
&#160;
data.Users.DeleteOnSubmit&#40;user&#41;;
data.SubmitChanges&#40;&#41;;
&#160;
data.Users.InsertOnSubmit&#40;user&#41;;
data.SubmitChanges&#40;&#41;;

Here you will actually get an exception on the second [...]]]></description>
			<content:encoded><![CDATA[<p>If you keep the same entity around after it has been deleted and <code>SubmitChanges()</code> is called then you can run into an <code>InvalidOperationException</code> if you try to insert it again.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataClasses1DataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var user <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> User<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> userName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;foo&quot;</span>, password <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;bar&quot;</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
data.<span style="color: #0000FF;">Users</span>.<span style="color: #0000FF;">InsertOnSubmit</span><span style="color: #000000;">&#40;</span>user<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
data.<span style="color: #0000FF;">SubmitChanges</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
data.<span style="color: #0000FF;">Users</span>.<span style="color: #0000FF;">DeleteOnSubmit</span><span style="color: #000000;">&#40;</span>user<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
data.<span style="color: #0000FF;">SubmitChanges</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
data.<span style="color: #0000FF;">Users</span>.<span style="color: #0000FF;">InsertOnSubmit</span><span style="color: #000000;">&#40;</span>user<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
data.<span style="color: #0000FF;">SubmitChanges</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Here you will actually get an exception on the second <code>InsertOnSubmit()</code> because the data context remembered the entity and will no longer allow you to attach it for some reason.  Once an entity has been deleted from a data context it can never go back.  To get around this you need to either insert the entity to a different data context or copy the data to a new instance of the same entity class and then insert it.  This has been confirmed <a href="http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6602af13-1d27-4f9a-b7e0-9c58238f6c55" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6602af13-1d27-4f9a-b7e0-9c58238f6c55');">here</a>.</p>
<p>Note: You are free to call <code>DeleteOnSubmit()</code> and then <code>InsertOnSubmit()</code> all you want as long as you never call <code>SubmitChanges()</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://graemehill.ca/linq-to-sql-gotcha-6-delete-save-insert-crash/feed</wfw:commentRss>
		</item>
	</channel>
</rss>

