move DbTriggerParams to Infrastructure layer.

- createConfig for flexable configuration
This commit is contained in:
tekh 2025-10-01 12:59:55 +02:00
parent ca24afe3c6
commit 53a656f6ee
8 changed files with 84 additions and 22 deletions

View File

@ -1,8 +0,0 @@
namespace EnvelopeGenerator.Application.Common.Configurations;
/// <summary>
///
/// </summary>
public class DbTriggerParams : Dictionary<string, ICollection<string>>
{
}

View File

@ -46,7 +46,6 @@ public static class DependencyInjection
services.Configure<MailParams>(config.GetSection(nameof(MailParams))); services.Configure<MailParams>(config.GetSection(nameof(MailParams)));
services.Configure<AuthenticatorParams>(config.GetSection(nameof(AuthenticatorParams))); services.Configure<AuthenticatorParams>(config.GetSection(nameof(AuthenticatorParams)));
services.Configure<TotpSmsParams>(config.GetSection(nameof(TotpSmsParams))); services.Configure<TotpSmsParams>(config.GetSection(nameof(TotpSmsParams)));
services.Configure<DbTriggerParams>(config.GetSection(nameof(DbTriggerParams)));
services.AddHttpClientService<GtxMessagingParams>(config.GetSection(nameof(GtxMessagingParams))); services.AddHttpClientService<GtxMessagingParams>(config.GetSection(nameof(GtxMessagingParams)));
services.TryAddSingleton<ISmsSender, GTXSmsSender>(); services.TryAddSingleton<ISmsSender, GTXSmsSender>();

View File

@ -0,0 +1,18 @@
#if NETFRAMEWORK
using System.Collections.Generic;
#endif
namespace EnvelopeGenerator.Infrastructure
#if NET
;
#elif NETFRAMEWORK
{
#endif
public class DbTriggerParams : Dictionary<string, ICollection<string>>
{
}
#if NETFRAMEWORK
}
#endif

View File

@ -9,6 +9,7 @@ using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection; using System.Reflection;
using DigitalData.UserManager.Domain.Entities; using DigitalData.UserManager.Domain.Entities;
#if NET #if NET
using CommandDotNet.Execution;
using EnvelopeGenerator.Infrastructure.Repositories; using EnvelopeGenerator.Infrastructure.Repositories;
using EnvelopeGenerator.Infrastructure.Executor; using EnvelopeGenerator.Infrastructure.Executor;
using EnvelopeGenerator.Application.Common.Interfaces.Repositories; using EnvelopeGenerator.Application.Common.Interfaces.Repositories;
@ -38,6 +39,7 @@ namespace EnvelopeGenerator.Infrastructure
/// </remarks> /// </remarks>
[Obsolete("Use IRepository")] [Obsolete("Use IRepository")]
public static IServiceCollection AddEnvelopeGeneratorInfrastructureServices(this IServiceCollection services, public static IServiceCollection AddEnvelopeGeneratorInfrastructureServices(this IServiceCollection services,
Action<Config> options,
Action<IServiceProvider, DbContextOptionsBuilder> Action<IServiceProvider, DbContextOptionsBuilder>
#if NET #if NET
? ?
@ -49,6 +51,9 @@ namespace EnvelopeGenerator.Infrastructure
#endif #endif
) )
{ {
// configure custom options
options(new Config(services));
if (dbContextOptions != null) if (dbContextOptions != null)
services.AddDbContext<EGDbContext>(dbContextOptions); services.AddDbContext<EGDbContext>(dbContextOptions);
@ -173,5 +178,25 @@ namespace EnvelopeGenerator.Infrastructure
return services; return services;
} }
#endif #endif
public class Config
{
private readonly IServiceCollection _services;
internal Config(IServiceCollection services)
{
_services = services;
}
public void ConfigureDbTriggerParams(IConfiguration configuration)
{
_services.Configure<DbTriggerParams>(configuration.GetSection(nameof(DbTriggerParams)));
}
public void ConfigureDbTriggerParams(Action<DbTriggerParams> configureOptions)
{
_services.Configure(configureOptions);
}
}
} }
} }

View File

