• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 26923300515

04 Jun 2026 01:03AM UTC coverage: 65.014% (+9.9%) from 55.157%
26923300515

push

github

pwelter34
Merge branch 'master' of https://github.com/loresoft/FluentCommand

1728 of 3450 branches covered (50.09%)

Branch coverage included in aggregate %.

5510 of 7683 relevant lines covered (71.72%)

297.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/src/FluentCommand/DataConfiguration.cs
1
using System.Data.Common;
2
using System.Text.Json;
3

4
using FluentCommand.Query.Generators;
5

6
namespace FluentCommand;
7

8
/// <summary>
9
/// The database configuration
10
/// </summary>
11
/// <seealso cref="IDataConfiguration" />
12
public class DataConfiguration : IDataConfiguration
13
{
14
    /// <summary>
15
    /// Initializes a new instance of the <see cref="DataConfiguration" /> class.
16
    /// </summary>
17
    /// <param name="providerFactory">The database provider factory.</param>
18
    /// <param name="connectionString">The database connection string.</param>
19
    /// <param name="cache">The data cache manager.</param>
20
    /// <param name="queryGenerator">The query generator.</param>
21
    /// <param name="queryLogger">The query command logger.</param>
22
    /// <param name="commandTimeout">The default command timeout in seconds.</param>
23
    /// <param name="interceptors">The interceptors to apply to each session created from this configuration.</param>
24
    /// <param name="jsonSerializerOptions">The JSON serializer options used by generated JSON column readers.</param>
25
    /// <exception cref="ArgumentNullException">The <paramref name="providerFactory"/> is null</exception>
26
    public DataConfiguration(
11✔
27
        DbProviderFactory providerFactory,
11✔
28
        string connectionString,
11✔
29
        IDataCache? cache = null,
11✔
30
        IQueryGenerator? queryGenerator = null,
11✔
31
        IDataQueryLogger? queryLogger = null,
11✔
32
        IEnumerable<IDataInterceptor>? interceptors = null,
11✔
33
        int? commandTimeout = null,
11✔
34
        JsonSerializerOptions? jsonSerializerOptions = null)
11✔
35
    {
36
        ProviderFactory = providerFactory ?? throw new ArgumentNullException(nameof(providerFactory));
11!
37
        ConnectionString = connectionString;
11✔
38
        QueryLogger = queryLogger;
11✔
39
        CommandTimeout = commandTimeout;
11✔
40
        DataCache = cache;
11✔
41
        QueryGenerator = queryGenerator ?? new SqlServerGenerator();
11✔
42
        Interceptors = interceptors ?? [];
11✔
43
        JsonSerializerOptions = jsonSerializerOptions;
11✔
44
    }
11✔
45

46
    /// <summary>
47
    /// Gets the database provider factory.
48
    /// </summary>
49
    /// <value>
50
    /// The provider factory.
51
    /// </value>
52
    public virtual DbProviderFactory ProviderFactory { get; }
53

54
    /// <summary>
55
    /// Gets the database connection string.
56
    /// </summary>
57
    /// <value>
58
    /// The connection string.
59
    /// </value>
60
    public virtual string ConnectionString { get; }
61

62
    /// <summary>
63
    /// Gets the data command query logger.
64
    /// </summary>
65
    /// <value>
66
    /// The data command query logger.
67
    /// </value>
68
    public IDataQueryLogger? QueryLogger { get; }
69

70
    /// <summary>
71
    /// Gets the default command timeout in seconds.
72
    /// </summary>
73
    /// <value>
74
    /// The default command timeout in seconds.
75
    /// </value>
76
    public int? CommandTimeout { get; }
77

78
    /// <summary>
79
    /// Gets the data cache manager.
80
    /// </summary>
81
    /// <value>
82
    /// The data cache manager.
83
    /// </value>
84
    public virtual IDataCache? DataCache { get; }
85

86
    /// <summary>
87
    /// Gets the query generator provider.
88
    /// </summary>
89
    /// <value>
90
    /// The query generator provider.
91
    /// </value>
92
    public IQueryGenerator QueryGenerator { get; }
93

94
    /// <summary>
95
    /// Gets the registered interceptors for this configuration.
96
    /// </summary>
97
    /// <value>
98
    /// The list of <see cref="IDataInterceptor"/> instances applied to each session created from this configuration.
99
    /// </value>
100
    public IEnumerable<IDataInterceptor> Interceptors { get; }
101

102
    /// <summary>
103
    /// Gets the JSON serializer options used by generated JSON column readers.
104
    /// </summary>
105
    public JsonSerializerOptions? JsonSerializerOptions { get; }
106

107
    /// <summary>
108
    /// Creates a new data session from this database configuration
109
    /// </summary>
110
    /// <param name="connectionString">The connection string to use for the session.  If <paramref name="connectionString" /> is <c>null</c>, <see cref="ConnectionString" /> will be used.</param>
111
    /// <returns>
112
    /// A new <see cref="IDataSession" /> instance.
113
    /// </returns>
114
    public virtual IDataSession CreateSession(string? connectionString = null)
115
    {
116
        var connection = CreateConnection(connectionString);
5✔
117
        return new DataSession(connection, true, DataCache, QueryGenerator, QueryLogger, Interceptors, CommandTimeout, JsonSerializerOptions);
5✔
118
    }
119

120
    /// <summary>
121
    /// Creates a new data session from this database configuration
122
    /// </summary>
123
    /// <param name="transaction">The transaction to create the session with.</param>
124
    /// <returns>
125
    /// A new <see cref="IDataSession" /> instance.
126
    /// </returns>
127
    /// <exception cref="System.ArgumentNullException">transaction is null</exception>
128
    /// <exception cref="System.ArgumentException">The specified transaction doesn't have a vaild connection </exception>
129
    public IDataSession CreateSession(DbTransaction transaction)
130
    {
131
        ArgumentNullException.ThrowIfNull(transaction);
×
132

133
        if (transaction.Connection == null)
×
134
            throw new ArgumentException("The specified transaction doesn't have a vaild connection", nameof(transaction));
×
135

136
        return new DataSession(transaction, false, DataCache, QueryGenerator, QueryLogger, Interceptors, CommandTimeout, JsonSerializerOptions);
×
137
    }
138

139
    /// <summary>
140
    /// Creates a new <see cref="DbConnection" /> instance from this database configuration.
141
    /// </summary>
142
    /// <param name="connectionString"></param>
143
    /// <returns>
144
    /// A new <see cref="DbConnection" /> instance.
145
    /// </returns>
146
    /// <exception cref="InvalidOperationException">Database provider factory failed to create a connection object.</exception>
147
    /// <exception cref="ArgumentException">The connection string is invalid</exception>
148
    public virtual DbConnection CreateConnection(string? connectionString = null)
149
    {
150
        var connection = ProviderFactory.CreateConnection();
209✔
151
        if (connection == null)
209!
152
            throw new InvalidOperationException("Database provider factory failed to create a connection object.");
×
153

154
        connectionString ??= ConnectionString;
209!
155
        if (string.IsNullOrEmpty(connectionString))
209!
156
            throw new ArgumentException("The connection string is invalid");
×
157

158
        connection.ConnectionString = connectionString;
209✔
159
        return connection;
209✔
160
    }
161
}
162

