Developer 02 a42e4287ff Refactor envelope generator service dependencies
This commit replaces the `AddEnvelopeGeneratorRepositories` method with `AddEnvelopeGeneratorInfrastructureServices`, allowing for more flexible configuration through `IConfiguration` and `Action<SQLExecutorParams>`. The `SQLExecutor` class now utilizes `SQLExecutorParams` for its connection string, enhancing configurability. A new `SQLExecutorParams` class has been introduced to encapsulate connection string management. Various service registration calls have been updated to integrate the new infrastructure services, improving modularity and ease of database connection management.
2025-05-05 10:47:00 +02:00

41 lines
1.5 KiB
C#

using Microsoft.Extensions.Hosting;
using EnvelopeGenerator.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using EnvelopeGenerator.Application.Services;
using Microsoft.EntityFrameworkCore;
using EnvelopeGenerator.Application;
namespace EnvelopeGenerator.Tests.Application;
public class Mock
{
public static IHost CreateHost(Action<HostApplicationBuilder>? builderOptions = null, string configPath = "appsettings.json", bool useRealDb = false, params string[] args)
{
var builder = Host.CreateApplicationBuilder(args.Any() ? args : null);
var config = builder.Configuration;
builder.Configuration.AddJsonFile(configPath, optional: true, reloadOnChange: true);
builder.Services
.AddEnvelopeGeneratorInfrastructureServices(opt =>
{
if (useRealDb)
{
var connStr = config.GetConnectionString("Default")
?? throw new InvalidOperationException("There is no default connection string in appsettings.json.");
opt.UseSqlServer(connStr);
}
else
opt.UseInMemoryDatabase("MockDB");
})
.AddEnvelopeGeneratorServices(builder.Configuration)
.AddScoped<DocumentStatusService>();
builderOptions?.Invoke(builder);
var host = builder.Build();
return host;
}
}