473,805 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1998
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.co m

"Samuel R. Neff" <in**********@n ewsgroup.nospam > wrote in message
news:mj******** *************** *********@4ax.c om...

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.c om> wrote:
Samuel,

I agree with your use of IDisposable. I don't agree with the
documentatio n 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.Rei nstateDb(); 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.SuppressFina lize(this);
// more code
}
~Foo() {
ForgottenDispos es.Global.Happe ned(this);
Dispose();
}
}

You can have ForgottenDispos es.Global.Happe ned 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
5803
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 include finalization syntax or force users to to explicit cleanup using IDisposable?
5
3190
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 = Cursors.Default;
2
343
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 destructor... is this whats going on? thanks
10
1595
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 Tag.Dispose() ' Free your own state (unmanaged objects). ' Set large fields to null. End Sub
4
2036
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 design-blunder, if not a 100-year sleep. It prevents
3
1749
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 lot of connections to the database and leaving them open until the GC decides to clean them. When the variable for the class goes out of scope is the database connection closed or will it remain ipen until the GC fires? This object may get...
6
3587
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. what is the correct way of implementing IDisposable.Dispose? 3. what is the preferred way, if there is one over the other, of calling the Dispose method on an object that implements IDisposable. Is it the way that uses the "using(object){}" construct...
12
2455
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 will distill it here. The core of his argument started with his statement: "There is no gain in performance, maintainability or otherwise by implementing the Dispose method if unmanaged resources are not involved." I focused particularly on...
47
2160
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 with them: a. Nuke 'em on a GC.Collect call (or equivalent) b. Call Dispose on the object. Yes, I understand that the compiler and the GC doesn't know what IDisposable
4
9702
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 released and the object is eligible for garbage collection when my class is disposed or its' finalizer is called by GC. My question is whether using this approach is even necessary? My feeling is yes because it ensures the member is explicitly...
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10607
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10104
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3843
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.