5 Replies - 348 Views - Last Post: 23 July 2013 - 06:57 AM Rate Topic: -----

#1 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Question on using Idisposable...

Posted 22 July 2013 - 07:13 PM

If class A uses class B, and class B implements idispoable (as it holds unmanged resources). Does Class A need to implement Idisposable too? If so, than if class C uses Class A does that need to implement idisposable further?

This post has been edited by ray1234: 22 July 2013 - 07:22 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Question on using Idisposable...

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Question on using Idisposable...

Posted 23 July 2013 - 12:16 AM

If it uses the class, it should handle cleaning up the class. You can handle this in multiple ways. Examples are making the class implement IDisposible (the method you suggested) but this passes the responsibility to the user of the class. Another is implementing a destructor, but then you never know when (or if) it will be called.

This post has been edited by Momerath: 23 July 2013 - 12:17 AM

Was This Post Helpful? 0
  • +
  • -

#3 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,519
  • Joined: 08-April 09

Re: Question on using Idisposable...

Posted 23 July 2013 - 12:31 AM

If class B is implementing that interface it means it was defined somewhere else, possible in class A. If that is the case then no, class A doesn't need to implement that. Nowhere it is defined that every class my implement said interface it is up to you, OP to decide which class should implement .
Was This Post Helpful? 0
  • +
  • -

#4 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: Question on using Idisposable...

Posted 23 July 2013 - 04:54 AM

Classes that use unmanaged resources, or that hold references to managed objects that themselves use unmanaged resources, should always implement the IDisposable interface. The correct use of this interface ensures the efficient release of resources.

I found this from an article - http://www.blackwasp...Disposable.aspx


in my code. I'm actually doing this, where Class A is calling the dispose method of Class B that holds reference to Class C that uses Unmanaged resources, such as a memory stream. I made both class C and B to implment IDisposable, and let class A call dispose method on class B, so that it doesn't cause a ObjectDisposedException.

in this technique acceptable?

This post has been edited by ray1234: 23 July 2013 - 05:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,986
  • Joined: 05-May 12

Re: Question on using Idisposable...

Posted 23 July 2013 - 06:04 AM

Assuming that the dependence is:
A --> B --> C

Then you should implement IDisposable on A if you need the resources held by C to be released in a predictable manner. Notice that I said "should", rather than "must". If you don't have any need of that predictability, the you could simply let the destructor of A call Dispose() on B when A gets garbage collected.

Sometimes I find the "viral" effects of IDisposable as annoying as the "viral" effects of const correctness. For the sake of the Liskov substitution principle, if B or C were replaced with classes that needed the predictability, then the "should" becomes a "must". On the flip side, though, since the Liskov substitution principle stipulates that substituted types must have the same behavior, then you can't just blindly replace B or C with something that needs the predictability.

Based on the flavor questions you have asked recently, you seem to be using an IoC container. If your container supports object lifetime controls, then I would recommend implementing IDisposable on A anyway, and just let the IoC take care of cleaning up A when appropriate.
Was This Post Helpful? 2
  • +
  • -

#6 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: Question on using Idisposable...

Posted 23 July 2013 - 06:57 AM

Thanks Skydiver. I found that helpful.

I am using IOC container. Windsor to be exact. Becuase Class A will be a singleton. And will be disposed at the end of the app. I wouldn't want umanaged resources instances to remain when not referenced. Created on demand and removed. Thats why for class A i'm calling dispose method manually.

This post has been edited by ray1234: 23 July 2013 - 07:03 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1