@@ -114,30 +114,23 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior) =>
114
114
public override Task < int > ExecuteNonQueryAsync ( CancellationToken cancellationToken ) =>
115
115
ExecuteNonQueryAsync ( Connection . AsyncIOBehavior , cancellationToken ) ;
116
116
117
- internal async Task < int > ExecuteNonQueryAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken )
118
- {
119
- VerifyValid ( ) ;
120
- return await m_commandExecutor . ExecuteNonQueryAsync ( CommandText , m_parameterCollection , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
121
- }
117
+ internal Task < int > ExecuteNonQueryAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken ) =>
118
+ ! IsValid ( out var exception ) ? Utility . TaskFromException < int > ( exception ) :
119
+ m_commandExecutor . ExecuteNonQueryAsync ( CommandText , m_parameterCollection , ioBehavior , cancellationToken ) ;
122
120
123
121
public override Task < object > ExecuteScalarAsync ( CancellationToken cancellationToken ) =>
124
122
ExecuteScalarAsync ( Connection . AsyncIOBehavior , cancellationToken ) ;
125
123
126
- internal async Task < object > ExecuteScalarAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken )
127
- {
128
- VerifyValid ( ) ;
129
- return await m_commandExecutor . ExecuteScalarAsync ( CommandText , m_parameterCollection , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
130
- }
124
+ internal Task < object > ExecuteScalarAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken ) =>
125
+ ! IsValid ( out var exception ) ? Utility . TaskFromException < object > ( exception ) :
126
+ m_commandExecutor . ExecuteScalarAsync ( CommandText , m_parameterCollection , ioBehavior , cancellationToken ) ;
131
127
132
128
protected override Task < DbDataReader > ExecuteDbDataReaderAsync ( CommandBehavior behavior , CancellationToken cancellationToken ) =>
133
129
ExecuteReaderAsync ( behavior , Connection . AsyncIOBehavior , cancellationToken ) ;
134
130
135
- internal async Task < DbDataReader > ExecuteReaderAsync ( CommandBehavior behavior , IOBehavior ioBehavior ,
136
- CancellationToken cancellationToken )
137
- {
138
- VerifyValid ( ) ;
139
- return await m_commandExecutor . ExecuteReaderAsync ( CommandText , m_parameterCollection , behavior , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
140
- }
131
+ internal Task < DbDataReader > ExecuteReaderAsync ( CommandBehavior behavior , IOBehavior ioBehavior , CancellationToken cancellationToken ) =>
132
+ ! IsValid ( out var exception ) ? Utility . TaskFromException < DbDataReader > ( exception ) :
133
+ m_commandExecutor . ExecuteReaderAsync ( CommandText , m_parameterCollection , behavior , ioBehavior , cancellationToken ) ;
141
134
142
135
protected override void Dispose ( bool disposing )
143
136
{
@@ -181,15 +174,25 @@ private void VerifyNotDisposed()
181
174
182
175
private void VerifyValid ( )
183
176
{
184
- VerifyNotDisposed ( ) ;
185
- if ( DbConnection == null )
186
- throw new InvalidOperationException ( "Connection property must be non-null." ) ;
187
- if ( DbConnection . State != ConnectionState . Open && DbConnection . State != ConnectionState . Connecting )
188
- throw new InvalidOperationException ( "Connection must be Open; current state is {0}" . FormatInvariant ( DbConnection . State ) ) ;
189
- if ( DbTransaction != Connection . CurrentTransaction )
190
- throw new InvalidOperationException ( "The transaction associated with this command is not the connection's active transaction." ) ;
191
- if ( string . IsNullOrWhiteSpace ( CommandText ) )
192
- throw new InvalidOperationException ( "CommandText must be specified" ) ;
177
+ Exception exception ;
178
+ if ( ! IsValid ( out exception ) )
179
+ throw exception ;
180
+ }
181
+
182
+ private bool IsValid ( out Exception exception )
183
+ {
184
+ exception = null ;
185
+ if ( m_parameterCollection == null )
186
+ exception = new ObjectDisposedException ( GetType ( ) . Name ) ;
187
+ else if ( DbConnection == null )
188
+ exception = new InvalidOperationException ( "Connection property must be non-null." ) ;
189
+ else if ( DbConnection . State != ConnectionState . Open && DbConnection . State != ConnectionState . Connecting )
190
+ exception = new InvalidOperationException ( "Connection must be Open; current state is {0}" . FormatInvariant ( DbConnection . State ) ) ;
191
+ else if ( DbTransaction != Connection . CurrentTransaction )
192
+ exception = new InvalidOperationException ( "The transaction associated with this command is not the connection's active transaction." ) ;
193
+ else if ( string . IsNullOrWhiteSpace ( CommandText ) )
194
+ exception = new InvalidOperationException ( "CommandText must be specified" ) ;
195
+ return exception == null ;
193
196
}
194
197
195
198
internal void ReaderClosed ( ) => ( m_commandExecutor as StoredProcedureCommandExecutor ) ? . SetParams ( ) ;
0 commit comments