Developer 02 a070a0f64c Enhance CommandManager and Dependency Injection
- Updated `CommandManager` to include an `IEnvelopeReceiverService` constructor and a new property for service access. Added version printing functionality in the `Execute` method.
- Modified `AddCommandManagerRunner` in `DependencyInjection` to accept a connection string key name and added SQL Server caching configuration.
- Added `Microsoft.Extensions.Caching.SqlServer` package reference in the project file for caching support.
2025-04-14 12:55:48 +02:00

48 lines
1.8 KiB
C#

using CommandDotNet.NameCasing;
using CommandDotNet;
using Microsoft.Extensions.DependencyInjection;
using CommandDotNet.IoC.MicrosoftDependencyInjection;
using EnvelopeGenerator.Infrastructure;
using EnvelopeGenerator.Application.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
namespace EnvelopeGenerator.Terminal;
public static class DependencyInjection
{
public static IServiceCollection AddCommandManagerRunner(this IServiceCollection services, IConfiguration configuration, Case @case = Case.KebabCase, string connectionStringKeyName = "Default")
{
var connStr = configuration.GetConnectionString(connectionStringKeyName)
?? throw new InvalidOperationException("There is no default connection string in appsettings.json.");
services.AddDistributedSqlServerCache(options =>
{
options.ConnectionString = connStr;
options.SchemaName = "dbo";
options.TableName = "TBDD_CACHE";
});
// Add envelope generator services
services.AddEnvelopeGeneratorRepositories(options => options.UseSqlServer(connStr));
return services
.AddSingleton<CommandManager>()
.AddEnvelopeGeneratorRepositories()
.AddEnvelopeGeneratorServices(configuration)
.AddSingleton(sp =>
{
var runner = new AppRunner<CommandManager>();
runner.UseMicrosoftDependencyInjection(sp);
runner.UseNameCasing(@case);
return runner;
});
}
public static Task<int> RunCommandManagerRunner(this IServiceProvider provider, string[] args)
{
var runner = provider.GetRequiredService<AppRunner<CommandManager>>();
return runner.RunAsync(args);
}
}