163

164
/// <summary>
165
/// The database configuration by discriminator.  Used to register multiple instances of IDataConfiguration.
166
/// </summary>
167
/// <typeparam name="TDiscriminator">The type of the discriminator.</typeparam>
168
/// <seealso cref="FluentCommand.IDataConfiguration" />
169
public class DataConfiguration<TDiscriminator> : DataConfiguration, IDataConfiguration<TDiscriminator>
170
{
171
    /// <summary>
172
    /// Initializes a new instance of the <see cref="DataConfiguration{TDiscriminator}"/> class.
173
    /// </summary>
174
    /// <param name="providerFactory">The database provider factory.</param>
175
    /// <param name="connectionString">The database connection string.</param>
176
    /// <param name="cache">The data cache manager.</param>
177
    /// <param name="queryGenerator">The query generator.</param>
178
    /// <param name="queryLogger">The query command logger.</param>
179
    /// <param name="commandTimeout">The default command timeout in seconds.</param>
180
    /// <param name="interceptors">The interceptors to apply during this configuration's lifetime.</param>
181
    /// <param name="jsonSerializerOptions">The JSON serializer options used by generated JSON column readers.</param>
182
    public DataConfiguration(
183
        DbProviderFactory providerFactory,
184
        string connectionString,
185
        IDataCache? cache = null,
186
        IQueryGenerator? queryGenerator = null,
187
        IDataQueryLogger? queryLogger = null,
188
        IEnumerable<IDataInterceptor>? interceptors = null,
189
        int? commandTimeout = null,
190
        JsonSerializerOptions? jsonSerializerOptions = null)
191
        : base(providerFactory, connectionString, cache, queryGenerator, queryLogger, interceptors, commandTimeout, jsonSerializerOptions)
2✔
192
    {
193
    }
2✔
194
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc