<?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>out &#62;&#62; m_Conscientia; &#187; IDisposable</title>
	<atom:link href="http://blog.hypercomplex.co.uk/index.php/tag/idisposable/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hypercomplex.co.uk</link>
	<description>a multidimensional braindump</description>
	<lastBuildDate>Mon, 02 Aug 2010 22:30:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Should Dispose throw exceptions?</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/11/should-dispose-throw-exceptions/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/11/should-dispose-throw-exceptions/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 23:47:51 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[IDisposable]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=560</guid>
		<description><![CDATA[The Framework Design Guidelines suggest that you should avoid throwing an exception from Dispose. This page has a better quote: Avoid throwing an exception from within Dispose except under critical situations where the containing process has been corrupted (leaks, inconsistent shared state, etc.). I can think of two good reasons to avoid throwing (depending on [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://my.safaribooksonline.com/9780321545671">Framework Design Guidelines</a> suggest that you should <em>avoid</em> throwing an exception from Dispose. <a href="http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae">This</a> page has a better quote: </p>
<p><em>Avoid throwing an exception from within Dispose except under critical situations where the containing process has been corrupted (leaks, inconsistent shared state, etc.).</em></p>
<p>I can think of two good reasons to avoid throwing (depending on your use case they may or may not represent the desired behaviour):</p>
<ul>
<li> If there is an unhandled exception in the finaliser thread, under .NET 2.0 and above, the <a href="http://msdn.microsoft.com/en-us/library/ms228965.aspx">process terminates</a>. Yikes. As MSDN <a href="http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx">states</a>, IDisposable classes should call Dispose from the finaliser to be sure Dispose is always called. </li>
<li> Exception masking: If dispose is triggered by a using block after an exception is thrown inside the block, then Dispose throws, you will get the exception from dispose, not the original.</li>
</ul>
<p>Rogue Dispose methods pop up in the .NET framework (e.g. in WCF), and in other <a href="http://blog.cornetdesign.com/2009/06/exceptions-in-constructors-how-reflection-helped-workaround-an-oracle-bug/">libraries</a>. If you have to contend with some dangerous disposal, Marc Gravell has a safe <a href="http://marcgravell.blogspot.com/2008/11/dontdontuse-using.html">IDisposable wrapper</a> template/extension method pattern. This could easily be modified to do some logging rather than silently swallowing.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/11/should-dispose-throw-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.NET Framework Interfaces</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/07/net-framework-interfaces/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/07/net-framework-interfaces/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:30:33 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Application Development Foundation]]></category>
		<category><![CDATA[IDisposable]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=149</guid>
		<description><![CDATA[Interfaces define a common set of functionality which all classes that implement the interface must provide. For the purposes of the training course, six framework interfaces are relevant: IDisposable Free up (primarily unmanaged) resources which a class instance is responsible for. This article describes how to correctly implement IDisposable. IConvertable Convert the implementer’s value into [...]]]></description>
			<content:encoded><![CDATA[<p>Interfaces define a common set of functionality which all classes that implement the interface must provide. For the purposes of the training course, six framework interfaces are relevant:</p>
<table border="0">
<tr>
<td VALIGN=TOP><a href="http://msdn.microsoft.com/en-us/library/system.idisposable.aspx ">IDisposable</a></td>
<td>Free up (primarily unmanaged) resources which a class instance is responsible for. This <a href="http://msdn.microsoft.com/en-us/magazine/cc163392.aspx">article</a> describes how to correctly implement IDisposable.</td>
</tr>
<tr>
<td><a href="http://msdn.microsoft.com/en-us/library/system.iconvertible(VS.71).aspx ">IConvertable</a></td>
<td>Convert the implementer’s value into a CLR type</td>
</tr>
<tr>
<td VALIGN=TOP><a href="http://msdn.microsoft.com/en-us/library/system.icloneable.aspx">ICloneable</a></td>
<td>Create a new instance of a class based on an existing one. This is now deprecated because it was unclear whether it meant deep or shallow copy, as suggested by Eric Lippert <a href="http://stackoverflow.com/questions/1031023/copy-a-class-c ">here</a>. </td>
</tr>
<tr>
<td VALIGN=TOP><a href="http://msdn.microsoft.com/en-us/library/system.icomparable.aspx ">IComparable</a></td>
<td>Implemented by types which can be sorted based on a single method, CompareTo. This determines whether objects are greater or less than each other.</td>
</tr>
<tr>
<td><a href="http://msdn.microsoft.com/en-us/library/ms131187.aspx ">IEquatable</a></td>
<td>Determine whether implementing class instances are equal</td>
</tr>
<tr>
<td><a href="http://msdn.microsoft.com/en-us/library/system.iformattable.aspx ">IFormattable</a></td>
<td>Convert the implementer’s value into a formatted string. Useful in conjunction with <a href="http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx ">System.Globalization.CultureInfo</a></td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/07/net-framework-interfaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When dependency injection meets IDisposable</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/06/when-dependency-injection-meets-idisposable/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/06/when-dependency-injection-meets-idisposable/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 21:50:58 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[IDisposable]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=500</guid>
		<description><![CDATA[What follows is a solution which should really have been obvious to me the first time I faced the problem: how do you decouple logic and instantiation of collaborating objects when faced with an IDisposable class? Consider this: public class NiceClass &#123; private DependentClass injectedDependency; &#160; public NiceClass&#40;DependentClass injectedDependency&#41; &#123; this.injectedDependency = injectedDependency; &#125; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>What follows is a solution which should really have been obvious to me the first time I faced the problem: how do you decouple logic and instantiation of collaborating objects when faced with an IDisposable class? Consider this:</p>

<div class="wp_codebox"><table><tr id="p5004"><td class="code" id="p500code4"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NiceClass
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> DependentClass injectedDependency<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NiceClass<span style="color: #008000;">&#40;</span>DependentClass injectedDependency<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">injectedDependency</span> <span style="color: #008000;">=</span> injectedDependency<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CoupledMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IFace c <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Face<span style="color: #008000;">&#40;</span>injectedDependency<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// do something with c</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>We can see that injectedDependency is not coupled, but what about the local instance of Face in CoupledMethod? To decouple it use a delegate to return the IFace instance instead of calling new directly. If the coupling had been to a concrete type (Face), we could refactor to an abstract type (IFace) or wrapper first.</p>

<div class="wp_codebox"><table><tr id="p5005"><td class="code" id="p500code5"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NiceClass
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">delegate</span> IFace IFaceCreatorDelegate<span style="color: #008000;">&#40;</span>DependentClass injectedDependency<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> DependentClass injectedDependency<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IFaceCreatorDelegate iFaceCreator<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NiceClass<span style="color: #008000;">&#40;</span>DependentClass injectedDependency, IFaceCreatorDelegate iFaceCreator<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">injectedDependency</span> <span style="color: #008000;">=</span> injectedDependency<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">iFaceCreator</span> <span style="color: #008000;">=</span> iFaceCreator<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CoupledMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IFace c <span style="color: #008000;">=</span> iFaceCreator<span style="color: #008000;">&#40;</span>injectedDependency<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// do something with c</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This is an additional three lines of code, which arguably makes the control flow harder to read statically. It does, however, improve testability. I think of this as equivalent to the classic tradeoff between <a href="http://www.threeriversinstitute.org/AbstractVsConcreteParameters.html">abstract and concrete parameters</a>.</p>
<p>Instantiating NiceClass is not too arduous either with a lambda expression (although it is still a little verbose).</p>

<div class="wp_codebox"><table><tr id="p5006"><td class="code" id="p500code6"><pre class="csharp" style="font-family:monospace;">NiceClass nice <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> NiceClass<span style="color: #008000;">&#40;</span>
   dependency, 
   <span style="color: #008000;">&#40;</span>DependentClass d<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Face<span style="color: #008000;">&#40;</span>d<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/06/when-dependency-injection-meets-idisposable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
