views:

106

answers:

4

I'm looking at some vb.net code I just inherited, and cannot fathom why the original developer would do this.

Basically, each "Domain" class is a collection of properties. And each one implements IDisposable.Dispose, and overrides Finalize(). There is no base class, so each just extents Object.

Dispose sets each private var to Nothing, or calls _private.Dispose when the property is another domain object. There's a private var that tracks the disposed state, and the final thing in Dispose is GC.suppressFinalize(Me)

Finalize just calls Me.Dispose and MyBase.Finalize.

Is there any benefit to this? Any harm? There are no un-managed resources, no db connections, nothing that would seem to need this.

A: 

It would seem to me that this is something that is NOT needed at all, especially without un-managed resources and data connections.

If you happen to be able to sanitize and post the code we might be able to get a bit more insight, but realistically I can't see a need for it.

Mitchel Sellers
+3  A: 

That strikes me as being a VB6 pattern.

I would bet the guy was coming straight from VB6, maybe in the earlier days of .NET when these patterns were not widely understood.

There also is one case were setting an nternal reference to nothing is useful in a call to Dispose: when the member is marked as Withevents. Without that, you risk having an uncollected object handling events when it really should not be doing that anymore.

Denis Troller
+1 for the withevents mention
Randolpho
also for the "early days of .Net" mention: there's a lot of old bad code out there that isn't really the fault of the developer: just weak communication of how things fit together.
Joel Coehoorn
exactly my point. When .Net came out, most VB6 developpers struggled to know what to do with it. And the introduction of the GC and the Disposable pattern was one of those struggling points. A lot of investment was needed to understand what needed to be changed, and most people did not have access to that information, so they went with their usual patterns.
Denis Troller
A: 

Depending on the size of the objects, and how often they are created/destroyed, it could be to ensure GC can happen as early as possible.

It may be, that this pattern was used in other projects and it continues on without understanding why it was used in the first place. Monkey Gardeners

sfossen
A: 

The only reason that I could see for this -- and this is dubious at best -- is if these things are being created and disposed of higher in the "food chain" and there is a potential for some of these domain classes to have either a limited or unmanaged resource at some point.

Even that is sketchy...it sounds like someone came from an unmanaged background and was looking for the .NET equivalent to managing your memory and came across the IDisposable interface.

Adam Robinson