Dipose

J

Jason MacKenzie

I have a class that does some database acess. I'm pretty meticulous in
closing all connections when done with them and disposing of all objects
that have a dispose method .

Does this class need to have a dispose method of its own? I'm a little
unclear on this?

Thanks,

Jason MacKenzie
 
H

Herfried K. Wagner [MVP]

Jason MacKenzie said:
I have a class that does some database acess. I'm pretty meticulous in
closing all connections when done with them and disposing of all objects
that have a dispose method .

Does this class need to have a dispose method of its own? I'm a little
unclear on this?

You can implement the 'IDisposable' interface and close the connections in
the class' 'Dispose' method. This would make sense.
 
D

David Browne

Jason MacKenzie said:
I have a class that does some database acess. I'm pretty meticulous in
closing all connections when done with them and disposing of all objects
that have a dispose method .

Does this class need to have a dispose method of its own? I'm a little
unclear on this?


The class must implement IDisposable just in case it aggregates a disposable
object.

If your class has a member variable of type IDbConnection, then yes, it
should implement IDisposable and IDisposable.Dispose should invoke
IDbConnection.Dispose.

If, on the other hand, your class uses IDbConnections objects only as local
variables inside its methods, then no, you don't need to implement
IDisposable.


David
 
C

Cor Ligthert

Jason,
I have a class that does some database acess. I'm pretty meticulous in
closing all connections when done with them and disposing of all objects
that have a dispose method .

Because that it is the connection which does as well the connectionpooling
it is advised to close as often as possible your connections. However as one
of the exceptions in this case with myconnection.dispose. When you need them
again you create them new every time.

(This is an advice in situations with more than 100 open connections)

I see no reason why you would dispose all the other objects by the way.

Just my thought,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top