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.
This commit is contained in:
Developer 02
2025-04-14 12:55:48 +02:00
parent c0608b457c
commit a070a0f64c
3 changed files with 21 additions and 2 deletions

View File

@@ -1,9 +1,10 @@
using CommandDotNet; using CommandDotNet;
using EnvelopeGenerator.Application.Contracts.Services;
using System.Reflection; using System.Reflection;
namespace EnvelopeGenerator.Terminal; namespace EnvelopeGenerator.Terminal;
public class CommandManager public class CommandManager(IEnvelopeReceiverService envelopeReceiverService)
{ {
[DefaultCommand] [DefaultCommand]
public void Execute([Option(Description = "print envelope generator termianal version.")] bool version) public void Execute([Option(Description = "print envelope generator termianal version.")] bool version)
@@ -11,4 +12,7 @@ public class CommandManager
if(version) if(version)
Console.WriteLine($"v{Assembly.GetExecutingAssembly().GetName().Version}"); Console.WriteLine($"v{Assembly.GetExecutingAssembly().GetName().Version}");
} }
[Subcommand]
public IEnvelopeReceiverService EnvelopeReceiverService => envelopeReceiverService;
} }

View File

@@ -5,13 +5,27 @@ using CommandDotNet.IoC.MicrosoftDependencyInjection;
using EnvelopeGenerator.Infrastructure; using EnvelopeGenerator.Infrastructure;
using EnvelopeGenerator.Application.Extensions; using EnvelopeGenerator.Application.Extensions;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
namespace EnvelopeGenerator.Terminal; namespace EnvelopeGenerator.Terminal;
public static class DependencyInjection public static class DependencyInjection
{ {
public static IServiceCollection AddCommandManagerRunner(this IServiceCollection services, IConfiguration configuration, Case @case = Case.KebabCase) 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 return services
.AddSingleton<CommandManager>() .AddSingleton<CommandManager>()
.AddEnvelopeGeneratorRepositories() .AddEnvelopeGeneratorRepositories()

View File

@@ -21,6 +21,7 @@
<PackageReference Include="CommandDotNet" Version="8.1.1" /> <PackageReference Include="CommandDotNet" Version="8.1.1" />
<PackageReference Include="CommandDotNet.IoC.MicrosoftDependencyInjection" Version="7.1.0" /> <PackageReference Include="CommandDotNet.IoC.MicrosoftDependencyInjection" Version="7.1.0" />
<PackageReference Include="CommandDotNet.NameCasing" Version="5.1.0" /> <PackageReference Include="CommandDotNet.NameCasing" Version="5.1.0" />
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="9.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.3" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.3" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.3" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.3" />