Skip to content

Commit fdbc56e

Browse files
committed
Review feedback tweaks.
1 parent 533eb8b commit fdbc56e

12 files changed

Lines changed: 29 additions & 60 deletions

File tree

src/CoreEx.Database.Postgres/PostgresDatabase.cs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,17 @@
1818
/// <para>This class also implements the <see cref="IUnitOfWork"/> including a <see href="https://microservices.io/patterns/data/transactional-outbox.html">transactional outbox</see>. The <see cref="IUnitOfWork"/>
1919
/// functionality is enabled by the <see cref="PostgresUnitOfWorkInvoker"/>; note, this is not thread-safe.</para>
2020
/// </remarks>
21-
public partial class PostgresDatabase : Database<NpgsqlConnection, PostgresCommand, PostgresDatabaseArgs>
21+
/// <param name="dataSource">The <see cref="NpgsqlDataSource"/>.</param>
22+
/// <param name="jsonSerializerOptions">The optional <see cref="JsonSerializerOptions"/>.</param>
23+
/// <param name="logger">The optional <see cref="ILogger"/>.</param>
24+
public partial class PostgresDatabase(NpgsqlDataSource dataSource, JsonSerializerOptions? jsonSerializerOptions = null, ILogger<PostgresDatabase>? logger = null) : Database<NpgsqlConnection, PostgresCommand, PostgresDatabaseArgs, PostgresDatabaseColumns>(dataSource.CreateConnection(), PostgresInvoker.Default, PostgresDatabaseColumns.Default, jsonSerializerOptions, logger)
2225
{
2326
/// <summary>
2427
/// Gets the default <see cref="DuplicateErrorNumbers"/>.
2528
/// </summary>
2629
/// <remarks>See <see href="https://www.postgresql.org/docs/current/errcodes-appendix.html"/>.</remarks>
2730
public static string[] DefaultDuplicateErrorNumbers { get; } = ["23505"];
2831

29-
/// <summary>
30-
/// Initializes a new instance of the <see cref="PostgresDatabase"/> class.
31-
/// </summary>
32-
/// <param name="dataSource">The <see cref="NpgsqlDataSource"/>.</param>
33-
/// <param name="jsonSerializerOptions">The optional <see cref="JsonSerializerOptions"/>.</param>
34-
/// <param name="logger">The optional <see cref="ILogger"/>.</param>
35-
public PostgresDatabase(NpgsqlDataSource dataSource, JsonSerializerOptions? jsonSerializerOptions = null, ILogger<PostgresDatabase>? logger = null)
36-
: base(dataSource.CreateConnection(), PostgresInvoker.Default, jsonSerializerOptions, logger)
37-
=> NamedColumns = PostgresDatabaseColumns.Default with { };
38-
39-
/// <summary>
40-
/// Gets or sets the names of the convention-based <see cref="PostgresDatabaseColumns"/>.
41-
/// </summary>
42-
/// <remarks>Defaults from <see cref="PostgresDatabaseColumns.Default"/>.</remarks>
43-
public new PostgresDatabaseColumns NamedColumns
44-
{
45-
get => (PostgresDatabaseColumns)base.NamedColumns;
46-
set => base.NamedColumns = value;
47-
}
48-
4932
/// <inheritdoc/>
5033
public override ISourceConverter<string?> RowVersionConverter => EncodedStringToUInt32Converter.Default;
5134

src/CoreEx.Database.SqlServer/SqlServerDatabase.cs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
/// <para>This class also implements the <see cref="IUnitOfWork"/> including a <see href="https://microservices.io/patterns/data/transactional-outbox.html">transactional outbox</see>. The <see cref="IUnitOfWork"/>
1919
/// functionality is enabled by the <see cref="SqlServerUnitOfWorkInvoker"/>; note, this is not thread-safe.</para>
2020
/// </remarks>
21-
public partial class SqlServerDatabase : Database<SqlConnection, SqlServerCommand, SqlServerDatabaseArgs>
21+
/// <param name="connection">The <see cref="SqlConnection"/>.</param>
22+
/// <param name="jsonSerializerOptions">The optional <see cref="JsonSerializerOptions"/>.</param>
23+
/// <param name="logger">The optional <see cref="ILogger"/>.</param>
24+
public partial class SqlServerDatabase(SqlConnection connection, JsonSerializerOptions? jsonSerializerOptions = null, ILogger<SqlServerDatabase>? logger = null) : Database<SqlConnection, SqlServerCommand, SqlServerDatabaseArgs, SqlServerDatabaseColumns>(connection, SqlServerInvoker.Default, SqlServerDatabaseColumns.Default, jsonSerializerOptions, logger)
2225
{
2326
/// <summary>
2427
/// Gets the default <see cref="DuplicateErrorNumbers"/>.
@@ -27,26 +30,6 @@ public partial class SqlServerDatabase : Database<SqlConnection, SqlServerComman
2730
/// and <see href="https://docs.microsoft.com/en-us/azure/sql-database/sql-database-develop-error-messages"/>.</remarks>
2831
public static int[] DefaultDuplicateErrorNumbers { get; } = [2601, 2627];
2932

30-
/// <summary>
31-
/// Initializes a new instance of the <see cref="SqlServerDatabase"/> class.
32-
/// </summary>
33-
/// <param name="connection">The <see cref="SqlConnection"/>.</param>
34-
/// <param name="jsonSerializerOptions">The optional <see cref="JsonSerializerOptions"/>.</param>
35-
/// <param name="logger">The optional <see cref="ILogger"/>.</param>
36-
public SqlServerDatabase(SqlConnection connection, JsonSerializerOptions? jsonSerializerOptions = null, ILogger<SqlServerDatabase>? logger = null)
37-
: base(connection, SqlServerInvoker.Default, jsonSerializerOptions, logger)
38-
=> NamedColumns = SqlServerDatabaseColumns.Default with { };
39-
40-
/// <summary>
41-
/// Gets or sets the names of the convention-based <see cref="SqlServerDatabaseColumns"/>.
42-
/// </summary>
43-
/// <remarks>Defaults from <see cref="SqlServerDatabaseColumns.Default"/>.</remarks>
44-
public new SqlServerDatabaseColumns NamedColumns
45-
{
46-
get => (SqlServerDatabaseColumns)base.NamedColumns;
47-
set => base.NamedColumns = value;
48-
}
49-
5033
/// <inheritdoc/>
5134
public override ISourceConverter<string?> RowVersionConverter => StringBase64Converter.Default;
5235

src/CoreEx.Database/Abstractions/DatabaseArgsBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public abstract record class DatabaseArgsBase : IDataArgs
1717
/// Indicates whether to transform the underlying <see cref="DbException"/> into an <see cref="IExtendedException"/> equivalent.
1818
/// </summary>
1919
/// <remarks>Defaults to <see langword="true"/>.
20-
/// <para>The <see cref="Database{TConnection, TCommand, TDatabaseArgs}.OnDbException(DbException)"/> will be skipped where set to <see langword="false"/>.</para></remarks>
20+
/// <para>The <see cref="Database{TConnection, TCommand, TDatabaseArgs, TDatabaseColumns}.OnDbException(DbException)"/> will be skipped where set to <see langword="false"/>.</para></remarks>
2121
public bool TransformException { get; init; } = true;
2222
}

src/CoreEx.Database/Database.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
/// <typeparam name="TConnection">The <see cref="DbConnection"/> <see cref="Type"/>.</typeparam>
77
/// <typeparam name="TCommand">The <see cref="DatabaseCommand{TDatabaseArgs, TSelf}"/> <see cref="Type"/>.</typeparam>
88
/// <typeparam name="TDatabaseArgs">The <see cref="DatabaseArgs"/> <see cref="Type"/>.</typeparam>
9+
/// <typeparam name="TDatabaseColumns">The <see cref="DatabaseColumns"/> <see cref="Type"/>.</typeparam>
910
/// <param name="connection">The <typeparamref name="TConnection"/> <see cref="DbConnection"/>.</param>
1011
/// <param name="invoker">The <see cref="DatabaseInvoker"/>.</param>
12+
/// <param name="namedColumns">The convention-based column names.</param>
1113
/// <param name="jsonSerializerOptions">The optional <see cref="JsonSerializerOptions"/>.</param>
1214
/// <param name="logger">The optional <see cref="ILogger"/>.</param>
13-
public abstract class Database<TConnection, TCommand, TDatabaseArgs>(TConnection connection, DatabaseInvoker invoker, JsonSerializerOptions? jsonSerializerOptions = null, ILogger<Database<TConnection, TCommand, TDatabaseArgs>>? logger = null) : IDatabase, IDisposable
14-
where TConnection : DbConnection where TCommand : DatabaseCommand<TDatabaseArgs, TCommand> where TDatabaseArgs : DatabaseArgs, new()
15+
public abstract class Database<TConnection, TCommand, TDatabaseArgs, TDatabaseColumns>(TConnection connection, DatabaseInvoker invoker, TDatabaseColumns namedColumns, JsonSerializerOptions? jsonSerializerOptions = null, ILogger<Database<TConnection, TCommand, TDatabaseArgs, TDatabaseColumns>>? logger = null) : IDatabase, IDisposable
16+
where TConnection : DbConnection where TCommand : DatabaseCommand<TDatabaseArgs, TCommand> where TDatabaseArgs : DatabaseArgs, new() where TDatabaseColumns : DatabaseColumns
1517
{
1618
private static readonly TDatabaseArgs _defaultDbArgs = new();
17-
private static readonly DatabaseColumns _defaultColumns = new();
1819
private static readonly DatabaseWildcard _defaultWildcard = new();
1920

2021
private readonly SemaphoreSlim _semaphore = new(1, 1);
@@ -26,7 +27,7 @@ public abstract class Database<TConnection, TCommand, TDatabaseArgs>(TConnection
2627
public string DatabaseId { get; } = Guid.NewGuid().ToString();
2728

2829
/// <inheritdoc/>
29-
public ILogger? Logger { get; } = logger ?? ExecutionContext.GetService<ILogger<Database<TConnection, TCommand, TDatabaseArgs>>>();
30+
public ILogger? Logger { get; } = logger ?? ExecutionContext.GetService<ILogger<Database<TConnection, TCommand, TDatabaseArgs, TDatabaseColumns>>>();
3031

3132
/// <inheritdoc/>
3233
public DatabaseInvoker Invoker { get; } = invoker.ThrowIfNull();
@@ -46,11 +47,12 @@ public abstract class Database<TConnection, TCommand, TDatabaseArgs>(TConnection
4647
public bool DateTimeOffsetTransform { get; set; } = true;
4748

4849
/// <inheritdoc/>
49-
public DatabaseColumns NamedColumns
50-
{
51-
get => field ?? throw new InvalidOperationException($"{nameof(NamedColumns)} is not initialized.");
52-
set => field = value.ThrowIfNull();
53-
}
50+
DatabaseColumns IDatabase.NamedColumns => NamedColumns;
51+
52+
/// <summary>
53+
/// Gets or sets the names of the convention-based <see cref="Extended.DatabaseColumns"/>.
54+
/// </summary>
55+
public TDatabaseColumns NamedColumns { get; set; } = namedColumns.ThrowIfNull();
5456

5557
/// <summary>
5658
/// Gets or sets the <see cref="DatabaseWildcard"/> to enable wildcard replacement.

src/CoreEx.Database/IDatabase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public interface IDatabase
3737
bool DateTimeOffsetTransform { get; set; }
3838

3939
/// <summary>
40-
/// Gets or sets the names of the convention-based <see cref="Extended.DatabaseColumns"/>.
40+
/// Gets the names of the convention-based <see cref="Extended.DatabaseColumns"/>.
4141
/// </summary>
42-
DatabaseColumns NamedColumns { get; set; }
42+
DatabaseColumns NamedColumns { get; }
4343

4444
/// <summary>
4545
/// Gets or sets the <see cref="DatabaseWildcard"/> to enable wildcard replacement.

src/CoreEx.Database/SqlStatement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static SqlStatement FromResource(string resourceName, Assembly? assembly
4848
public static SqlStatement FromResource<TResource>(string resourceName) => FromResource(resourceName, typeof(TResource).Assembly);
4949

5050
/// <summary>
51-
/// Initializes a new instance of the <see cref="SqlStatement"/> struct.
51+
/// Initializes a new instance of the <see cref="SqlStatement"/> class.
5252
/// </summary>
5353
/// <param name="commandType">The <see cref="System.Data.CommandType"/>.</param>
5454
/// <param name="commandText">The command text.</param>

src/CoreEx.UnitTesting/GlobalUsing.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
global using DbEx.SqlServer.Migration;
2323
global using AwesomeAssertions;
2424
global using Microsoft.Extensions.DependencyInjection;
25+
global using System.Collections.Concurrent;
2526
global using System.Diagnostics.CodeAnalysis;
2627
global using System.Reflection;
2728
global using System.Text;

src/CoreEx.UnitTesting/UnitTestExExtensions.Postgres.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static async Task MigratePostgresDataAsync(this TesterBase tester, Func<M
5555
{
5656
// Determine the connection string and configure the migration args.
5757
var cs = CoreEx.Abstractions.Internal.GetValueFromConfigurationWhereApplicable(connectionString.ThrowIfNullOrEmpty(), tester.ThrowIfNull().Configuration);
58-
var ma = new MigrationArgs(_connectionStrings.Add(cs) ? MigrationCommand.All | MigrationCommand.ResetAndData : MigrationCommand.ResetAndData, cs);
58+
var ma = new MigrationArgs(_connectionStrings.TryAdd(cs, 0) ? MigrationCommand.All | MigrationCommand.ResetAndData : MigrationCommand.ResetAndData, cs);
5959

6060
if (configureMigrationArgs is not null)
6161
ma = configureMigrationArgs(ma);

src/CoreEx.UnitTesting/UnitTestExExtensions.SqlServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static async Task MigrateSqlServerDataAsync(this TesterBase tester, Func<
5555
{
5656
// Determine the connection string and configure the migration args.
5757
var cs = CoreEx.Abstractions.Internal.GetValueFromConfigurationWhereApplicable(connectionString.ThrowIfNullOrEmpty(), tester.ThrowIfNull().Configuration);
58-
var ma = new MigrationArgs(_connectionStrings.Add(cs) ? MigrationCommand.All | MigrationCommand.ResetAndData : MigrationCommand.ResetAndData, cs);
58+
var ma = new MigrationArgs(_connectionStrings.TryAdd(cs, 0) ? MigrationCommand.All | MigrationCommand.ResetAndData : MigrationCommand.ResetAndData, cs);
5959

6060
if (configureMigrationArgs is not null)
6161
ma = configureMigrationArgs(ma);

src/CoreEx.UnitTesting/UnitTestExExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace UnitTestEx;
77
/// </summary>
88
public static partial class UnitTestExExtensions
99
{
10-
private static readonly HashSet<string> _connectionStrings = [];
10+
private static readonly ConcurrentDictionary<string, byte> _connectionStrings = [];
1111

1212
/// <summary>
1313
/// Enables an <see cref="ExecutionContext"/> instance to be tested managed within a <see cref="TesterBase.Services"/> <see cref="ServiceProviderServiceExtensions.CreateScope(IServiceProvider)"/>.

0 commit comments

Comments
 (0)