mirror of
https://github.com/gwdevhub/Daybreak.git
synced 2026-07-25 08:22:07 +00:00
Native linux support (#1378) (Closes #1375 Closes #1376 Closes #1377 Closes #1374 Closes #1373 Closes #1372 Closes #1371)
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using Daybreak.Models;
|
||||
using Daybreak.Services.Options;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Extensions;
|
||||
|
||||
namespace Daybreak.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddDaybreakOptions<TOptions>(this IServiceCollection services)
|
||||
where TOptions : class, new()
|
||||
{
|
||||
services.AddSingleton(new OptionEntry(typeof(TOptions)));
|
||||
services.AddSingleton<IOptionsFactory<TOptions>>(sp =>
|
||||
new DaybreakOptionsFactory<TOptions>(sp.GetRequiredService<OptionsManager>()));
|
||||
|
||||
services.AddSingleton<IOptionsChangeTokenSource<TOptions>>(sp =>
|
||||
new DaybreakOptionsChangeTokenSource<TOptions>(sp.GetRequiredService<OptionsManager>()));
|
||||
|
||||
services.AddSingleton<IOptionsMonitorCache<TOptions>, OptionsCache<TOptions>>();
|
||||
|
||||
services.AddSingleton<IOptionsMonitor<TOptions>>(sp =>
|
||||
{
|
||||
var factory = sp.GetRequiredService<IOptionsFactory<TOptions>>();
|
||||
var sources = sp.GetServices<IOptionsChangeTokenSource<TOptions>>();
|
||||
var cache = sp.GetRequiredService<IOptionsMonitorCache<TOptions>>();
|
||||
var monitor = new OptionsMonitor<TOptions>(factory, sources, cache);
|
||||
monitor.OnChange((_, name) => cache.TryRemove(name ?? Options.DefaultName));
|
||||
return new OptionsMonitor<TOptions>(factory, sources, cache);
|
||||
});
|
||||
|
||||
services.AddSingleton<IOptions<TOptions>>(sp =>
|
||||
new OptionsWrapper<TOptions>(sp.GetRequiredService<IOptionsMonitor<TOptions>>().CurrentValue));
|
||||
|
||||
services.AddScoped<IOptionsSnapshot<TOptions>>(sp =>
|
||||
new OptionsManager<TOptions>(
|
||||
sp.GetRequiredService<IOptionsFactory<TOptions>>()));
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddHostedSingleton<TInterface, TImplementation>(this IServiceCollection services)
|
||||
where TImplementation : class, TInterface, IHostedService
|
||||
where TInterface : class
|
||||
{
|
||||
services.AddSingleton<TInterface, TImplementation>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<TInterface>().Cast<TImplementation>());
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddHostedSingleton<TImplementation>(this IServiceCollection services)
|
||||
where TImplementation : class, IHostedService
|
||||
{
|
||||
services.AddSingleton<TImplementation>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<TImplementation>());
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user