AgentSkillsCN

Dependency Injection Setup

使用 Microsoft.Extensions.DependencyInjection 配置并注册所有 ArdysaModsTools 服务。

SKILL.md
--- frontmatter
name: Dependency Injection Setup
description: Configure and register all ArdysaModsTools services using Microsoft.Extensions.DependencyInjection

Dependency Injection Setup

ArdysaModsTools uses Microsoft.Extensions.DependencyInjection for all service resolution.

Register All Services

csharp
using ArdysaModsTools.Core.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
services.AddArdysaServices();

var provider = services.BuildServiceProvider();

Service Categories

AddArdysaServices() registers everything. Individual categories can also be registered separately:

csharp
services
    .AddCoreServices()       // ModInstaller, Status, ActiveMods, Detection, Config, FileTransaction
    .AddConflictServices()   // ConflictDetector, ConflictResolver, ModPriority
    .AddHeroServices()       // HeroGeneration
    .AddLoggingServices()    // Logger (NullLogger default)
    .AddPresenters()         // ModOperations, Patch, Navigation presenters
    .AddUIFactories();       // MainForm factory

Resolve Services

csharp
// Core services
var installer = provider.GetRequiredService<IModInstallerService>();
var status = provider.GetRequiredService<IStatusService>();
var activeMods = provider.GetRequiredService<IActiveModsService>();
var detection = provider.GetRequiredService<IDetectionService>();
var config = provider.GetRequiredService<IConfigService>();

// Hero generation
var heroGen = provider.GetRequiredService<IHeroGenerationService>();

// Conflict handling
var detector = provider.GetRequiredService<IConflictDetector>();
var resolver = provider.GetRequiredService<IConflictResolver>();

Service Lifetimes

ServiceLifetimeReason
IModInstallerServiceTransientEach operation gets fresh state
IStatusServiceTransientDisposable (timer + file watcher)
IActiveModsServiceTransientStateless query service
IDetectionServiceTransientFresh detection each call
IConfigServiceSingletonShared config state
IConflictDetectorSingletonStateless, reusable
IAppLoggerSingletonSingle logger instance

Replace Logger

The default is NullLogger. Replace with a real logger:

csharp
var services = new ServiceCollection();
services.AddArdysaServices();

// Override with real logger
services.AddSingleton<IAppLogger>(new Logger(richTextBoxConsole));

var provider = services.BuildServiceProvider();

Key File

  • ServiceCollectionExtensions: Core/DependencyInjection/ServiceCollectionExtensions.cs