Skip to content

Instantly share code, notes, and snippets.

@jehugaleahsa
Created August 24, 2016 14:21
Show Gist options
  • Save jehugaleahsa/e460f9e168e89bc450f328eefb2c2a21 to your computer and use it in GitHub Desktop.
Save jehugaleahsa/e460f9e168e89bc450f328eefb2c2a21 to your computer and use it in GitHub Desktop.
Connection Lifetime Manager
public class ConnectionLifetimeManager : IDisposable
{
private readonly IDbConnection connection;
private readonly bool needsClosed;
public ConnectionLifetimeManager(IDbConnection connection)
{
this.connection = connection;
if (connection.State == ConnectionState.Closed)
{
connection.Open();
needsClosed = true;
}
}
public void Dispose()
{
if (needsClosed)
{
connection.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment