473,241 Members | 1,352 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,241 software developers and data experts.

Should my managed wrapper implement IDisposable?

Greetings,

I got a class that wraps the System.Data.SqlClient.SqlConnection class (no
COM interaction). I'm not sure if I'm supposed to implement the IDisposable
pattern for this wrapper or not. Since one of it's members (SqlConnection)
implements this interface, I'm thinking maybe I ought to. But on the other
hand, those unmanaged resources has already been wrapped in the
SqlConnection class...

If so, how would my Dispose method look like?
I'm not really sure if I should look at SqlConnection as a managed or
unmanaged resource...

--

Thanks

/Billy

Nov 15 '05 #1
2 4790
Only implement I disposable if you have resources which need to be
cleaned up after use. A popular one would be any open Streams, etc.

So in your wrapper, if you have things that need to be "cleaned" up,
then you'll want to implement IDisposable.

It's as easy as inheriting from it and creating the method

public void Dispose()
{
// do your cleanup here
}

It sounds like you should probably, since you're wrapping
SqlConnection. At the very minimum, have your dispose call your
SqlConnections's dispose function.

I'm not an expert on the topic, but those are my thoughts. Someone
else may have more sound reasoning.

On Fri, 5 Dec 2003 20:22:53 +0100, "Billy Porter" <bi***@xymox.com>
wrote:
Greetings,

I got a class that wraps the System.Data.SqlClient.SqlConnection class (no
COM interaction). I'm not sure if I'm supposed to implement the IDisposable
pattern for this wrapper or not. Since one of it's members (SqlConnection)
implements this interface, I'm thinking maybe I ought to. But on the other
hand, those unmanaged resources has already been wrapped in the
SqlConnection class...

If so, how would my Dispose method look like?
I'm not really sure if I should look at SqlConnection as a managed or
unmanaged resource...


Nov 15 '05 #2
If your class creates the SqlConnection (for example) only as needed and
makes sure it cleans it up in try...finally blocks , using(), etc. there is
no need for it to implement IDisposeable itself, since there isn't really
anything to clean up at that point.

On the other hand, if your class has a instantiation of the SqlConnection
scoped to the class for some reason then it needs to be IDisposeable so it
can clean that up.

The former approach is generally recommended anyway (for resource
conservation), so you should rarely have to implement IDisposeable.

<om***@yahoo.com> wrote in message
news:dh********************************@4ax.com...
Only implement I disposable if you have resources which need to be
cleaned up after use. A popular one would be any open Streams, etc.

So in your wrapper, if you have things that need to be "cleaned" up,
then you'll want to implement IDisposable.

It's as easy as inheriting from it and creating the method

public void Dispose()
{
// do your cleanup here
}

It sounds like you should probably, since you're wrapping
SqlConnection. At the very minimum, have your dispose call your
SqlConnections's dispose function.

I'm not an expert on the topic, but those are my thoughts. Someone
else may have more sound reasoning.

On Fri, 5 Dec 2003 20:22:53 +0100, "Billy Porter" <bi***@xymox.com>
wrote:
Greetings,

I got a class that wraps the System.Data.SqlClient.SqlConnection class (noCOM interaction). I'm not sure if I'm supposed to implement the IDisposablepattern for this wrapper or not. Since one of it's members (SqlConnection)implements this interface, I'm thinking maybe I ought to. But on the otherhand, those unmanaged resources has already been wrapped in the
SqlConnection class...

If so, how would my Dispose method look like?
I'm not really sure if I should look at SqlConnection as a managed or
unmanaged resource...

Nov 15 '05 #3

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

Similar topics

22
by: Alper AKCAYOZ | last post by:
Hello Esteemed Developers and Experts, I have been using Microsoft Visual C++ .NET for 1 year. During this time, I have searhed some topics over internets. Most of the topics about .NET is...
10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
4
by: 0to60 | last post by:
I'm trying to create a .dll with VS.NET 2003 Architect that contains a math computational component. I need the guts of the thing to be in native code, as performance is key for that part. But, I...
9
by: WithPit | last post by:
I am trying to create an Managed C++ Wrapper around an unmanaged library which contains C++ code. Some of the unmanaged methods returns an returntype which is of the abstract base type (for...
46
by: clintonG | last post by:
Documentation tells me how but not when, why or where... <%= Clinton Gallagher http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx
3
by: Mark | last post by:
If your class implements IDisposable, I was told that this increases the speed with which your class is garbage collected. Is this true? And if so, how much "time" does it save? Assume that we...
8
by: Varangian | last post by:
Hello, was wondering of how to dispose of managed resources? or referencing every member of a class to null will release resources...? http://www.marcclifton.com/tabid/79/Default.aspx...
9
by: =?Utf-8?B?RWR3YXJkUw==?= | last post by:
I would greatly appreciate some help on passing managed object into unmanaged code. I need to pass a reference (address of) of a managed class into unmanaged code (written by a thrid party). The...
2
by: Bob Altman | last post by:
Hi all, We have a native class modeled after the System::Exception class, and all exceptions that we throw derive from this class. For now this class is quite simple: just Description and...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.