Skip to content

Commit 022b6bd

Browse files
kekekeksNickCraver
authored andcommitted
Added an option for a custom ProfiledDbDataReader implementation (MiniProfiler#325)
* Added extension points for creating ProfiledDbCommand and ProfiledDbDataReader
1 parent 3c11ecb commit 022b6bd

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/MiniProfiler.Shared/Data/ProfiledDbCommand.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ public override UpdateRowSource UpdatedRowSource
196196
set => _command.UpdatedRowSource = value;
197197
}
198198

199+
/// <summary>
200+
/// Creates a wrapper data reader for <see cref="ExecuteDbDataReader"/> and <see cref="ExecuteDbDataReaderAsync"/> />
201+
/// </summary>
202+
protected virtual DbDataReader CreateDbDataReader(DbDataReader original, IDbProfiler profiler)
203+
=> new ProfiledDbDataReader(original, profiler);
204+
199205
/// <summary>
200206
/// Executes a database data reader.
201207
/// </summary>
@@ -213,7 +219,7 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
213219
try
214220
{
215221
result = _command.ExecuteReader(behavior);
216-
result = new ProfiledDbDataReader(result, _profiler);
222+
result = CreateDbDataReader(result, _profiler);
217223
}
218224
catch (Exception e)
219225
{
@@ -246,7 +252,7 @@ protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBeha
246252
try
247253
{
248254
result = await _command.ExecuteReaderAsync(behavior, cancellationToken).ConfigureAwait(false);
249-
result = new ProfiledDbDataReader(result, _profiler);
255+
result = CreateDbDataReader(result, _profiler);
250256
}
251257
catch (Exception e)
252258
{

src/MiniProfiler.Shared/Data/ProfiledDbConnection.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,19 @@ protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLeve
137137
{
138138
return new ProfiledDbTransaction(_connection.BeginTransaction(isolationLevel), this);
139139
}
140+
141+
/// <summary>
142+
/// Creates and returns a <see cref="DbCommand"/> object associated with the current connection.
143+
/// </summary>
144+
/// <returns>A <see cref="ProfiledDbCommand"/> wrapping the created <see cref="DbCommand"/>.</returns>
145+
protected virtual DbCommand CreateDbCommand(DbCommand original, IDbProfiler profiler)
146+
=> new ProfiledDbCommand(original, this, profiler);
140147

141148
/// <summary>
142149
/// Creates and returns a <see cref="DbCommand"/> object associated with the current connection.
143150
/// </summary>
144151
/// <returns>A <see cref="ProfiledDbCommand"/> wrapping the created <see cref="DbCommand"/>.</returns>
145-
protected override DbCommand CreateDbCommand() => new ProfiledDbCommand(_connection.CreateCommand(), this, _profiler);
152+
protected override DbCommand CreateDbCommand() => CreateDbCommand(_connection.CreateCommand(), _profiler);
146153

147154
/// <summary>
148155
/// Dispose the underlying connection.

0 commit comments

Comments
 (0)