Skip to content

Commit ece018f

Browse files
author
electricessence
committed
Added generic DbConnectionFactory that takes a factory function.
1 parent 487529f commit ece018f

File tree

7 files changed

+124
-60
lines changed

7 files changed

+124
-60
lines changed

DbConnectionFactory.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Data;
3+
using System.Data.Common;
4+
5+
namespace Open.Database.Extensions
6+
{
7+
/// <summary>
8+
/// Generic connection factory implementation that accepts a factory function.
9+
/// </summary>
10+
/// <typeparam name="TConnection"></typeparam>
11+
public class DbConnectionFactory<TConnection> : IDbConnectionFactory<TConnection>
12+
where TConnection:DbConnection
13+
{
14+
readonly Func<TConnection> _factory;
15+
16+
/// <summary>
17+
/// Constructs a DbConnectionFactory.
18+
/// </summary>
19+
/// <param name="factory"></param>
20+
public DbConnectionFactory(Func<TConnection> factory)
21+
{
22+
_factory = factory ?? throw new ArgumentNullException(nameof(factory));
23+
}
24+
25+
/// <summary>
26+
/// Creates a connection of from the underlying factory function.
27+
/// </summary>
28+
public virtual TConnection Create() => _factory();
29+
30+
IDbConnection IDbConnectionFactory.Create() => Create();
31+
}
32+
}

Documentation.xml

Lines changed: 43 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ExpressiveDbCommand.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Data;
43
using System.Data.Common;
54
using System.Linq;
6-
using System.Threading.Tasks;
75

