From 5938f9d6a49e423a8b81185295c3404712d37e17 Mon Sep 17 00:00:00 2001 From: Jean Gatto Date: Mon, 1 Sep 2025 11:03:41 -0300 Subject: [PATCH] refactor: rename variables for clarity and improve health check configuration --- src/Shop.Core/Extensions/JsonExtensions.cs | 28 +---------------- .../PrivateConstructorContractResolver.cs | 31 +++++++++++++++++++ .../ServicesCollectionExtensions.cs | 18 +++++------ 3 files changed, 40 insertions(+), 37 deletions(-) create mode 100644 src/Shop.Core/JsonResolvers/PrivateConstructorContractResolver.cs diff --git a/src/Shop.Core/Extensions/JsonExtensions.cs b/src/Shop.Core/Extensions/JsonExtensions.cs index 6484dfc..54afd35 100644 --- a/src/Shop.Core/Extensions/JsonExtensions.cs +++ b/src/Shop.Core/Extensions/JsonExtensions.cs @@ -1,8 +1,7 @@ using System; -using System.Reflection; using System.Text.Json; using System.Text.Json.Serialization; -using System.Text.Json.Serialization.Metadata; +using Shop.Core.JsonResolvers; namespace Shop.Core.Extensions; @@ -45,28 +44,3 @@ public static class JsonExtensions return jsonSettings; } } - -internal sealed class PrivateConstructorContractResolver : DefaultJsonTypeInfoResolver -{ - /// - /// Gets the JSON type information for the specified type, with support for creating objects with private constructors. - /// - /// The type to get the JSON type information for. - /// The to use for serialization. - /// The JSON type information for the specified type. - public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options) - { - var jsonTypeInfo = base.GetTypeInfo(type, options); - - // Check if the type is an object, has no public constructor, and CreateObject is not already set - if (jsonTypeInfo.Kind == JsonTypeInfoKind.Object - && jsonTypeInfo.CreateObject is null - && jsonTypeInfo.Type.GetConstructors(BindingFlags.Public | BindingFlags.Instance).Length == 0) - { - // Set CreateObject to a lambda expression that creates an instance using a private constructor - jsonTypeInfo.CreateObject = () => Activator.CreateInstance(jsonTypeInfo.Type, true); - } - - return jsonTypeInfo; - } -} \ No newline at end of file diff --git a/src/Shop.Core/JsonResolvers/PrivateConstructorContractResolver.cs b/src/Shop.Core/JsonResolvers/PrivateConstructorContractResolver.cs new file mode 100644 index 0000000..738b198 --- /dev/null +++ b/src/Shop.Core/JsonResolvers/PrivateConstructorContractResolver.cs @@ -0,0 +1,31 @@ +using System; +using System.Reflection; +using System.Text.Json; +using System.Text.Json.Serialization.Metadata; + +namespace Shop.Core.JsonResolvers; + +public sealed class PrivateConstructorContractResolver : DefaultJsonTypeInfoResolver +{ + /// + /// Gets the JSON type information for the specified type, with support for creating objects with private constructors. + /// + /// The type to get the JSON type information for. + /// The to use for serialization. + /// The JSON type information for the specified type. + public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options) + { + var jsonTypeInfo = base.GetTypeInfo(type, options); + + // Check if the type is an object, has no public constructor, and CreateObject is not already set + if (jsonTypeInfo.Kind == JsonTypeInfoKind.Object + && jsonTypeInfo.CreateObject is null + && jsonTypeInfo.Type.GetConstructors(BindingFlags.Public | BindingFlags.Instance).Length == 0) + { + // Set CreateObject to a lambda expression that creates an instance using a private constructor + jsonTypeInfo.CreateObject = () => Activator.CreateInstance(jsonTypeInfo.Type, true); + } + + return jsonTypeInfo; + } +} \ No newline at end of file diff --git a/src/Shop.PublicApi/Extensions/ServicesCollectionExtensions.cs b/src/Shop.PublicApi/Extensions/ServicesCollectionExtensions.cs index acb70e5..1c25df6 100644 --- a/src/Shop.PublicApi/Extensions/ServicesCollectionExtensions.cs +++ b/src/Shop.PublicApi/Extensions/ServicesCollectionExtensions.cs @@ -29,16 +29,16 @@ internal static class ServicesCollectionExtensions public static IServiceCollection AddHealthChecks(this IServiceCollection services, IConfiguration configuration) { - var options = configuration.GetOptions(); + var connectionOptions = configuration.GetOptions(); var healthCheckBuilder = services .AddHealthChecks() .AddDbContextCheck(tags: DbRelationalTags) .AddDbContextCheck(tags: DbRelationalTags) - .AddMongoDb(clientFactory: _ => new MongoClient(options.NoSqlConnection), tags: DbNoSqlTags); + .AddMongoDb(clientFactory: _ => new MongoClient(connectionOptions.NoSqlConnection), tags: DbNoSqlTags); - if (!options.CacheConnectionInMemory()) - healthCheckBuilder.AddRedis(options.CacheConnection); + if (!connectionOptions.CacheConnectionInMemory()) + healthCheckBuilder.AddRedis(connectionOptions.CacheConnection); return services; } @@ -48,12 +48,10 @@ internal static class ServicesCollectionExtensions if (!environment.IsEnvironment(TestingEnvironmentName)) { services.AddDbContextPool((serviceProvider, optionsBuilder) => - ConfigureDbContext( - serviceProvider, optionsBuilder, QueryTrackingBehavior.TrackAll)); + ConfigureDbContext(serviceProvider, optionsBuilder, QueryTrackingBehavior.TrackAll)); services.AddDbContextPool((serviceProvider, optionsBuilder) => - ConfigureDbContext( - serviceProvider, optionsBuilder, QueryTrackingBehavior.NoTrackingWithIdentityResolution)); + ConfigureDbContext(serviceProvider, optionsBuilder, QueryTrackingBehavior.NoTrackingWithIdentityResolution)); } return services; @@ -85,13 +83,13 @@ internal static class ServicesCollectionExtensions DbContextOptionsBuilder optionsBuilder, QueryTrackingBehavior queryTrackingBehavior) where TDbContext : DbContext { + var connectionOptions = serviceProvider.GetOptions(); var logger = serviceProvider.GetRequiredService>(); - var options = serviceProvider.GetOptions(); var environment = serviceProvider.GetRequiredService(); var envIsDevelopment = environment.IsDevelopment(); optionsBuilder - .UseSqlServer(options.SqlConnection, sqlServerOptions => + .UseSqlServer(connectionOptions.SqlConnection, sqlServerOptions => { sqlServerOptions .MigrationsAssembly(DbMigrationAssemblyName)