-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIDbConnectionFactory.cs
113 lines (97 loc) · 4.09 KB
/
IDbConnectionFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Data.Common;
namespace Open.Database.Extensions;
/// <summary>
/// Common interface for creating a connection. Can easily be used with dependency injection.
/// </summary>
public interface IDbConnectionFactory
{
/// <summary>
/// Creates a new connection ready for use.
/// </summary>
/// <returns>An IDbConnection.</returns>
IDbConnection Create();
}
/// <inheritdoc />
/// <typeparam name="TConnection">The actual connection type.</typeparam>
public interface IDbConnectionFactory<out TConnection> : IDbConnectionFactory
where TConnection : IDbConnection
{
/// <inheritdoc />
/// <returns>An connection of type <typeparamref name="TConnection"/>.</returns>
new TConnection Create();
}
/// <summary>
/// Extensions for converting a connection factory into a pool.
/// </summary>
public static class DbConnectionFactoryExtensions
{
/// <summary>
/// A struct that represents a connection factory that can be used as a pool.
/// </summary>
public readonly struct ConnectionFactoryToPoolAdapter(Func<IDbConnection> factory) : IDbConnectionPool
{
/// <summary>
/// Constructs a connection factory to pool adapter.
/// </summary>
public ConnectionFactoryToPoolAdapter(IDbConnectionFactory factory)
: this(factory.Create) { }
/// <inheritdoc />
public IDbConnection Take()
=> factory();
/// <inheritdoc />
public void Give(IDbConnection connection)
=> connection.Dispose();
}
/// <inheritdoc cref="ConnectionFactoryToPoolAdapter(Func{IDbConnection})" />/>
public readonly struct ConnectionFactoryToPoolAdapter<TConnection>(Func<TConnection> factory) : IDbConnectionPool<TConnection>
where TConnection : IDbConnection
{
/// <summary>
/// Constructs a connection factory to pool adapter.
/// </summary>
public ConnectionFactoryToPoolAdapter(IDbConnectionFactory<TConnection> factory)
: this(factory.Create) { }
/// <inheritdoc />
public TConnection Take()
=> factory();
IDbConnection IDbConnectionPool.Take()
=> Take();
/// <inheritdoc />
public void Give(IDbConnection connection)
=> connection.Dispose();
}
/// <summary>
/// Provides a connection pool that simply creates from a connection factory and disposes when returned.
/// </summary>
/// <param name="connectionFactory">The connection factory to generate connections from.</param>
/// <returns>An <see cref="ConnectionFactoryToPoolAdapter"/> to handle this factory.</returns>
public static ConnectionFactoryToPoolAdapter AsPool(this IDbConnectionFactory connectionFactory)
=> new (connectionFactory);
/// <inheritdoc cref="AsPool(IDbConnectionFactory)"/>
public static ConnectionFactoryToPoolAdapter AsPool(this Func<IDbConnection> connectionFactory)
=> new(connectionFactory);
/// <summary>
/// Provides a connection pool that simply creates from a connection factory and disposes when returned.
/// </summary>
/// <param name="connectionFactory">The connection factory to generate connections from.</param>
/// <returns>An <see cref="ConnectionFactoryToPoolAdapter{TConnection}"/> to handle this factory.</returns>
public static ConnectionFactoryToPoolAdapter<TConnection> AsPool<TConnection>(this IDbConnectionFactory<TConnection> connectionFactory)
where TConnection : IDbConnection
=> new (connectionFactory);
/// <inheritdoc cref="AsPool{TConnection}(IDbConnectionFactory{TConnection})"/>
public static ConnectionFactoryToPoolAdapter<TConnection> AsPool<TConnection>(this Func<TConnection> connectionFactory)
where TConnection : IDbConnection
=> new(connectionFactory);
/// <summary>
/// Coerces a non-generic connection factory to a generic one.
/// </summary>
/// <param name="connectionFactory">The source connection factory.</param>
/// <returns>The generic version of the source factory.</returns>
public static IDbConnectionFactory<IDbConnection> AsGeneric(this IDbConnectionFactory connectionFactory)
{
if (connectionFactory is null) throw new ArgumentNullException(nameof(connectionFactory));
Contract.EndContractBlock();
return connectionFactory is IDbConnectionFactory<IDbConnection> p ? p
: new DbConnectionFactory<IDbConnection>(connectionFactory.Create);
}
}