The PetaPoco.Database base implements IDisposable, so why don't most examples have a 'using' statement?

The PetaPoco.Database object implements IDisposable, but I rarely ever see code samples (including PetaPoco’s own site ) that include the using as statement as follows:

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

More often than not, I just see:

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

How should PetaPoco.Database objects be transferred?

+5
source share
5 answers

As a simple rule: if the class implements IDisposable, then go ahead and wrap it when used. In fact, they cannot use any unmanaged resources, but it will not hurt and can protect you from changes in the future.

+3
source

PetaPoco's helper is here. Dispose is the same as calling CloseSharedConnection() . However, C # syntax only supports (...) using IDisposable . For example, IDBConnection , from memory, supports both Close and Dispose .

Basically, it comes down to choosing

Example 1

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

Example 2

 var db = new PetaPoco() try { // code } finally { db.CloseSharedConnection(); } 
+1
source

It depends on what you are implementing. The database is a database connection, you do not want to delete the connection every time you execute the sql statement in the database, because the performance will be terrible.

If you are using a web solution, you usually use one database object for each query, but this query certainly executes several sql statements in different filters, global filters and several control methods, so you cannot use Using

0
source

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

0
source

IDisposable tells GC what to do when it wants to get rid of object .
using asks the GC get rid of object when its scope ends.
using using with object , which is not IDisposable useless.

-2
source

Source: https://habr.com/ru/post/1234885/


All Articles