Last active
September 16, 2018 18:26
-
-
Save manisero/3bf4b005b5f8724402a4ae0aa248ecc1 to your computer and use it in GitHub Desktop.
DbConnection resolver for both transaction and no-transaction scenarios.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using System; | |
// using System.Data.Common; | |
public interface IDbConnectionResolver | |
{ | |
IDbConnectionWrapper Resolve(); | |
} | |
public interface IDbConnectionWrapper : IDisposable | |
{ | |
DbConnection Connection { get; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using System; | |
// using System.Data.Common; | |
/// <summary> | |
/// Should be used inside transaction, new instance should be created for each transaction. | |
/// Creates single connection and returns it on each <see cref="Resolve"/> invocation. | |
/// Disposes the connection on <see cref="Dispose"/>. | |
/// <see cref="ConnectionWrapper.Dispose"/> does nothing. | |
/// </summary> | |
public class LongLivingSqlConnectionResolver : IDbConnectionResolver, IDisposable | |
{ | |
private class ConnectionWrapper : IDbConnectionWrapper | |
{ | |
public ConnectionWrapper( | |
DbConnection connection) | |
{ | |
Connection = connection; | |
} | |
public DbConnection Connection { get; } | |
public void Dispose() { } | |
} | |
private readonly Lazy<DbConnection> _connection; | |
public LongLivingSqlConnectionResolver( | |
Func<DbConnection> connectionFactory) | |
{ | |
_connection = new Lazy<DbConnection>(connectionFactory); | |
} | |
public IDbConnectionWrapper Resolve() | |
{ | |
return new ConnectionWrapper(_connection.Value); | |
} | |
public void Dispose() | |
{ | |
if (_connection.IsValueCreated) | |
{ | |
_connection.Value.Dispose(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using System; | |
// using System.Data.Common; | |
/// <summary> | |
/// Should be used outside transaction, can be singleton. | |
/// Returns new connection on each <see cref="Resolve"/> invocation. | |
/// The connection is disposed on <see cref="ConnectionWrapper.Dispose"/>. | |
/// </summary> | |
public class ShortLivingSqlConnectionResolver : IDbConnectionResolver | |
{ | |
private class ConnectionWrapper : IDbConnectionWrapper | |
{ | |
public ConnectionWrapper( | |
DbConnection connection) | |
{ | |
Connection = connection; | |
} | |
public DbConnection Connection { get; } | |
public void Dispose() => Connection.Dispose(); | |
} | |
private readonly Func<DbConnection> _connectionFactory; | |
public ShortLivingSqlConnectionResolver( | |
Func<DbConnection> connectionFactory) | |
{ | |
_connectionFactory = connectionFactory; | |
} | |
public IDbConnectionWrapper Resolve() | |
{ | |
var connection = _connectionFactory(); | |
return new ConnectionWrapper(connection); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment