public async Task InvokeOnConnection(ConnectionName connectionName, ConnectionIntent intent, Func <DbConnection, Task> action) { using (var connection = await GetConnection(connectionName, intent)) { await action(connection); } }
public async Task <T> InvokeOnConnection <T>(ConnectionName connectionName, ConnectionIntent intent, Func <DbConnection, Task <T> > func) { using (var connection = await GetConnection(connectionName, intent)) { return(await func(connection)); } }
private async Task <DbConnection> GetConnection(ConnectionName connectionName, ConnectionIntent intent) { ApplicationIntent applicationIntent; switch (intent) { case ConnectionIntent.ReadOnly: applicationIntent = ApplicationIntent.ReadOnly; break; case ConnectionIntent.ReadWrite: applicationIntent = ApplicationIntent.ReadWrite; break; default: throw new ArgumentOutOfRangeException(nameof(intent), $"{intent} is not a valid value for {typeof(ConnectionIntent)}"); } var conn = await OpenConnection(connectionName, applicationIntent).ConfigureAwait(false); if (_storeConnections) { _dbConnections.Push(conn); } return(conn); }