86
namespace Open.Database.Extensions
97
{

ExpressiveDbCommandBase.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,31 @@ public Task<int> ExecuteNonQueryAsync()
111111
/// <summary>
112112
/// Calls ExecuteScalarAsync on the underlying command.
113113
/// </summary>
114-
/// <returns>The varlue returned from the method.</returns>
114+
/// <returns>The value returned from the method.</returns>
115115
public Task<object> ExecuteScalarAsync()
116116
=> ExecuteAsync(command => command.ExecuteScalarAsync());
117117

118118
/// <summary>
119119
/// Asynchronously executes scalar on the underlying command.
120120
/// </summary>
121121
/// <typeparam name="T">The type expected.</typeparam>
122-
/// <returns>The varlue returned from the method.</returns>
122+
/// <returns>The value returned from the method.</returns>
123123
public async Task<T> ExecuteScalarAsync<T>(Func<object, T> transform)
124124
=> transform(await ExecuteScalarAsync());
125125

126+
/// <summary>
127+
/// Asynchronously executes scalar on the underlying command and casts to the expected type.
128+
/// </summary>
129+
/// <typeparam name="T">The type expected.</typeparam>
130+
/// <returns>The value returned from the method.</returns>
131+
public async Task<T> ExecuteScalarAsync<T>()
132+
=> (T)(await ExecuteScalarAsync());
133+
126134
/// <summary>
127135
/// Asynchronously executes scalar on the underlying command.
128136
/// </summary>
129137
/// <typeparam name="T">The type expected.</typeparam>
130-
/// <returns>The varlue returned from the method.</returns>
138+
/// <returns>The value returned from the method.</returns>
131139
public async Task<T> ExecuteScalarAsync<T>(Func<object, Task<T>> transform)
132140
=> await transform(await ExecuteScalarAsync());
133141

IDbConnectionFactory.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22

33
namespace Open.Database.Extensions
44
{
5-
/// <summary>
6-
/// Base interface for creating connections.
7-
/// Useful for dependency injection.
8-
/// </summary>
9-
/// <typeparam name="TConnection">The actual connection type.</typeparam>
10-
public interface IDbConnectionFactory<out TConnection>
5+
/// <summary>
6+
/// Simplified interface with IDbConnection as the generic type.
7+
/// </summary>
8+
public interface IDbConnectionFactory
9+
{
10+
/// <summary>
11+
/// Generates a new connection of declared generic type.
12+
/// </summary>
13+
/// <returns></returns>
14+
IDbConnection Create();
15+
}
16+
17+
/// <summary>
18+
/// Base interface for creating connections.
19+
/// Useful for dependency injection.
20+
/// </summary>
21+
/// <typeparam name="TConnection">The actual connection type.</typeparam>
22+
public interface IDbConnectionFactory<out TConnection> : IDbConnectionFactory
1123
where TConnection : IDbConnection
1224
{
1325
/// <summary>
1426
/// Generates a new connection of declared generic type.
1527
/// </summary>
1628
/// <returns></returns>
17-
TConnection Create();
29+
new TConnection Create();
1830
}
1931

20-
/// <summary>
21-
/// Simplified interface with IDbConnection as the generic type.
22-
/// </summary>
23-
public interface IDbConnectionFactory
24-
: IDbConnectionFactory<IDbConnection>
25-
{
26-
27-
}
32+
2833
}

Open.Database.Extensions.csproj

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
<Description>Useful set of utilities and abstractions for simplifying modern database operations and ensuring DI compatibility.</Description>
1212
<RepositoryUrl>https://github.com/electricessence/Open.Database.Extensions</RepositoryUrl>
1313
<RepositoryType>git</RepositoryType>
14-
<Version>5.4.0</Version>
15-
<AssemblyVersion>5.4.0.0</AssemblyVersion>
16-
<FileVersion>5.4.0.0</FileVersion>
17-
<PackageReleaseNotes>Refactored code that was exclusive to SqlClient to be part of broarder Db classes and extensions.
18-
Provides 'async' functionality for all DbDataReader and DbCommand classes.</PackageReleaseNotes>
14+
<Version>5.4.1</Version>
15+
<AssemblyVersion>5.4.1.0</AssemblyVersion>
16+
<FileVersion>5.4.1.0</FileVersion>
17+
<PackageReleaseNotes>Tweaks to interfaces and restored ExecuteScalarAsync&lt;T&gt;().</PackageReleaseNotes>
1918
</PropertyGroup>
2019

2120
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">

SqlClient/SqlConnectionFactory.cs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Data;
1+
using System;
2+
using System.Data;
23
using System.Data.SqlClient;
34

45
namespace Open.Database.Extensions.SqlClient
@@ -7,31 +8,22 @@ namespace Open.Database.Extensions.SqlClient
78
/// <summary>
89
/// Default SqlConnectionFactory for generating SqlConnections.
910
/// </summary>
10-
public class SqlConnectionFactory : IDbConnectionFactory, IDbConnectionFactory<SqlConnection>
11+
public class SqlConnectionFactory : DbConnectionFactory<SqlConnection>
1112
{
12-
string _connectionString;
13-
14-
/// <summary>
15-
/// Default injectable connection factory constructor.
16-
/// </summary>
17-
/// <param name="connectionString">Required connection string value.</param>
18-
public SqlConnectionFactory(string connectionString)
13+
/// <summary>
14+
/// Default injectable connection factory constructor.
15+
/// </summary>
16+
/// <param name="factory">The factory that generates the connections.</param>
17+
public SqlConnectionFactory(Func<SqlConnection> factory) : base(factory)
1918
{
20-
_connectionString = connectionString;
2119
}
2220

23-
/// <summary>
24-
/// Method for generating SqlConnections.
25-
/// </summary>
26-
/// <returns></returns>
27-
public SqlConnection Create()
21+
/// <summary>
22+
/// Default injectable connection factory constructor that accepts a connection string.
23+
/// </summary>
24+
/// <param name="connectionString">Required connection string value.</param>
25+
public SqlConnectionFactory(string connectionString) : base (()=>new SqlConnection(connectionString))
2826
{
29-
return new SqlConnection(_connectionString);
3027
}
31-
32-
IDbConnection IDbConnectionFactory<IDbConnection>.Create()
33-
{
34-
return Create();
35-
}
3628
}
3729
}

0 commit comments

Comments
 (0)