6

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?

Howiecamp
  • 2,981
  • 6
  • 38
  • 59
  • 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 nothing – Steve Oct 30 '15 at 20:32
  • 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? – Howiecamp Oct 30 '15 at 20:34
  • @Howiecamp 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. – Behrooz Oct 30 '15 at 20:35
  • @Behrooz - 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. – Howiecamp Oct 30 '15 at 20:37
  • @Howiecamp I think You understand the logic better than they did. they're not gods. – Behrooz Oct 30 '15 at 20:38

5 Answers5

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.

STW
  • 44,917
  • 17
  • 105
  • 161
  • 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. – Plebsori Jan 14 '16 at 09:49
2

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();
}
Plebsori
  • 1,075
  • 9
  • 23
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

Eduardo Molteni
  • 38,786
  • 23
  • 141
  • 206
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

Nadi
  • 11
  • 1
-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.

Behrooz
  • 1,696
  • 2
  • 32
  • 54
  • But PetaPoco.Database *does* support IDisposable. – Howiecamp Oct 30 '15 at 20:34
  • @Howiecamp That's not against what I said. see my other comment above.I merely stated 3 points. – Behrooz Oct 30 '15 at 20:36
  • But since the object *does* implement IDisposable, what's the point of the comment about using using with objects that *don't* implement IDisposable? – Howiecamp Oct 30 '15 at 20:38
  • 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. – Behrooz Oct 30 '15 at 20:42