Skip to content

Commit ba94638

Browse files
Merge pull request #385 from sqlkata/feature/idisposable
implement IDisposable for QueryFactory
2 parents 2957322 + 84ab2e5 commit ba94638

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

Program/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ static void Main(string[] args)
4646
var compiler = new SqlServerCompiler();
4747
var sql = compiler.Compile(query).Sql;
4848
Console.WriteLine(sql);
49+
50+
51+
using (var db = SqlLiteQueryFactory())
52+
{
53+
var accounts = db.Query("accounts").Get();
54+
Console.WriteLine(accounts.Count());
55+
}
4956
}
5057

5158
private static void log(Compiler compiler, Query query)
@@ -79,6 +86,7 @@ private static QueryFactory SqlLiteQueryFactory()
7986
}
8087

8188
return db;
89+
8290
}
8391

8492
private static QueryFactory SqlServerQueryFactory()

SqlKata.Execution/QueryFactory.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
namespace SqlKata.Execution
1212
{
13-
public class QueryFactory
13+
public class QueryFactory : IDisposable
1414
{
1515
public IDbConnection Connection { get; set; }
1616
public Compiler Compiler { get; set; }
1717
public Action<SqlResult> Logger = result => { };
18+
private bool disposedValue;
19+
1820
public int QueryTimeout { get; set; } = 30;
1921

2022
public QueryFactory() { }
@@ -823,5 +825,35 @@ internal SqlResult CompileAndLog(Query query)
823825
return compiled;
824826
}
825827

828+
protected virtual void Dispose(bool disposing)
829+
{
830+
if (!disposedValue)
831+
{
832+
if (disposing)
833+
{
834+
Connection.Dispose();
835+
}
836+
837+
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
838+
// TODO: set large fields to null
839+
Connection = null;
840+
Compiler = null;
841+
disposedValue = true;
842+
}
843+
}
844+
845+
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
846+
// ~QueryFactory()
847+
// {
848+
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
849+
// Dispose(disposing: false);
850+
// }
851+
852+
public void Dispose()
853+
{
854+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
855+
Dispose(disposing: true);
856+
GC.SuppressFinalize(this);
857+
}
826858
}
827859
}

0 commit comments

Comments
 (0)