refactor(DIExtensions): Option zum Hinzufügen von db-Kontext über dbContextOptions-Eingang hinzugefügt

This commit is contained in:
Developer 02 2025-03-28 13:42:49 +01:00
parent 8824bfef00
commit 77713997bf
2 changed files with 24 additions and 9 deletions

View File

@ -1,16 +1,32 @@
using EnvelopeGenerator.Application.Contracts.Repositories;
using EnvelopeGenerator.Application.Extensions;
using EnvelopeGenerator.Infrastructure.Repositories;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
namespace EnvelopeGenerator.Infrastructure;
public static class DIExtensions
{
public static IServiceCollection AddEnvelopeGeneratorRepositories(this IServiceCollection services)
/// <summary>
/// Registers the required repositories for the Envelope Generator service to the given <see cref="IServiceCollection"/>.
/// This method adds the repositories for various envelope-related entities, such as configuration, document receivers, envelopes, and users,
/// as scoped services to the dependency injection container. Optionally, it can also configure the <see cref="EGDbContext"/>
/// with the provided database context options if specified.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to which the services are added.</param>
/// <param name="dbContextOptions">An optional action to configure the <see cref="DbContextOptionsBuilder"/> for the <see cref="EGDbContext"/>.
/// If not provided, the <see cref="EGDbContext"/> will not be configured.</param>
/// <returns>The updated <see cref="IServiceCollection"/> with the added repository services.</returns>
/// <remarks>
/// This method ensures that the repositories are registered as scoped services, meaning that a new instance of each repository
/// will be created per HTTP request (or per scope) within the dependency injection container.
/// </remarks>
public static IServiceCollection AddEnvelopeGeneratorRepositories(this IServiceCollection services, Action<DbContextOptionsBuilder>? dbContextOptions = null)
{
if(dbContextOptions is not null)
services.AddDbContext<EGDbContext>(dbContextOptions);
services.TryAddScoped<IConfigRepository, ConfigRepository>();
services.TryAddScoped<IDocumentReceiverElementRepository, DocumentReceiverElementRepository>();
services.TryAddScoped<IEnvelopeDocumentRepository, EnvelopeDocumentRepository>();
@ -30,8 +46,4 @@ public static class DIExtensions
return services;
}
public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, IConfiguration configuration) => services
.AddEnvelopeGeneratorRepositories()
.AddEnvelopeGeneratorServices(configuration);
}

View File

@ -15,6 +15,7 @@ using DigitalData.EmailProfilerDispatcher;
using EnvelopeGenerator.Infrastructure;
using EnvelopeGenerator.Web.Sanitizers;
using EnvelopeGenerator.Application.Contracts.Services;
using EnvelopeGenerator.Application.Extensions;
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Info("Logging initialized!");
@ -80,7 +81,7 @@ try
//AddEF Core dbcontext
var connStr = config.GetConnectionString(Key.Default) ?? throw new InvalidOperationException("There is no default connection string in appsettings.json.");
builder.Services.AddDbContext<EGDbContext>(options => options.UseSqlServer(connStr));
builder.Services.AddDistributedSqlServerCache(options =>
{
options.ConnectionString = connStr;
@ -89,7 +90,9 @@ try
});
// Add envelope generator services
builder.Services.AddEnvelopeGenerator(config);
builder.Services.AddEnvelopeGeneratorRepositories(options => options.UseSqlServer(connStr));
builder.Services.AddEnvelopeGeneratorServices(config);
builder.Services.Configure<CookiePolicyOptions>(options =>
{