Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ostensibly, this approach makes sense when you cannot make the disposal thread-safe (for example when talking to COM objects, which MUST be released on the same thread). However, in practice, you would find that this approach leads to objects living longer than they should.</p> <p>I would strive to make the disposal thread safe, that way you can call <code>Dispose</code> from the finalizer and achieve truly automatic lifetime management. You have to be careful as might not be appropriate for some types of resources - like file or network handles, which might need tighter control. Otherwise, this is the <strong>best</strong> solution to this problem.</p> <p>If the disposal HAS to be on the same thread then you are in a bit of a pickle. If the object needing disposing is in the model layer (as in business rules object) - it's quite likely it has layers of UI on top of, needing complex logic to dispose it (like events after a window is closed). It's a lose/lose situation and a choice between objects living forever (like your original solution) and complex disposal logic (which turns ugly pretty quickly).</p> <p>Perhaps something to experiment with. You could separate the disposable resource into its own class and have the factory maintain a strong reference to it. The resource object maintains a weak reference back to business object. When the business object is finalized, <code>WeakReference</code> will return <code>null</code>, thus enabling you to make pass over the disposables and dump the ones that are no longer needed.</p> <pre><code>public class Global { private static readonly List&lt;Resource&gt; Disposables = new List&lt;Resource&gt;(); public HeavyLifter GetHeavyLifter() { var resource = new HeavyLifterResource(); var heavyLifter = new HeavyLifter(resource); resource.BusinessObject = heavyLifter; Disposables.Add(resource); } public void DisposeAll() { Disposables.ForEach(d =&gt; d.CleanUp()); } } public abstract class Resource : IDisposable { WeakReference&lt;object&gt; m_BusinessObject; public WeakReference&lt;object&gt; BusinessObject {get;set;} public CleanUp() { if (!m_BusinessObject.IsAlive) Dispose(); } } public HeavyLifter { public HeavyLifter (Disposable d) { m_resourceObj = d; } HeavyLifterResource m_resourceObj; } public class HeavyLifterResource :Resource { public void Dispose() { //disposal } } </code></pre> <p>Let me again reiterate that the above approach is only appropriate for a certain class of objects. You wouldn't want to handle your network connections in such fuzzy way, but you can, for example, do this for business objects that need a network connection to dispose themselves. Even then, this is only appropriate when such connection is persistent through the lifetime of the application.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload