Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Extension methods for registering Drivelution services in the DI container.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Registers the Drivelution driver update services for the current platform.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="configure">Optional configuration action for <see cref="DrivelutionOptions"/>.</param>
/// <returns>The service collection for chaining.</returns>
/// <example>
/// <code>
/// // 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) { ... }
/// }
/// </code>
/// </example>
public static IServiceCollection AddDrivelution(
this IServiceCollection services,
Action<DrivelutionOptions>? configure = null)
{
// Configuration
var options = new DrivelutionOptions();
configure?.Invoke(options);
services.AddSingleton(options);

// Infrastructure
services.AddSingleton<ICommandRunner, CommandRunner>();

// Platform-specific registrations
if (OperatingSystem.IsWindows())
{
services.AddSingleton<IDriverValidator, WindowsDriverValidator>();
services.AddSingleton<IDriverBackup, WindowsDriverBackup>();
services.AddSingleton<IGeneralDrivelution, WindowsGeneralDrivelution>();
}
else if (OperatingSystem.IsLinux())
{
services.AddSingleton<IDriverValidator, LinuxDriverValidator>();
services.AddSingleton<IDriverBackup, LinuxDriverBackup>();
services.AddSingleton<IGeneralDrivelution, LinuxGeneralDrivelution>();
}
else if (OperatingSystem.IsMacOS())
{
services.AddSingleton<IDriverValidator, MacOSDriverValidator>();
services.AddSingleton<IDriverBackup, MacOSDriverBackup>();
services.AddSingleton<IGeneralDrivelution, MacOsGeneralDrivelution>();
}

return services;
}
}
17 changes: 17 additions & 0 deletions src/c#/GeneralUpdate.Drivelution/GeneralDrivelution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -35,6 +36,22 @@ public static IGeneralDrivelution Create(DrivelutionOptions? options = null)
return Core.DrivelutionFactory.Create(options);
}

/// <summary>
/// Resolves a driver updater from the DI service provider.
/// Falls back to <see cref="Create(DrivelutionOptions?)"/> if not registered.
/// </summary>
/// <param name="serviceProvider">DI service provider.</param>
/// <returns>Platform-adapted driver updater.</returns>
public static IGeneralDrivelution Create(IServiceProvider serviceProvider)
{
var updater = serviceProvider.GetService<IGeneralDrivelution>();
if (updater is not null)
return updater;

var options = serviceProvider.GetService<DrivelutionOptions>();
return Core.DrivelutionFactory.Create(options);
}

/// <summary>
/// Quick driver update (with default configuration)
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@
<PackagePath>./</PackagePath>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.8" />
</ItemGroup>

</Project>
Loading