Method EnsureOpenScope
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.Core.dll
EnsureOpenScope<TConnection>(TConnection)
Opens the connection if it is not open (a broken connection is closed first) and returns a ConnectionScope<TConnection> that puts it back when disposed: a connection this call opened is closed again, and one that was already open is left open.
public static ConnectionScope<TConnection> EnsureOpenScope<TConnection>(this TConnection connection) where TConnection : IDbConnection
Parameters
connectionTConnectionThe connection to open.
Returns
- ConnectionScope<TConnection>
The connection and the state it was found in; Dispose() closes it again if the scope opened it.
Type Parameters
TConnectionThe type of the connection.
Remarks
The same rule as EnsureOpen(IDbConnection), which returns the prior state for a caller that does the closing itself; the scope does the closing when it is disposed. It is safe to call on a connection that is already open. Prefer EnsureOpenScopeAsync<TConnection>(TConnection, CancellationToken).
Exceptions
- ArgumentNullException
connectionis null.
EnsureOpenScope(IDbConnectionFactory)
Creates a connection from the factory, opens it if needed, and returns a ConnectionScope<TConnection> that disposes it when the scope is disposed.
public static ConnectionScope<IDbConnection> EnsureOpenScope(this IDbConnectionFactory connectionFactory)
Parameters
connectionFactoryIDbConnectionFactoryThe connection factory to create the connection from.
Returns
- ConnectionScope<IDbConnection>
The connection, and the state it was in before it was opened.
Remarks
The scope form of Open<T>(IDbConnectionFactory, Func<IDbConnection, T>): the same create, open if needed, dispose, without the callback. Prefer EnsureOpenScopeAsync(IDbConnectionFactory, CancellationToken). If opening throws the connection is disposed before the exception propagates.
Exceptions
- ArgumentNullException
connectionFactoryis null.
EnsureOpenScope<TConnection>(IDbConnectionFactory<TConnection>)
Creates a connection from the factory, opens it if needed, and returns a ConnectionScope<TConnection> that disposes it when the scope is disposed.
public static ConnectionScope<TConnection> EnsureOpenScope<TConnection>(this IDbConnectionFactory<TConnection> connectionFactory) where TConnection : IDbConnection
Parameters
connectionFactoryIDbConnectionFactory<TConnection>The connection factory to create the connection from.
Returns
- ConnectionScope<TConnection>
The connection, and the state it was in before it was opened.
Type Parameters
TConnectionThe type of the connection.
Remarks
The scope form of Open<T>(IDbConnectionFactory, Func<IDbConnection, T>): the same create, open if needed, dispose, without the callback. Prefer EnsureOpenScopeAsync(IDbConnectionFactory, CancellationToken). If opening throws the connection is disposed before the exception propagates.
Exceptions
- ArgumentNullException
connectionFactoryis null.
EnsureOpenScope(IDbConnectionPool)
Takes a connection from the pool, opens it if needed, and returns a ConnectionScope<TConnection> that gives it back to the pool when the scope is disposed.
public static ConnectionScope<IDbConnection> EnsureOpenScope(this IDbConnectionPool connectionPool)
Parameters
connectionPoolIDbConnectionPoolThe connection pool to take the connection from.
Returns
- ConnectionScope<IDbConnection>
The connection, and the state it was in before it was opened.
Remarks
The scope form of Open<T>(IDbConnectionPool, Func<IDbConnection, ConnectionState, T>): the same take, open if needed, give back, without the callback. Giving back is the pool's: it may close the connection, keep it, or dispose it. Prefer EnsureOpenScopeAsync(IDbConnectionPool, CancellationToken). If opening throws the connection is given back before the exception propagates.
Exceptions
- ArgumentNullException
connectionPoolis null.
EnsureOpenScope<TConnection>(IDbConnectionPool<TConnection>)
Takes a connection from the pool, opens it if needed, and returns a ConnectionScope<TConnection> that gives it back to the pool when the scope is disposed.
public static ConnectionScope<TConnection> EnsureOpenScope<TConnection>(this IDbConnectionPool<TConnection> connectionPool) where TConnection : IDbConnection
Parameters
connectionPoolIDbConnectionPool<TConnection>The connection pool to take the connection from.
Returns
- ConnectionScope<TConnection>
The connection, and the state it was in before it was opened.
Type Parameters
TConnectionThe type of the connection.
Remarks
The scope form of Open<T>(IDbConnectionPool, Func<IDbConnection, ConnectionState, T>): the same take, open if needed, give back, without the callback. Giving back is the pool's: it may close the connection, keep it, or dispose it. Prefer EnsureOpenScopeAsync(IDbConnectionPool, CancellationToken). If opening throws the connection is given back before the exception propagates.
Exceptions
- ArgumentNullException
connectionPoolis null.