public async Task ExplicitOpenAsync_and_Dispose_are_order_independent()
        {
            using (var ctx = new SqlStandardCallContext(TestHelper.Monitor))
            {
                ISqlConnectionController c = ctx[TestHelper.GetConnectionString()];
                c.Connection.State.Should().Be(ConnectionState.Closed);
                var d1 = await c.ExplicitOpenAsync();

                c.Connection.State.Should().Be(ConnectionState.Open);
                var d2 = await c.ExplicitOpenAsync();

                var d3 = await c.ExplicitOpenAsync();

                d1.Should().NotBeNull();
                d2.Should().NotBeNull();
                d3.Should().NotBeNull();
                c.Connection.State.Should().Be(ConnectionState.Open);
                d1.Dispose();
                d1.Dispose();
                d2.Dispose();
                c.Connection.State.Should().Be(ConnectionState.Open);
                d3.Dispose();
                c.Connection.State.Should().Be(ConnectionState.Closed);
            }
        }
 public async Task ExecuteScalarAsync_works_on_closed_or_opened_connection()
 {
     using( var cmd = new SqlCommand( "select count(*) from sys.objects" ) )
     using( var ctx = new SqlStandardCallContext() )
     {
         ISqlConnectionController c = ctx[TestHelper.GetConnectionString()];
         c.Connection.State.Should().Be( ConnectionState.Closed );
         using( c.ExplicitOpen() )
         {
             c.Connection.State.Should().Be( ConnectionState.Open );
             ((int)c.ExecuteScalar( cmd )).Should().BeGreaterThan( 0 );
         }
         c.Connection.State.Should().Be( ConnectionState.Closed );
         ((int)await c.ExecuteScalarAsync( cmd )).Should().BeGreaterThan( 0 );
         c.Connection.State.Should().Be( ConnectionState.Closed );
         cmd.CommandText = "select count(*) from sys.objects where name='no-object-here'";
         using( await c.ExplicitOpenAsync() )
         {
             c.Connection.State.Should().Be( ConnectionState.Open );
             ((int)await c.ExecuteScalarAsync( cmd )).Should().Be( 0 );
         }
         c.Connection.State.Should().Be( ConnectionState.Closed );
         ((int)await c.ExecuteScalarAsync( cmd )).Should().Be( 0 );
         c.Connection.State.Should().Be( ConnectionState.Closed );
         cmd.CommandText = "select name from sys.tables where name='no-object-here'";
         using( await c.ExplicitOpenAsync() )
         {
             (await c.ExecuteScalarAsync( cmd )).Should().BeNull();
         }
         c.Connection.State.Should().Be( ConnectionState.Closed );
     }
 }
        public static async Task <object?> ExecuteScalarAsync(this ISqlConnectionController @this, SqlCommand cmd, CancellationToken cancellationToken = default)
        {
            var ctx = @this.SqlCallContext;

            using (await @this.ExplicitOpenAsync())
            {
                return(await ctx.Executor.ExecuteQueryAsync(ctx.Monitor, @this.Connection, @this.Transaction, cmd, (c, t) => c.ExecuteScalarAsync(t), cancellationToken));
            }
        }
        public static async Task <T> ExecuteQueryAsync <T>(this ISqlConnectionController @this,
                                                           SqlCommand cmd,
                                                           Func <SqlCommand, CancellationToken, Task <T> > innerExecutor,
                                                           CancellationToken cancellationToken = default)
        {
            var ctx = @this.SqlCallContext;

            using (await @this.ExplicitOpenAsync())
            {
                return(await ctx.Executor.ExecuteQueryAsync(ctx.Monitor, @this.Connection, @this.Transaction, cmd, innerExecutor, cancellationToken));
            }
        }
 public static Task PreOpenAsync(this ISqlConnectionController ctx, CancellationToken cancellationToken = default)
 {
     return(ctx.ExplicitOpenAsync(cancellationToken));
 }