@ -13,6 +13,7 @@ using DigitalData.UserManager.Infrastructure.Contracts;
using DigitalData.Core.Client; using DigitalData.Core.Client;
using EnvelopeGenerator.Application.Common.Configurations; using EnvelopeGenerator.Application.Common.Configurations;
#elif NETFRAMEWORK #elif NETFRAMEWORK
using System.Linq;
#endif #endif
namespace EnvelopeGenerator.Infrastructure namespace EnvelopeGenerator.Infrastructure
@ -35,7 +36,7 @@ public abstract class EGDbContextBase : DbContext
, IUserManagerDbContext, IMailDbContext , IUserManagerDbContext, IMailDbContext
#endif #endif
{ {
public DbSet<Config> Configs { get; set; } public DbSet<Domain.Entities.Config> Configs { get; set; }
public DbSet<EnvelopeReceiver> EnvelopeReceivers { get; set; } public DbSet<EnvelopeReceiver> EnvelopeReceivers { get; set; }
@ -85,7 +86,11 @@ public abstract class EGDbContextBase : DbContext
public bool IsMigration { get; set; } = false; public bool IsMigration { get; set; } = false;
public EGDbContextBase(DbContextOptions options, IOptions<DbTriggerParams> triggerParamOptions, ILogger? logger = null) : base(options) public EGDbContextBase(DbContextOptions options, IOptions<DbTriggerParams> triggerParamOptions, ILogger
#if NET
?
#endif
logger = null) : base(options)
{ {
_triggers = triggerParamOptions.Value; _triggers = triggerParamOptions.Value;
_logger = logger; _logger = logger;
@ -213,7 +218,9 @@ public abstract class EGDbContextBase : DbContext
.SelectMany(t => t.Value) .SelectMany(t => t.Value)
.ForEach(tName => .ForEach(tName =>
{ {
#if NET
modelBuilder.Entity<T>().ToTable(tb => tb.HasTrigger(tName)); modelBuilder.Entity<T>().ToTable(tb => tb.HasTrigger(tName));
#endif
_logger?.LogInformation("Trigger '{triggerName}' has been added to the '{entityName}' entity.", tName, typeof(T).Name); _logger?.LogInformation("Trigger '{triggerName}' has been added to the '{entityName}' entity.", tName, typeof(T).Name);
}); });

View File

@ -0,0 +1,20 @@
#if NETFRAMEWORK
using System.Collections.Generic;
using System;
#endif
namespace EnvelopeGenerator.Infrastructure
{
public static class EnumerableExtensions
{
#if NETFRAMEWORK
public static void ForEach<T>(this IEnumerable<T> values, Action<T> action)
{
foreach (T value in values)
{
action(value);
}
}
#endif
}
}

View File

@ -1,19 +1,18 @@
#if NET #if NET
using DigitalData.Core.Infrastructure; using DigitalData.Core.Infrastructure;
using EnvelopeGenerator.Domain.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using EnvelopeGenerator.Application.Common.Interfaces.Repositories; using EnvelopeGenerator.Application.Common.Interfaces.Repositories;
namespace EnvelopeGenerator.Infrastructure.Repositories; namespace EnvelopeGenerator.Infrastructure.Repositories;
[Obsolete("Use IRepository")] [Obsolete("Use IRepository")]
public class ConfigRepository : CRUDRepository<Config, int, EGDbContext>, IConfigRepository public class ConfigRepository : CRUDRepository<Domain.Entities.Config, int, EGDbContext>, IConfigRepository
{ {
public ConfigRepository(EGDbContext dbContext) : base(dbContext, dbContext.Configs) public ConfigRepository(EGDbContext dbContext) : base(dbContext, dbContext.Configs)
{ {
} }
public async Task<Config?> ReadFirstAsync() public async Task<Domain.Entities.Config?> ReadFirstAsync()
{ {
var configs = await _dbSet.ToListAsync(); var configs = await _dbSet.ToListAsync();
return configs.Count > 0 ? configs[0] : default; return configs.Count > 0 ? configs[0] : default;

View File

@ -103,14 +103,16 @@ try
// Add envelope generator services // Add envelope generator services
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete
builder.Services.AddEnvelopeGeneratorInfrastructureServices((provider, options) => builder.Services.AddEnvelopeGeneratorInfrastructureServices(
{ opt => opt.ConfigureDbTriggerParams(config),
var logger = provider.GetRequiredService<ILogger<EGDbContext>>(); (provider, options) =>
options.UseSqlServer(connStr) {
.LogTo(log => logger.LogInformation("{log}", log), Microsoft.Extensions.Logging.LogLevel.Trace) var logger = provider.GetRequiredService<ILogger<EGDbContext>>();
.EnableSensitiveDataLogging() options.UseSqlServer(connStr)
.EnableDetailedErrors(); .LogTo(log => logger.LogInformation("{log}", log), Microsoft.Extensions.Logging.LogLevel.Trace)
}); .EnableSensitiveDataLogging()
.EnableDetailedErrors();
});
#pragma warning restore CS0618 // Type or member is obsolete #pragma warning restore CS0618 // Type or member is obsolete
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete