move DbTriggerParams to Infrastructure layer.
- createConfig for flexable configuration
This commit is contained in:
parent
ca24afe3c6
commit
53a656f6ee
@ -1,8 +0,0 @@
|
||||
namespace EnvelopeGenerator.Application.Common.Configurations;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class DbTriggerParams : Dictionary<string, ICollection<string>>
|
||||
{
|
||||
}
|
||||
@ -46,7 +46,6 @@ public static class DependencyInjection
|
||||
services.Configure<MailParams>(config.GetSection(nameof(MailParams)));
|
||||
services.Configure<AuthenticatorParams>(config.GetSection(nameof(AuthenticatorParams)));
|
||||
services.Configure<TotpSmsParams>(config.GetSection(nameof(TotpSmsParams)));
|
||||
services.Configure<DbTriggerParams>(config.GetSection(nameof(DbTriggerParams)));
|
||||
|
||||
services.AddHttpClientService<GtxMessagingParams>(config.GetSection(nameof(GtxMessagingParams)));
|
||||
services.TryAddSingleton<ISmsSender, GTXSmsSender>();
|
||||
|
||||
18
EnvelopeGenerator.Infrastructure/DbTriggerParams.cs
Normal file
18
EnvelopeGenerator.Infrastructure/DbTriggerParams.cs
Normal 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
|
||||
@ -9,6 +9,7 @@ using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Reflection;
|
||||
using DigitalData.UserManager.Domain.Entities;
|
||||
#if NET
|
||||
using CommandDotNet.Execution;
|
||||
using EnvelopeGenerator.Infrastructure.Repositories;
|
||||
using EnvelopeGenerator.Infrastructure.Executor;
|
||||
using EnvelopeGenerator.Application.Common.Interfaces.Repositories;
|
||||
@ -38,6 +39,7 @@ namespace EnvelopeGenerator.Infrastructure
|
||||
/// </remarks>
|
||||
[Obsolete("Use IRepository")]
|
||||
public static IServiceCollection AddEnvelopeGeneratorInfrastructureServices(this IServiceCollection services,
|
||||
Action<Config> options,
|
||||
Action<IServiceProvider, DbContextOptionsBuilder>
|
||||
#if NET
|
||||
?
|
||||
@ -49,6 +51,9 @@ namespace EnvelopeGenerator.Infrastructure
|
||||
#endif
|
||||
)
|
||||
{
|
||||
// configure custom options
|
||||
options(new Config(services));
|
||||
|
||||
if (dbContextOptions != null)
|
||||
services.AddDbContext<EGDbContext>(dbContextOptions);
|
||||
|
||||
@ -173,5 +178,25 @@ namespace EnvelopeGenerator.Infrastructure
|
||||
return services;
|
||||
}
|
||||
#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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -13,6 +13,7 @@ using DigitalData.UserManager.Infrastructure.Contracts;
|
||||
using DigitalData.Core.Client;
|
||||
using EnvelopeGenerator.Application.Common.Configurations;
|
||||
#elif NETFRAMEWORK
|
||||
using System.Linq;
|
||||
#endif
|
||||
|
||||
namespace EnvelopeGenerator.Infrastructure
|
||||
@ -35,7 +36,7 @@ public abstract class EGDbContextBase : DbContext
|
||||
, IUserManagerDbContext, IMailDbContext
|
||||
#endif
|
||||
{
|
||||
public DbSet<Config> Configs { get; set; }
|
||||
public DbSet<Domain.Entities.Config> Configs { get; set; }
|
||||
|
||||
public DbSet<EnvelopeReceiver> EnvelopeReceivers { get; set; }
|
||||
|
||||
@ -85,7 +86,11 @@ public abstract class EGDbContextBase : DbContext
|
||||
|
||||
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;
|
||||
_logger = logger;
|
||||
@ -213,7 +218,9 @@ public abstract class EGDbContextBase : DbContext
|
||||
.SelectMany(t => t.Value)
|
||||
.ForEach(tName =>
|
||||
{
|
||||
#if NET
|
||||
modelBuilder.Entity<T>().ToTable(tb => tb.HasTrigger(tName));
|
||||
#endif
|
||||
_logger?.LogInformation("Trigger '{triggerName}' has been added to the '{entityName}' entity.", tName, typeof(T).Name);
|
||||
});
|
||||
|
||||
|
||||
20
EnvelopeGenerator.Infrastructure/EnumerableExtensions.cs
Normal file
20
EnvelopeGenerator.Infrastructure/EnumerableExtensions.cs
Normal 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
|
||||
}
|
||||
}
|
||||
@ -1,19 +1,18 @@
|
||||
#if NET
|
||||
using DigitalData.Core.Infrastructure;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using EnvelopeGenerator.Application.Common.Interfaces.Repositories;
|
||||
|
||||
namespace EnvelopeGenerator.Infrastructure.Repositories;
|
||||
|
||||
[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 async Task<Config?> ReadFirstAsync()
|
||||
public async Task<Domain.Entities.Config?> ReadFirstAsync()
|
||||
{
|
||||
var configs = await _dbSet.ToListAsync();
|
||||
return configs.Count > 0 ? configs[0] : default;
|
||||
|
||||
@ -103,7 +103,9 @@ try
|
||||
|
||||
// Add envelope generator services
|
||||
#pragma warning disable CS0618 // Type or member is obsolete
|
||||
builder.Services.AddEnvelopeGeneratorInfrastructureServices((provider, options) =>
|
||||
builder.Services.AddEnvelopeGeneratorInfrastructureServices(
|
||||
opt => opt.ConfigureDbTriggerParams(config),
|
||||
(provider, options) =>
|
||||
{
|
||||
var logger = provider.GetRequiredService<ILogger<EGDbContext>>();
|
||||
options.UseSqlServer(connStr)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user