refactor: rename variables for clarity and improve health check configuration
This commit is contained in:
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the JSON type information for the specified type, with support for creating objects with private constructors.
|
||||
/// </summary>
|
||||
/// <param name="type">The type to get the JSON type information for.</param>
|
||||
/// <param name="options">The <see cref="JsonSerializerOptions"/> to use for serialization.</param>
|
||||
/// <returns>The JSON type information for the specified type.</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the JSON type information for the specified type, with support for creating objects with private constructors.
|
||||
/// </summary>
|
||||
/// <param name="type">The type to get the JSON type information for.</param>
|
||||
/// <param name="options">The <see cref="JsonSerializerOptions"/> to use for serialization.</param>
|
||||
/// <returns>The JSON type information for the specified type.</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -29,16 +29,16 @@ internal static class ServicesCollectionExtensions
|
||||
|
||||
public static IServiceCollection AddHealthChecks(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var options = configuration.GetOptions<ConnectionOptions>();
|
||||
var connectionOptions = configuration.GetOptions<ConnectionOptions>();
|
||||
|
||||
var healthCheckBuilder = services
|
||||
.AddHealthChecks()
|
||||
.AddDbContextCheck<WriteDbContext>(tags: DbRelationalTags)
|
||||
.AddDbContextCheck<EventStoreDbContext>(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<WriteDbContext>((serviceProvider, optionsBuilder) =>
|
||||
ConfigureDbContext<WriteDbContext>(
|
||||
serviceProvider, optionsBuilder, QueryTrackingBehavior.TrackAll));
|
||||
ConfigureDbContext<WriteDbContext>(serviceProvider, optionsBuilder, QueryTrackingBehavior.TrackAll));
|
||||
|
||||
services.AddDbContextPool<EventStoreDbContext>((serviceProvider, optionsBuilder) =>
|
||||
ConfigureDbContext<EventStoreDbContext>(
|
||||
serviceProvider, optionsBuilder, QueryTrackingBehavior.NoTrackingWithIdentityResolution));
|
||||
ConfigureDbContext<EventStoreDbContext>(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<ConnectionOptions>();
|
||||
var logger = serviceProvider.GetRequiredService<ILogger<TDbContext>>();
|
||||
var options = serviceProvider.GetOptions<ConnectionOptions>();
|
||||
var environment = serviceProvider.GetRequiredService<IHostEnvironment>();
|
||||
var envIsDevelopment = environment.IsDevelopment();
|
||||
|
||||
optionsBuilder
|
||||
.UseSqlServer(options.SqlConnection, sqlServerOptions =>
|
||||
.UseSqlServer(connectionOptions.SqlConnection, sqlServerOptions =>
|
||||
{
|
||||
sqlServerOptions
|
||||
.MigrationsAssembly(DbMigrationAssemblyName)
|
||||
|
||||
Reference in New Issue
Block a user