473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Use IDisposable for general clean-up code?


I'd like some opinions on whether or not to use IDisposable for
classes that require clean-up but when the clean-up is not related to
unmanaged resources or other disposable objects.

The most descriptive case is unit test data. We have unit test data
classes that generate fake data in the database which we can then test
against, and then the unit test data classes delete those records.

We can have all of these test data classes implement IDisposable and
the Dispose method can clean up the data (not during garbage
collection of course..only when someone actually calls Dispose).

An alternative viewpoint that's been suggested is that this is an
abuse of IDisposable and we instead should just have a CleanUp method
or something similar that cleans stuff up and all developers have to
know to call CleanUp.

What are pro's and con's of each approach? Are there any established
standards of what not to use IDisposable for?

Thanks,

Sam
Nov 17 '05 #1
5 1974
Samuel,

I agree with your use of IDisposable. I don't agree with the
documentation regarding IDisposable, that it is only for managed resources.
The way I see it, it is used for whenever you need deterministic
finalization semantics.

Additionally, you have language support for doing this. To not use
IDisposable would be a huge burden on the coder.

However, I will say this, unless you have some other reason not to do
this, you might want to run your test cases in a transaction and abort the
transaction when the test is complete. This way, you don't have to delete
your fake data, it's done automatically.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Samuel R. Neff" <in**********@newsgroup.nospam> wrote in message
news:mj********************************@4ax.com...

I'd like some opinions on whether or not to use IDisposable for
classes that require clean-up but when the clean-up is not related to
unmanaged resources or other disposable objects.

The most descriptive case is unit test data. We have unit test data
classes that generate fake data in the database which we can then test
against, and then the unit test data classes delete those records.

We can have all of these test data classes implement IDisposable and
the Dispose method can clean up the data (not during garbage
collection of course..only when someone actually calls Dispose).

An alternative viewpoint that's been suggested is that this is an
abuse of IDisposable and we instead should just have a CleanUp method
or something similar that cleans stuff up and all developers have to
know to call CleanUp.

What are pro's and con's of each approach? Are there any established
standards of what not to use IDisposable for?

Thanks,

Sam

Nov 17 '05 #2

We don't abort the transaction because left-over test data is a sign
of a bad test (or insufficient tests). If we don't properly clean up
the data, then there is a problem with the test or class being tested
that has to be corrected. All test data we generate is tagged as test
data so we can find it later. We've found bugs in both our tests and
in our real code that only manifested themselves as left-over test
data.

Thanks,

Sam
On Tue, 16 Aug 2005 11:51:14 -0400, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
Samuel,

I agree with your use of IDisposable. I don't agree with the
documentation regarding IDisposable, that it is only for managed resources.
The way I see it, it is used for whenever you need deterministic
finalization semantics.

Additionally, you have language support for doing this. To not use
IDisposable would be a huge burden on the coder.

However, I will say this, unless you have some other reason not to do
this, you might want to run your test cases in a transaction and abort the
transaction when the test is complete. This way, you don't have to delete
your fake data, it's done automatically.

Hope this helps.


Nov 17 '05 #3
>> The most descriptive case is unit test data. We have unit test data
classes that generate fake data in the database which we can then test
against, and then the unit test data classes delete those records. <<

Not related to your IDisposable question but unit testing. What we do
is in the tests' setup, we have a helper class that brings the database
to a known state. This either involves:
1-)Dropping and recreating all database objects and setting up the
known test data, however, it is very time consuming as this needs to be
done for every test
2-)Or, Setup the know test data which requires any previous data to be
deleted.

Number 2 is much quicker because it doesn't require dropping all the
tables, stored procs, views etc...Basically all it does is runs a
script that deletes all data in the database, and then runs a separate
script that adds all the known test data in the entire database. This
is all encapsulated in a helper class, thus all we need to do is
something like: HelperClass.ReinstateDb(); and the database is ready.

HTH.

Nov 17 '05 #4
Hi Sam,

I agree with the concept you have on IDisposable. But cleaning up the test
data is not done by IDisposable. Since you have tags on your test data, you
can clean them later with other codes that searches the database for test
data.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #5


Samuel R. Neff wrote:
An alternative viewpoint that's been suggested is that this is an
abuse of IDisposable and we instead should just have a CleanUp method
or something similar that cleans stuff up and all developers have to
know to call CleanUp.
You can "check" for forgotten Dispose() by declaring a destructor:

public class Foo: IDisposable {
public void Dispose() {
GC.SuppressFinalize(this);
// more code
}
~Foo() {
ForgottenDisposes.Global.Happened(this);
Dispose();
}
}

You can have ForgottenDisposes.Global.Happened throw an exception, that
will break the debugger at the offending spot.

You can selectively change what Happend does, throw, log and ignore
comes to mind as valid options, depending on whether a debugger is
attached and possibly some configuration.

If the call to Happened returns, your application will *try* to
Dispose() the data-structure at garbage-collection-time, even though the
programmer forgot.
What are pro's and con's of each approach? Are there any established
standards of what not to use IDisposable for?


IDisposable is a good choice for places where the code, not the GC,
should decide when to clean up.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jim | last post by:
Why was a destructor (finalizer) included in the syntax of C# if implementing IDisposable is the sure way to release resources held by a class? If you were doing a language from scratch would you...
5
by: Robert Heuvel | last post by:
Hi, this is what I did: public struct SWaitCursor:IDisposable { public SWaitCursor (int i) { Cursor.Current = Cursors.WaitCursor; } void System.IDisposable.Dispose() { Cursor.Current =...
2
by: Brian Henry | last post by:
when you make a class implement IDisposable, is that basicly the same thing as a destructor implementation in C++? it seems ot just add a dispose method to a class, which in C++ that'd be a...
10
by: Dennis | last post by:
VB.Net Documentation for implementing IDisposable has: Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean) If disposing Then ' Free other state (managed objects). End If...
4
by: Helge Jensen | last post by:
In C# 2.0 System.IO.Stream is declared as: public class Stream: ..., IDisposable { ... public void Dispose(); public void Dispose(bool); IDisposable.Dispose(); } Which must be a...
3
by: karl | last post by:
I have created a class that connects to a SQL database when it is created. This is a maintenance application that re-creates this class at regular intervals. I am worried that I will be creating a...
6
by: Water Cooler v2 | last post by:
I heard from someone that we must not implement IDisposable for all classes. Can someone please tell me: 1. the reason why we must not implement IDisposable for all the classes we write. 2....
12
by: Cordell Lawrence \(News Group\) | last post by:
There an ongoing discussion between a colleague and myself about the usefulness of the IDisposable pattern beyond the reclamation of unmanaged resources. The discussion is somewhat lengthy so I...
47
by: Hilton | last post by:
Hi, I'm sure I'm simplifying things here, but how about if the GC did this to objects that implement IDisposable: 1. Always Generation 1 (I think that is the correct name) 2. Get aggressive...
4
by: cgarcia0117 | last post by:
For any class I write in C# that has a member variable that implements IDisposable my class implements the IDisposable pattern. I do this to guarantee the reference to the member is explicitly...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.