Skip to content

Instantly share code, notes, and snippets.

@dcagnetta
Created March 27, 2022 12:43
Show Gist options
  • Save dcagnetta/d8119b3eca9a586cbd6ce1c7636e7188 to your computer and use it in GitHub Desktop.
Save dcagnetta/d8119b3eca9a586cbd6ce1c7636e7188 to your computer and use it in GitHub Desktop.
IDisposable Pattern
public class DbRepository : IDisposable
{
public IDbConnection Connection { get; set; }
private bool _disposedValue;
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
Connection.Dispose();
}
// Free unmanaged resources (unmanaged objects) and override finalizer
// Set large fields to null
Connection = null;
_disposedValue = true;
}
}
public virtual void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment