<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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>Comments on: IDisposable Part 3 &#8211; Encapsulating an IDisposable class</title>
	<atom:link href="http://reedcopsey.com/2009/04/19/idisposable-part-3-encapsulating-an-idisposable-class/feed/" rel="self" type="application/rss+xml" />
	<link>http://reedcopsey.com/2009/04/19/idisposable-part-3-encapsulating-an-idisposable-class/</link>
	<description>Thoughts on C#, WPF, .NET, and programming for Scientific Visualization</description>
	<lastBuildDate>Fri, 11 May 2012 16:07:35 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Reed</title>
		<link>http://reedcopsey.com/2009/04/19/idisposable-part-3-encapsulating-an-idisposable-class/#comment-2145</link>
		<dc:creator>Reed</dc:creator>
		<pubDate>Wed, 18 Apr 2012 08:55:36 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=27#comment-2145</guid>
		<description>&quot;From what I’ve read, it seems I can assume that the API object itself will be disposed of on its own since it is a manged resource. Would that be correct?&quot;

Yes.  If the API you&#039;re using is written properly, you don&#039;t need a finalizer.  The reason for this is that, as soon as your &quot;wrapper&quot; class would be finalized, any managed references (the internal API handle) are also eligible for finalization, and the IDisposable implementation there should handle the closing automatically.

Adding a finalizer in your class would really have no effect, and just decrease performance.

If, however, you are finding that the connection is not being closed because the internal implementation is written improperly, you could add a finalizer and close it explicitly - but this really shouldn&#039;t be required.

