Skip to content

Instantly share code, notes, and snippets.

@ProductiveRage
Created January 19, 2019 12:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProductiveRage/31a92e1470b96ce34e454a73bd8e19c4 to your computer and use it in GitHub Desktop.
Save ProductiveRage/31a92e1470b96ce34e454a73bd8e19c4 to your computer and use it in GitHub Desktop.
// In relation to https://stackoverflow.com/a/50925790/3813189
public class WrappedDbConnection : DbConnection
{
private readonly DbConnection _conn;
public WrappedDbConnection(DbConnection connection) => _conn = connection ?? throw new ArgumentNullException(nameof(connection));
public override string ConnectionString
{
get { return _conn.ConnectionString; }
set { _conn.ConnectionString = value; }
}
public override int ConnectionTimeout => _conn.ConnectionTimeout;
public override string Database => _conn.Database;
public override ConnectionState State => _conn.State;
public override string DataSource => _conn.DataSource;
public override string ServerVersion => _conn.ServerVersion;
public override void Open() => _conn.Open();
public override void Close() => _conn.Close();
protected override DbTransaction BeginDbTransaction(IsolationLevel il) => _conn.BeginTransaction(il);
public override void ChangeDatabase(string databaseName) => _conn.ChangeDatabase(databaseName);
protected override DbCommand CreateDbCommand() => new WrappedDbCommand(_conn.CreateCommand());
}
public class WrappedDbCommand : DbCommand
{
private readonly DbCommand _cmd;
public WrappedDbCommand(DbCommand command) => _cmd = command ?? throw new ArgumentNullException(nameof(command));
public override string CommandText
{
get { return _cmd.CommandText; }
set { _cmd.CommandText = value; }
}
public override int CommandTimeout
{
get { return _cmd.CommandTimeout; }
set { _cmd.CommandTimeout = value; }
}
public override CommandType CommandType
{
get { return _cmd.CommandType; }
set { _cmd.CommandType = value; }
}
protected override DbConnection DbConnection
{
get { return _cmd.Connection; }
set { _cmd.Connection = value; }
}
protected override DbParameterCollection DbParameterCollection => _cmd.Parameters;
protected override DbTransaction DbTransaction
{
get { return _cmd.Transaction; }
set { _cmd.Transaction = value; }
}
public override UpdateRowSource UpdatedRowSource
{
get { return _cmd.UpdatedRowSource; }
set { _cmd.UpdatedRowSource = value; }
}
public override void Prepare() => _cmd.Prepare();
public override void Cancel() => _cmd.Cancel();
protected override DbParameter CreateDbParameter() => _cmd.CreateParameter();
public override int ExecuteNonQuery()
{
Console.WriteLine($"[ExecuteNonQuery] {_cmd.CommandText}");
return _cmd.ExecuteNonQuery();
}
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
{
Console.WriteLine($"[ExecuteReader({behavior})] {_cmd.CommandText}");
return _cmd.ExecuteReader();
}
public override object ExecuteScalar()
{
Console.WriteLine($"[ExecuteScalar] {_cmd.CommandText}");
return _cmd.ExecuteScalar();
}
// TODO: I can't pass this through to the underlying DbCommand (though I can't see why since it's a public
// property on DbCommand, which is why we have to implement it!) so I'll just leave it as always false
public override bool DesignTimeVisible
{
get { return false; }
set { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment