-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandWrapper.cs
300 lines (268 loc) · 11.7 KB
/
CommandWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System;
using System.Data;
using System.Data.Common;
using System.Dynamic;
using System.Linq.Expressions;
using System.Reflection;
namespace SqlProfiler
{
/// <summary>
/// Generic <see cref="DbCommand"/> wrapper with dynamic tricks to allow easy access to driver-specific properties of wrapped command
/// </summary>
public class CommandWrapper : DbCommand, IDynamicMetaObjectProvider
{
/// <summary>
/// The wrapped command
/// </summary>
/// <returns></returns>
/// <remarks>This is public for simple debugging by the user; the call to <see cref="DelegatingMetaObject"/> needs to add <see cref="BindingFlags.Public"/> to match.</remarks>
public DbCommand Wrapped { get; }
private readonly PropertyInfo _DbConnection;
private readonly PropertyInfo _DbParameterCollection;
private readonly PropertyInfo _DbTransaction;
private readonly MethodInfo _CreateDbParameter;
private readonly MethodInfo _ExecuteDbDataReader;
/// <summary>
/// Constructor
/// </summary>
/// <param name="wrapped">The command to wrap</param>
public CommandWrapper(DbCommand wrapped)
{
Wrapped = wrapped;
// Get all the protected elements which we need to pass back to the wrapped object
var type = wrapped.GetType();
_DbConnection = type.GetNonPublicProperty("DbConnection");
_DbParameterCollection = type.GetNonPublicProperty("DbParameterCollection");
_DbTransaction = type.GetNonPublicProperty("DbTransaction");
_CreateDbParameter = type.GetNonPublicMethod("CreateDbParameter", Type.EmptyTypes);
_ExecuteDbDataReader = type.GetNonPublicMethod("ExecuteDbDataReader", new Type[] { typeof(CommandBehavior) });
}
/// <summary>
/// Supports dynamically getting and setting properties on the wrapped DbCommand
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
public DynamicMetaObject GetMetaObject(Expression expression)
{
return new DelegatingMetaObject(expression, this, nameof(Wrapped), BindingFlags.Instance | BindingFlags.Public);
}
/// <summary>
/// Wrapped <see cref="DbCommand.CommandText"/> property
/// </summary>
public override string CommandText
{
get { return Wrapped.CommandText; }
set { Wrapped.CommandText = value; }
}
/// <summary>
/// Wrapped <see cref="DbCommand.CommandTimeout"/> property
/// </summary>
public override int CommandTimeout
{
get { return Wrapped.CommandTimeout; }
set { Wrapped.CommandTimeout = value; }
}
/// <summary>
/// Wrapped <see cref="DbCommand.CommandType"/> property
/// </summary>
public override CommandType CommandType
{
get { return Wrapped.CommandType; }
set { Wrapped.CommandType = value; }
}
/// <summary>
/// Wrapped <see cref="DbCommand.DesignTimeVisible"/> property
/// </summary>
public override bool DesignTimeVisible
{
get { return Wrapped.DesignTimeVisible; }
set { Wrapped.DesignTimeVisible = value; }
}
/// <summary>
/// Wrapped <see cref="DbCommand.UpdatedRowSource"/> property
/// </summary>
public override UpdateRowSource UpdatedRowSource
{
get { return Wrapped.UpdatedRowSource; }
set { Wrapped.UpdatedRowSource = value; }
}
/// <summary>
/// Wrapped non-public <see cref="DbCommand.DbConnection"/> property
/// </summary>
protected override DbConnection DbConnection
{
get { return (DbConnection)_DbConnection.GetValue(Wrapped); }
set { _DbConnection.SetValue(Wrapped, (DbConnection)value); }
}
/// <summary>
/// Wrapped non-public <see cref="DbCommand.DbParameterCollection "/> property
/// </summary>
protected override DbParameterCollection DbParameterCollection
{
get { return (DbParameterCollection)_DbParameterCollection.GetValue(Wrapped); }
}
/// <summary>
/// Wrapped non-public <see cref="DbCommand.DbTransaction"/> property
/// </summary>
protected override DbTransaction DbTransaction { get { return (DbTransaction)_DbTransaction.GetValue(Wrapped); } set { _DbTransaction.SetValue(Wrapped, (DbTransaction)value); } }
/// <summary>
/// Wrapped <see cref="DbCommand.Cancel"/> method, with <see cref="PreCancel(DbCommand)"/> and <see cref="PostCancel(DbCommand, object)"/> hooks
/// </summary>
public override void Cancel()
{
var profilingObject = PreCancel(Wrapped);
Wrapped.Cancel();
PostCancel(Wrapped, profilingObject);
}
/// <summary>
/// Pre-<see cref="DbCommand.Cancel"/> hook
/// </summary>
/// <param name="command">The wrapped command</param>
/// <returns></returns>
protected virtual object PreCancel(DbCommand command) { return null; }
/// <summary>
/// Post-<see cref="DbCommand.Cancel"/> hook
/// </summary>
/// <param name="command">The wrapped command</param>
/// <param name="profilingObject">Whatever was returned from <see cref="PreCancel(DbCommand)"/></param>
/// <returns></returns>
protected virtual void PostCancel(DbCommand command, object profilingObject) {}
/// <summary>
/// Wrapped <see cref="DbCommand.ExecuteNonQuery"/> method, with <see cref="PreExecuteNonQuery(DbCommand)"/> and <see cref="PostExecuteNonQuery(DbCommand, object)"/> hooks
/// </summary>
/// <returns></returns>
public override int ExecuteNonQuery()
{
var profilingObject = PreExecuteNonQuery(Wrapped);
var result = Wrapped.ExecuteNonQuery();
PostExecuteNonQuery(Wrapped, profilingObject);
return result;
}
/// <summary>
/// Pre-<see cref="DbCommand.ExecuteNonQuery"/> hook
/// </summary>
/// <param name="command">The wrapped command</param>
/// <returns></returns>
protected virtual object PreExecuteNonQuery(DbCommand command) { return null; }
/// <summary>
/// Post-<see cref="DbCommand.ExecuteNonQuery"/> hook
/// </summary>
/// <param name="command">The wrapped command</param>
/// <param name="profilingObject">Whatever was returned from the pre-method hook</param>
/// <returns></returns>
protected virtual void PostExecuteNonQuery(DbCommand command, object profilingObject) {}
/// <summary>
/// Wrapped <see cref="DbCommand.ExecuteScalar"/> method, with <see cref="PreExecuteScalar(DbCommand)"/> and <see cref="PostExecuteScalar(DbCommand, object)"/> hooks
/// </summary>
/// <returns></returns>
public override object ExecuteScalar()
{
var profilingObject = PreExecuteScalar(Wrapped);
var result = Wrapped.ExecuteScalar();
PostExecuteScalar(Wrapped, profilingObject);
return result;
}
/// <summary>
/// Pre-<see cref="DbCommand.ExecuteScalar"/> hook
/// </summary>
/// <param name="command">The wrapped command</param>
/// <returns></returns>
protected virtual object PreExecuteScalar(DbCommand command) { return null; }
/// <summary>
/// Post-<see cref="DbCommand.ExecuteScalar"/> hook
/// </summary>
/// <param name="command">The wrapped command</param>
/// <param name="profilingObject">Whatever was returned from the pre-method hook</param>
/// <returns></returns>
protected virtual void PostExecuteScalar(DbCommand command, object profilingObject) {}
/// <summary>
/// Wrapped <see cref="DbCommand.Prepare"/> method, with <see cref="PrePrepare(DbCommand)"/> and <see cref="PostPrepare(DbCommand, object)"/> hooks
/// </summary>
public override void Prepare()
{
var profilingObject = PrePrepare(Wrapped);
Wrapped.Prepare();
PostPrepare(Wrapped, profilingObject);
}
/// <summary>
/// Pre-<see cref="DbCommand.Prepare"/> hook
/// </summary>
/// <param name="command">The wrapped command</param>
/// <returns></returns>
protected virtual object PrePrepare(DbCommand command) { return null; }
/// <summary>
/// Post-<see cref="DbCommand.Prepare"/> hook
/// </summary>
/// <param name="command">The wrapped command</param>
/// <param name="profilingObject">Whatever was returned from the pre-method hook</param>
/// <returns></returns>
protected virtual void PostPrepare(DbCommand command, object profilingObject) {}
/// <summary>
/// Wrapped non-public <see cref="DbCommand.CreateDbParameter"/> method, with <see cref="PreCreateDbParameter(DbCommand)"/> and <see cref="PostCreateDbParameter(DbCommand, object)"/> hooks
/// </summary>
/// <returns></returns>
protected override DbParameter CreateDbParameter()
{
var profilingObject = PreCreateDbParameter(Wrapped);
DbParameter param;
try
{
param = (DbParameter)_CreateDbParameter.Invoke(Wrapped, null);
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}
PostCreateDbParameter(Wrapped, profilingObject);
return param;
}
/// <summary>
/// Pre-<see cref="DbCommand.CreateDbParameter"/> hook
/// </summary>
/// <param name="command">The wrapped command</param>
/// <returns></returns>
protected virtual object PreCreateDbParameter(DbCommand command) { return null; }
/// <summary>
/// Post-<see cref="DbCommand.CreateDbParameter"/> hook
/// </summary>
/// <param name="command">The wrapped command</param>
/// <param name="profilingObject">Whatever was returned from the pre-method hook</param>
/// <returns></returns>
protected virtual void PostCreateDbParameter(DbCommand command, object profilingObject) {}
/// <summary>
/// Wrapped non-public <see cref="DbCommand.ExecuteDbDataReader(CommandBehavior)"/> method, with <see cref="PreExecuteDbDataReader(DbCommand, CommandBehavior)"/> and <see cref="PostExecuteDbDataReader(DbCommand, CommandBehavior, object)"/> hooks
/// </summary>
/// <param name="behavior">The command behavior</param>
/// <returns></returns>
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
{
var profilingObject = PreExecuteDbDataReader(Wrapped, behavior);
DbDataReader reader;
try
{
reader = (DbDataReader)_ExecuteDbDataReader.Invoke(Wrapped, new object[] { behavior });
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}
PostExecuteDbDataReader(Wrapped, behavior, profilingObject);
return reader;
}
/// <summary>
/// Pre-<see cref="DbCommand.ExecuteDbDataReader(CommandBehavior)"/> hook
/// </summary>
/// <param name="command">The wrapped command</param>
/// <param name="behavior">The command behavior</param>
/// <returns></returns>
protected virtual object PreExecuteDbDataReader(DbCommand command, CommandBehavior behavior) { return null; }
/// <summary>
/// Post-<see cref="DbCommand.ExecuteDbDataReader(CommandBehavior)"/> hook
/// </summary>
/// <param name="command">The wrapped command</param>
/// <param name="behavior">The command behavior</param>
/// <param name="profilingObject">Whatever was returned from the pre-method hook</param>
/// <returns></returns>
protected virtual void PostExecuteDbDataReader(DbCommand command, CommandBehavior behavior, object profilingObject) {}
}
}