<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://bloggingabout.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'design', 'disposing', and 'dependancy injection'</title><link>http://bloggingabout.net/search/SearchResults.aspx?a=1&amp;o=DateDescending&amp;tag=design,disposing,dependancy+injection&amp;orTags=0</link><description>Search results matching tags 'design', 'disposing', and 'dependancy injection'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Injected objects should not be disposed be the class that receives the instance</title><link>http://bloggingabout.net/blogs/ramon/archive/2008/02/28/injected-objects-should-not-be-disposed-be-the-class-that-receives-the-instance.aspx</link><pubDate>Thu, 28 Feb 2008 09:05:00 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:457971</guid><dc:creator>Ramon Smits</dc:creator><description>&lt;p&gt;I just read &lt;a href="http://hammett.castleproject.org/?p=252"&gt;Windsor: Component Burden described as a (bad) side effect&lt;/a&gt; but this is not how it should work.&lt;/p&gt;&lt;p&gt;&lt;u&gt;Disposable objects should be cleanup by its creator. This is the easiest way to define the disposing responsibility!&lt;/u&gt;&lt;/p&gt;&lt;p&gt;In the mentioned article they have a situation where&amp;nbsp; A injects X into B at construction and then worry that B doesn&amp;#39;t dispose X. Well the reason for that is that B shouldn&amp;#39;t. A should dispose X when it knows for sure that X will not be used anymore. Either at the end of the application of when it knows that B is already garbage collected.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Lets start with an example by first defining X:&lt;/b&gt;&lt;/p&gt;&lt;span id="ctl00_ContentPlaceHolder1_output"&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;interface&lt;/span&gt; IX{&lt;br /&gt;    &lt;span class="rem"&gt;//...&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; X : IX, IDisposable{&lt;br /&gt;    &lt;span class="rem"&gt;//...&lt;/span&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;p&gt;&lt;b&gt;Example where A is responsible for disposing X:&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;span id="ctl00_ContentPlaceHolder1_output"&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; A{&lt;br /&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; Test()    {&lt;br /&gt;        &lt;span class="kwrd"&gt;using&lt;/span&gt;(IDisposable x = &lt;span class="kwrd"&gt;new&lt;/span&gt; X()){&lt;br /&gt;            &lt;span class="kwrd"&gt;object&lt;/span&gt; b = &lt;span class="kwrd"&gt;new&lt;/span&gt; B(x);&lt;br /&gt;            &lt;span class="rem"&gt;//... doing cool stuff with b&lt;/span&gt;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; B{&lt;br /&gt;    IX _x;&lt;br /&gt;    &lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; B(IX x)    {&lt;br /&gt;        _x = x;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    &lt;span class="rem"&gt;//... The cool methods of B&lt;/span&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Example where B is responsible for disposing X:&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;span id="ctl00_ContentPlaceHolder1_output"&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; A : IFactory{&lt;br /&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; Create&amp;lt;T&amp;gt;();&lt;br /&gt;    &lt;br /&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; Test()    {&lt;br /&gt;        &lt;span class="kwrd"&gt;object&lt;/span&gt; b = &lt;span class="kwrd"&gt;new&lt;/span&gt; B(&lt;span class="kwrd"&gt;this&lt;/span&gt;);&lt;br /&gt;        &lt;span class="rem"&gt;//... doing cool stuff with b&lt;/span&gt;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; B : IDisposable{&lt;br /&gt;    IX _x;&lt;br /&gt;    &lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; C(IFactory factory)    {&lt;br /&gt;        _x = factory.Create&amp;lt;IX&amp;gt;();&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Dispose()    {&lt;br /&gt;        IDisposable d = _x &lt;span class="kwrd"&gt;as&lt;/span&gt; IDisposable;&lt;br /&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt;(d==&lt;span class="kwrd"&gt;null&lt;/span&gt;) d.Dispose();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;In the example above you clearly see who&amp;#39;s responsible for disposing X. It&amp;#39;s creator cleansup and this is how it should be done everywhere in my humble opinion. If implementation is doing it in a different way then refactor your code asap to make it more readable and more maintainable. &lt;/p&gt;&lt;p&gt;Ofcourse these simple examples are not like the component architecture of windsor but the solution they describe there smells. If class A injects X into B at construction and B implements IDisposable then class B isn&amp;#39;t responsible for disposing X. A should do the cleanup so must know when X can be safely disposed. This implies that A should know something about B&amp;#39;s lifetime.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Either dispose X when the application domain shutsdown; or&lt;/li&gt;&lt;li&gt;Knows when A is garbage colllected by checking for example its weak reference; or&lt;/li&gt;&lt;li&gt;Subclassing B at runtime to implement a callback or demanding B to implement a custom interface so A can subscribe to a dispose event. &lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;</description></item></channel></rss>