-Reed</description>
		<content:encoded><![CDATA[<p>&#8220;From what I’ve read, it seems I can assume that the API object itself will be disposed of on its own since it is a manged resource. Would that be correct?&#8221;</p>
<p>Yes.  If the API you&#8217;re using is written properly, you don&#8217;t need a finalizer.  The reason for this is that, as soon as your &#8220;wrapper&#8221; class would be finalized, any managed references (the internal API handle) are also eligible for finalization, and the IDisposable implementation there should handle the closing automatically.</p>
<p>Adding a finalizer in your class would really have no effect, and just decrease performance.</p>
<p>If, however, you are finding that the connection is not being closed because the internal implementation is written improperly, you could add a finalizer and close it explicitly &#8211; but this really shouldn&#8217;t be required.</p>
<p>-Reed</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JCDrumKing</title>
		<link>http://reedcopsey.com/2009/04/19/idisposable-part-3-encapsulating-an-idisposable-class/#comment-2138</link>
		<dc:creator>JCDrumKing</dc:creator>
		<pubDate>Tue, 10 Apr 2012 15:51:16 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=27#comment-2138</guid>
		<description>Reed,

Thank you very much for your commitment to the developer community.

I am still wrangling with what to do in my particular situation...
I have written a class that encapsulates API functionality for an external system.  Creating the connection is costly -- consistently takes 6 to 7 seconds to connect -- so I am creating the connection in the constructor and leaving it open for the object&#039;s brief lifetime (it is for a web application).  (During its lifetime, several methods are repetitively called, and performance would be horrible if I opened and closed the connection within each method.)

The API object does have a Disconnect() and Dispose() method and I am exposing my own Disconnect() method and implementing IDispose.  Good form would be for the user of my class to always Disconnect() and Dispose(), but of course there is no way to guarantee that will happen.

From what I&#039;ve read, it seems I can assume that the API object itself will be disposed of on its own since it is a manged resource.  Would that be correct?

More importantly, however, I do want to be sure that connection is always explicitly closed first.  Even though I have no unmanaged resources, is this a case where a destructor is definitely needed?  And I assume that I would check for and close the open connection regardless of whether Dispose(bool) was called with True or False?

If you have sample code that illustrates this scenario, that would be awesome:
1.  Class (can be sealed if that helps) with a private class-level variable that contains an open connection to something (connection made in the constructor).
2.  The Class exposes Disconnect() and implements IDispose.
3.  Code is in place to reliably ensure that the connection will be closed if the user of the class neglected to do so.

Another thing I wonder is, do I need to implement IDispose at all?  Is it dangerous/stupid to assume that the API object is smart enough to internally close the connection itself when it is disposed/finalized?

Thank you again!</description>
		<content:encoded><![CDATA[<p>Reed,</p>
<p>Thank you very much for your commitment to the developer community.</p>
<p>I am still wrangling with what to do in my particular situation&#8230;<br />
I have written a class that encapsulates API functionality for an external system.  Creating the connection is costly &#8212; consistently takes 6 to 7 seconds to connect &#8212; so I am creating the connection in the constructor and leaving it open for the object&#8217;s brief lifetime (it is for a web application).  (During its lifetime, several methods are repetitively called, and performance would be horrible if I opened and closed the connection within each method.)</p>
<p>The API object does have a Disconnect() and Dispose() method and I am exposing my own Disconnect() method and implementing IDispose.  Good form would be for the user of my class to always Disconnect() and Dispose(), but of course there is no way to guarantee that will happen.</p>
<p>From what I&#8217;ve read, it seems I can assume that the API object itself will be disposed of on its own since it is a manged resource.  Would that be correct?</p>
<p>More importantly, however, I do want to be sure that connection is always explicitly closed first.  Even though I have no unmanaged resources, is this a case where a destructor is definitely needed?  And I assume that I would check for and close the open connection regardless of whether Dispose(bool) was called with True or False?</p>
<p>If you have sample code that illustrates this scenario, that would be awesome:<br />
1.  Class (can be sealed if that helps) with a private class-level variable that contains an open connection to something (connection made in the constructor).<br />
2.  The Class exposes Disconnect() and implements IDispose.<br />
3.  Code is in place to reliably ensure that the connection will be closed if the user of the class neglected to do so.</p>
<p>Another thing I wonder is, do I need to implement IDispose at all?  Is it dangerous/stupid to assume that the API object is smart enough to internally close the connection itself when it is disposed/finalized?</p>
<p>Thank you again!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reed</title>
		<link>http://reedcopsey.com/2009/04/19/idisposable-part-3-encapsulating-an-idisposable-class/#comment-491</link>
		<dc:creator>Reed</dc:creator>
		<pubDate>Fri, 08 Jan 2010 02:25:49 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=27#comment-491</guid>
		<description>Zamesking: 

Thanks for the feedback!

I&#039;m glad the post is helping.   The series on IDisposable was probably my favorite, up until my new MVVM one ;)  Both are trying to tackle a subject that&#039;s been talked about a lot, but take it on from a slightly different angle, in a bit more depth.  I hope you find them useful.

-Reed</description>
		<content:encoded><![CDATA[<p>Zamesking: </p>
<p>Thanks for the feedback!</p>
<p>I&#8217;m glad the post is helping.   The series on IDisposable was probably my favorite, up until my new MVVM one <img src='http://reedcopsey.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />   Both are trying to tackle a subject that&#8217;s been talked about a lot, but take it on from a slightly different angle, in a bit more depth.  I hope you find them useful.</p>
<p>-Reed</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zamesking</title>
		<link>http://reedcopsey.com/2009/04/19/idisposable-part-3-encapsulating-an-idisposable-class/#comment-490</link>
		<dc:creator>zamesking</dc:creator>
		<pubDate>Fri, 08 Jan 2010 01:41:12 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=27#comment-490</guid>
		<description>Reed,

Thanks for explaining it clearly, I just forgot the purpose of the IDisposable interface. Actually, here AFactory class implements IDisposable interface to make it can be release it&#039;s managed resources explicitly.

Keep your good work, I decide to go through all your MVVM articles. Actually, I get to know you in msdn forums, and find you can always explaining tech in clear words, and your suggestions in the blogs are very helpful.</description>
		<content:encoded><![CDATA[<p>Reed,</p>
<p>Thanks for explaining it clearly, I just forgot the purpose of the IDisposable interface. Actually, here AFactory class implements IDisposable interface to make it can be release it&#8217;s managed resources explicitly.</p>
<p>Keep your good work, I decide to go through all your MVVM articles. Actually, I get to know you in msdn forums, and find you can always explaining tech in clear words, and your suggestions in the blogs are very helpful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reed</title>
		<link>http://reedcopsey.com/2009/04/19/idisposable-part-3-encapsulating-an-idisposable-class/#comment-460</link>
		<dc:creator>Reed</dc:creator>
		<pubDate>Wed, 06 Jan 2010 01:01:05 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=27#comment-460</guid>
		<description>Zamesking:

It won&#039;t be a problem.  If AFactory is every a candidate for finalization, that means that all of its private managed resources (in this case, LicenseGenerator) will also be candidates for finalization.  When LicenseGenerator&#039;s finalizer runs, it will dispose of the unmanaged resource.  There is no need for an explicit finalizer in AFactory, only in LicenseGenerator.

This is why, when encapsulating an IDisposable, you don&#039;t need a finalizer - even though you still want to be IDisposable, and why the pattern is differnet in this case.  If you directly contain a native resource, you need a finalizer - otherwise, you typically do not need one.</description>
		<content:encoded><![CDATA[<p>Zamesking:</p>
<p>It won&#8217;t be a problem.  If AFactory is every a candidate for finalization, that means that all of its private managed resources (in this case, LicenseGenerator) will also be candidates for finalization.  When LicenseGenerator&#8217;s finalizer runs, it will dispose of the unmanaged resource.  There is no need for an explicit finalizer in AFactory, only in LicenseGenerator.</p>
<p>This is why, when encapsulating an IDisposable, you don&#8217;t need a finalizer &#8211; even though you still want to be IDisposable, and why the pattern is differnet in this case.  If you directly contain a native resource, you need a finalizer &#8211; otherwise, you typically do not need one.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zamesking</title>
		<link>http://reedcopsey.com/2009/04/19/idisposable-part-3-encapsulating-an-idisposable-class/#comment-459</link>
		<dc:creator>zamesking</dc:creator>
		<pubDate>Wed, 06 Jan 2010 00:47:46 +0000</pubDate>
		<guid isPermaLink="false">http://reedcopsey.com/?p=27#comment-459</guid>
		<description>Hi reed,

In this case, if the user forgets to call the dispose method of AFactory, unmanaged resources of LicenseGenerator will not be released. While in pattern1 and partter2. the finalizer will call dispose(false) to dispose unmanned resource when they forget to. Will this be a problem?</description>
		<content:encoded><![CDATA[<p>Hi reed,</p>
<p>In this case, if the user forgets to call the dispose method of AFactory, unmanaged resources of LicenseGenerator will not be released. While in pattern1 and partter2. the finalizer will call dispose(false) to dispose unmanned resource when they forget to. Will this be a problem?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
 
