[CONNECT .NET] Correctly using IDisposableEnumerable<DisplayRuleSet>

DisplayRulesManager.GetDisplayRuleSetsInFile(DgnFile) returns a list of IDisposableEnumerable<DisplayRuleSet>.  Class DisplayRuleSet implements IDisposable.

Note that IDisposableEnumerable is part of the MstnPlatformNET API.  It's not part of Microsoft .NET.

So, we have a disposable collection of disposable objects. Does the outer disposable class (IDisposableEnumerable) automatically dispose its members (DisplayRuleSet), or should we do that manually (or via an extension method, as suggested here)?

  • Hi John,

    it's my guess only, based on quick check of how the code is implemented... ;-)

    returns a list of IDisposableEnumerable<DisplayRuleSet>.  Class DisplayRuleSet implements IDisposable.

    You did not mention complete IDisposableEnumerable interface definition, which is public interface IDisposableEnumerable<T> : IEnumerable<T>, IDisposable where T: IDisposable.

    So, when GetDisplayRuleSetsInFile returns the list of DisplayRuleSet, this class must implement IDisposable, because required by the interface definition.

    Does the outer disposable class (IDisposableEnumerable) automatically dispose its members (DisplayRuleSet)

    Yes, it's done automatically. A representation of this interface isDisposableList<T>, used by GetDisplayRuleSetsInFile method internally. And when you check DisposableList<T> implementation, it disposes all list members in its destructor.

    So, I think the only thing you should dispose only the "outter class".

    or should we do that manually (or via an extension method, as suggested here)?

    In my opinion, when you will need something like described in the article (even when the approach is interesting), class is not implemented correctly.

    Also, be aware that when IEnumerable<T> instance is iterated using forech (or accessed with LINQ), items disposal is done automatically, because compiler takes care to produce necessary code.

    Regards,

      Jan