PetaPoco.Database implements IDisposable, so why don't most examples have a 'using' statement?
Asked Answered
G

5

7

The PetaPoco.Database object implements IDisposable but I rarely if ever see code samples (including on PetaPoco's own website) that include a using statement as follows:

using (var db = new Database("MyConnectionString"))
{
    // Do database stuff
}

Most often I simply see:

var db = new Database("MyConnectionString");
// Do database stuff
// I never see .Dispose() called.

How should PetaPoco.Database objects actually be handed?

Genevagenevan answered 30/10, 2015 at 20:28 Comment(5)
If it implements IDisposable you should be sure to call Dispose. And, in any case, you have nothing to loose if the Dispose method does nothingThespian
But if IDisposable is implemented then shouldn't I be putting it in a 'using' block instead, as this is typically how 'using' is meant to be used?Genevagenevan
@Genevagenevan It is and you should. they don't do it in examples because GC takes care of it when the AppDomain is destroyed with the program.Olivier
@Olivier - Thanks. But not sure why it wouldn't be shown in an example if it's the best practice to do it that way. The GC will of course always take care of everything when the AppDomain is destroyed, but the whole point of IDisposable is that you want to free those resources long before then. So I'm not sure I understand the logic there.Genevagenevan
@Genevagenevan I think You understand the logic better than they did. they're not gods.Olivier
M
3

As a simple rule: if a class implements IDisposable then go ahead and wrap it in a using. They may not actually be using any unmanaged resources, but it won't hurt and might future-proof you against changes.

Matchmark answered 2/11, 2015 at 22:14 Comment(1)
Could not agree more! However, some APIs use dispose for synax sugar, which when used in such a manner, should be well documented. I'm going to add a note to add this for PetaPoco.Imposition
I
3

PetaPoco maintainer here. Dispose is the same as calling CloseSharedConnection(). However, c# syntax only supports using(...) with IDisposable. For instance IDBConnection, from memory, supports both Close and Dispose.

Basically, it boils down to choice

Example 1

using(var db = new PetaPoco()) 
{
    // some code
}

Example 2

var db = new PetaPoco()

try 
{
    // code
}
finally 
{
    db.CloseSharedConnection();
}
Imposition answered 14/1, 2016 at 9:46 Comment(0)
O
0

It depends on what are you implementing. A Database object it's a connection to the database, you don't want to dispose the connection every time you execute a sql statement in the database because the performance will be terrible.

If you are implementing a web based solution, you typically use one Database object per request, but that request surely execute several sql statements in different filters, global filters and in several controller's methods, so you can't use Using

Orwin answered 2/11, 2015 at 22:8 Comment(0)
E
0

GC calls Object.Finalize. IDisposable is not called by GC, it has to be called manually. GC has nothing to do with using and IDisposable

Equine answered 9/2, 2017 at 14:40 Comment(0)
O
-3

IDisposable tells GC what to do when it wants to get rid of an object.
using asks GC to get rid of an object when its scope ends.
using using with an object that's not IDisposable is useless.

Olivier answered 30/10, 2015 at 20:31 Comment(4)
But PetaPoco.Database does support IDisposable.Genevagenevan
@Genevagenevan That's not against what I said. see my other comment above.I merely stated 3 points.Olivier
But since the object does implement IDisposable, what's the point of the comment about using using with objects that don't implement IDisposable?Genevagenevan
It was the result of the first two points. if A=>B and C=>A then !B=>!C. sorry I confused You. my brain sometimes works that way.Olivier

© 2022 - 2024 — McMap. All rights reserved.