refactor(DIExtensions): Option zum Hinzufügen von db-Kontext über dbContextOptions-Eingang hinzugefügt
This commit is contained in:
parent
8824bfef00
commit
77713997bf
@ -1,16 +1,32 @@
|
|||||||
using EnvelopeGenerator.Application.Contracts.Repositories;
|
using EnvelopeGenerator.Application.Contracts.Repositories;
|
||||||
using EnvelopeGenerator.Application.Extensions;
|
|
||||||
using EnvelopeGenerator.Infrastructure.Repositories;
|
using EnvelopeGenerator.Infrastructure.Repositories;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Infrastructure;
|
namespace EnvelopeGenerator.Infrastructure;
|
||||||
|
|
||||||
public static class DIExtensions
|
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<IConfigRepository, ConfigRepository>();
|
||||||
services.TryAddScoped<IDocumentReceiverElementRepository, DocumentReceiverElementRepository>();
|
services.TryAddScoped<IDocumentReceiverElementRepository, DocumentReceiverElementRepository>();
|
||||||
services.TryAddScoped<IEnvelopeDocumentRepository, EnvelopeDocumentRepository>();
|
services.TryAddScoped<IEnvelopeDocumentRepository, EnvelopeDocumentRepository>();
|
||||||
@ -30,8 +46,4 @@ public static class DIExtensions
|
|||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, IConfiguration configuration) => services
|
|
||||||
.AddEnvelopeGeneratorRepositories()
|
|
||||||
.AddEnvelopeGeneratorServices(configuration);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,7 @@ using DigitalData.EmailProfilerDispatcher;
|
|||||||
using EnvelopeGenerator.Infrastructure;
|
using EnvelopeGenerator.Infrastructure;
|
||||||
using EnvelopeGenerator.Web.Sanitizers;
|
using EnvelopeGenerator.Web.Sanitizers;
|
||||||
using EnvelopeGenerator.Application.Contracts.Services;
|
using EnvelopeGenerator.Application.Contracts.Services;
|
||||||
|
using EnvelopeGenerator.Application.Extensions;
|
||||||
|
|
||||||
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
|
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
|
||||||
logger.Info("Logging initialized!");
|
logger.Info("Logging initialized!");
|
||||||
@ -80,7 +81,7 @@ try
|
|||||||
|
|
||||||
//AddEF Core dbcontext
|
//AddEF Core dbcontext
|
||||||
var connStr = config.GetConnectionString(Key.Default) ?? throw new InvalidOperationException("There is no default connection string in appsettings.json.");
|
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 =>
|
builder.Services.AddDistributedSqlServerCache(options =>
|
||||||
{
|
{
|
||||||
options.ConnectionString = connStr;
|
options.ConnectionString = connStr;
|
||||||
@ -89,7 +90,9 @@ try
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Add envelope generator services
|
// Add envelope generator services
|
||||||
builder.Services.AddEnvelopeGenerator(config);
|
builder.Services.AddEnvelopeGeneratorRepositories(options => options.UseSqlServer(connStr));
|
||||||
|
|
||||||
|
builder.Services.AddEnvelopeGeneratorServices(config);
|
||||||
|
|
||||||
builder.Services.Configure<CookiePolicyOptions>(options =>
|
builder.Services.Configure<CookiePolicyOptions>(options =>
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user