From e6e8e4d7145c428e8a9a668536466efeb4a1cd74 Mon Sep 17 00:00:00 2001 From: JusterZhu Date: Sat, 23 May 2026 19:09:52 +0800 Subject: [PATCH] feat(Drivelution): add DI integration with ServiceCollectionExtensions.AddDrivelution() - Add ServiceCollectionExtensions.AddDrivelution() for DI registration - Register ICommandRunner, IDriverValidator, IDriverBackup, IGeneralDrivelution per platform - Support DrivelutionOptions configuration via Action delegate - GeneralDrivelution.Create(IServiceProvider) overload for DI resolution - Falls back to factory-created instance if not registered in DI - Add Microsoft.Extensions.DependencyInjection.Abstractions package reference Closes #290 --- .../Core/ServiceCollectionExtensions.cs | 75 +++++++++++++++++++ .../GeneralDrivelution.cs | 17 +++++ .../GeneralUpdate.Drivelution.csproj | 3 + 3 files changed, 95 insertions(+) create mode 100644 src/c#/GeneralUpdate.Drivelution/Core/ServiceCollectionExtensions.cs diff --git a/src/c#/GeneralUpdate.Drivelution/Core/ServiceCollectionExtensions.cs b/src/c#/GeneralUpdate.Drivelution/Core/ServiceCollectionExtensions.cs new file mode 100644 index 00000000..82e8b3d9 --- /dev/null +++ b/src/c#/GeneralUpdate.Drivelution/Core/ServiceCollectionExtensions.cs @@ -0,0 +1,75 @@ +using Microsoft.Extensions.DependencyInjection; +using GeneralUpdate.Drivelution.Abstractions; +using GeneralUpdate.Drivelution.Abstractions.Configuration; +using GeneralUpdate.Drivelution.Core.Execution; +using GeneralUpdate.Drivelution.Windows.Implementation; +using GeneralUpdate.Drivelution.Linux.Implementation; +using GeneralUpdate.Drivelution.MacOS.Implementation; + +namespace GeneralUpdate.Drivelution.Core; + +/// +/// Extension methods for registering Drivelution services in the DI container. +/// +public static class ServiceCollectionExtensions +{ + /// + /// Registers the Drivelution driver update services for the current platform. + /// + /// The service collection. + /// Optional configuration action for . + /// The service collection for chaining. + /// + /// + /// // ASP.NET / Generic Host + /// builder.Services.AddDrivelution(); + /// + /// // With options + /// builder.Services.AddDrivelution(options => + /// { + /// options.DefaultRetryCount = 5; + /// options.DefaultTimeoutSeconds = 600; + /// }); + /// + /// // Consumer + /// public class MyService + /// { + /// public MyService(IGeneralDrivelution updater) { ... } + /// } + /// + /// + public static IServiceCollection AddDrivelution( + this IServiceCollection services, + Action? configure = null) + { + // Configuration + var options = new DrivelutionOptions(); + configure?.Invoke(options); + services.AddSingleton(options); + + // Infrastructure + services.AddSingleton(); + + // Platform-specific registrations + if (OperatingSystem.IsWindows()) + { + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + } + else if (OperatingSystem.IsLinux()) + { + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + } + else if (OperatingSystem.IsMacOS()) + { + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + } + + return services; + } +} diff --git a/src/c#/GeneralUpdate.Drivelution/GeneralDrivelution.cs b/src/c#/GeneralUpdate.Drivelution/GeneralDrivelution.cs index e7ae3a5c..b7238055 100644 --- a/src/c#/GeneralUpdate.Drivelution/GeneralDrivelution.cs +++ b/src/c#/GeneralUpdate.Drivelution/GeneralDrivelution.cs @@ -3,6 +3,7 @@ using GeneralUpdate.Drivelution.Abstractions.Configuration; using GeneralUpdate.Drivelution.Abstractions.Models; using GeneralUpdate.Drivelution.Core.Utilities; +using Microsoft.Extensions.DependencyInjection; namespace GeneralUpdate.Drivelution; @@ -35,6 +36,22 @@ public static IGeneralDrivelution Create(DrivelutionOptions? options = null) return Core.DrivelutionFactory.Create(options); } + /// + /// Resolves a driver updater from the DI service provider. + /// Falls back to if not registered. + /// + /// DI service provider. + /// Platform-adapted driver updater. + public static IGeneralDrivelution Create(IServiceProvider serviceProvider) + { + var updater = serviceProvider.GetService(); + if (updater is not null) + return updater; + + var options = serviceProvider.GetService(); + return Core.DrivelutionFactory.Create(options); + } + /// /// Quick driver update (with default configuration) /// diff --git a/src/c#/GeneralUpdate.Drivelution/GeneralUpdate.Drivelution.csproj b/src/c#/GeneralUpdate.Drivelution/GeneralUpdate.Drivelution.csproj index dec7ca72..7b3594ca 100644 --- a/src/c#/GeneralUpdate.Drivelution/GeneralUpdate.Drivelution.csproj +++ b/src/c#/GeneralUpdate.Drivelution/GeneralUpdate.Drivelution.csproj @@ -48,5 +48,8 @@ ./ + + +