IUpdater and IDisposable

IUpdater and IDisposable

Anonymous
Not applicable
722 Views
2 Replies
Message 1 of 3

IUpdater and IDisposable

Anonymous
Not applicable

A question of disposability:

 

It seems like this is a common pattern: (and one I inherited)

 

 

public class MyUpdater : IUpdater
    {
        private UpdaterId _updaterId;

        public MyUpdater (AddInId addinInId)
        {
            _updaterId = new UpdaterId(addinInId, new Guid(<myUpdaterGuid>));
        }
    }

 

which leaves an UpdaterId object in the class.  Ostensibly for the purposes of supplying an implentation of GetUpdaterId:

 

 

 

public UpdaterId GetUpdaterId()
        {
            return _updaterId;
        }

 

Noting that UpdaterId is defined in the Revit API thusly:

 

public class UpdaterId : IDisposable

 

 

 

1. Is there any reason that GetUpdaterId couldn't look like this:

public UpdaterId GetUpdaterId()
        {
            return new UpdaterId(addinInId, new Guid(<myUpdaterGuid>));
        }

 

 

 

 

2. If not, shouldn't MyUpdater, in addition to implementing IUpdater, (technically) also implement IDisposable, because UpdaterId implements IDisposable?

 and thus we can properly dispose of "_updaterId" when MyUpdater gets garbage collected?

 

I'm not completely clear on all these concepts beyond that if I implement IDisposable on a class then I need to provide Dispose() but when we've got a member variable which itself implements IDisposable, it gets murky for me.  I know this will all get GC'd at the end (when Revit closes) but I'm curious to know more about what's going on here and if there is something I should be doing more correctly.

 

Thanks for any thoughts.

0 Likes
Accepted solutions (1)
723 Views
2 Replies
Replies (2)
Message 2 of 3

arnostlobel
Alumni
Alumni
Accepted solution
Dear Ken,


it almost does not matter which of the two ways you choose. As long as the actual GUID of your updater is constant, it does not matter whether you instantiate UpdaterId just once or every time we ask about it. The benefit of the former method is that the object does not get allocated multiple times necessarily; However, Revit does not ask for it often anyway, thus it is not like you will loose a lot performance. I kind of prefer the first method, for one reason or another, but I would not frown at the other either.


You do not need to implement IDisposable unless you have something that the GC would not know how to dispose, e.g. database connections, native file and memory resources, etc, which I do not believe is a common case. With an updater just doing its model updating work via Revit API, your Dispose method would have nothing to explicitly expose.


Cheers


Arnost Löbel

Sr. Principal Developer

Autodesk Revit R&D
Arnošt Löbel
Message 3 of 3

Anonymous
Not applicable
Hi Arnošt ,
Thanks for the clarification on that. The "code analysis" brought it to my attention and so I naturally wondered about whether or not what I was doing was "correct". It's obviously working but as you might imagine, I'm still learning about the murkier regions of .NET. (see any number of heated discussions on the IDisposable topic on StackOverflow)

Cheers for the clarification and hope you're enjoying the snow!
-Ken
0 Likes