Struct ConnectionScope<TConnection>
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.Core.dll
A connection that was opened if it needed to be, the state it was found in, and what to do with it when the scope is disposed.
public readonly struct ConnectionScope<TConnection> : IDisposable, IAsyncDisposable where TConnection : IDbConnection
Type Parameters
TConnectionThe type of the connection, as the caller holds it.
- Implements
- Inherited Members
Remarks
One type for every source of a connection: the scope carries how it ends. What disposing does depends on what made it:
| Made by | The connection came from; disposing the scope |
|---|---|
| EnsureOpenScope<TConnection>(TConnection) / EnsureOpenScopeAsync<TConnection>(TConnection, CancellationToken) | You already hold it; closes it if the scope opened it, and leaves it open if it was already open. Never disposes it. |
| EnsureOpenScope(IDbConnectionFactory) / EnsureOpenScopeAsync(IDbConnectionFactory, CancellationToken) (on a factory) | The factory created it, so it is yours; disposes it: Dispose, as
factory.Open(...) does, and DisposeAsync from DisposeAsync() when the connection has one. |
| EnsureOpenScope(IDbConnectionPool) / EnsureOpenScopeAsync(IDbConnectionPool, CancellationToken) (on a pool) | The pool leased it; gives it back with Give(IDbConnection), as
pool.Open(...) does. What giving back means (close it, keep it, dispose it) is the pool's. |
When you already hold the connection the scope is usually only there to be disposed:
await using var _ = await connection.EnsureOpenScopeAsync(cancellationToken);
// ... use the connection you already hold.
When the connection came from a factory or a pool the scope is how you get it:
await using var scope = await factory.EnsureOpenScopeAsync(cancellationToken);
var connection = scope.Connection;
The scope keeps the type of the connection (a SqlConnection stays a
SqlConnection), and deconstructs into the connection and the state it was found in:
var (sql, was) = scope;.
A connection that was broken is closed and opened again, so a scope that closes it closes it again afterwards; the broken state is not restored.
Dispose the value you were given exactly once, and do not copy it: a second dispose of a copy
disposes a factory's connection again, gives a pool's connection back again, or closes a connection
that someone opened in between. Deconstruct a scope you hold, never the call itself:
var (c, s) = connection.EnsureOpenScope(); drops the scope, and the connection it opened
stays open.
A default scope holds nothing: disposing it does nothing, and reading Connection (or deconstructing it) throws InvalidOperationException.
Properties
- Connection
Gets the connection, open for as long as the scope has not been disposed.
- PriorState
Gets the state the connection was in when the scope was made (not its state now): for example Closed, or Open (possibly with Executing or Fetching), or with Broken set.
Methods
- ConfigureAwait(bool)
Configures how await using awaits the disposal of this scope.
- Deconstruct(out TConnection, out ConnectionState)
Splits the scope into the connection and the state it was found in.
- Dispose()
Lets go of the connection the way its source calls for: closes it if the scope opened it (a connection the caller held), disposes it (a factory's), or gives it back to the pool (a pool's). See the table in the remarks of the type.
- DisposeAsync()
As Dispose(), asynchronously where the connection can:
DisposeAsyncfor a factory's connection that has one, andCloseAsyncfor a caller's DbConnection. Giving a connection back to a pool is synchronous.
- ReaderBehavior(CommandBehavior)
The behavior to execute a reader with:
behavior, plus CloseConnection when this scope opened the connection.