using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Jailbreak.Public.Extensions;
public static class ServiceCollectionExtensions
{
///
/// Add a to the global service collection
///
///
///
public static void AddPluginBehavior(this IServiceCollection collection)
where TExtension: class, IPluginBehavior
{
// Add the root extension itself as a scoped service.
// This means every time Load is called in the main Jailbreak loader,
// the extension will be fetched and kept as a singleton for the duration
// until "Unload" is called.
collection.AddScoped();
collection.AddTransient(provider => provider.GetRequiredService());
}
///
/// Add a to the global service collection
///
///
///
///
public static void AddPluginBehavior(this IServiceCollection collection)
where TExtension: class, IPluginBehavior, TInterface
where TInterface : class
{
// Add the root extension itself as a scoped service.
// This means every time Load is called in the main Jailbreak loader,
// the extension will be fetched and kept as a singleton for the duration
// until "Unload" is called.
collection.AddScoped();
collection.AddTransient(provider => provider.GetRequiredService());
collection.AddTransient(provider => provider.GetRequiredService());
}
///
/// Add an object to be loaded from the configuration file
///
///
/// The section where the configuration object will be loaded from
/// The configuration object. Must auto-fill all default values!
public static void AddConfig(this IServiceCollection collection, string sectionName)
where TConfig : class, new()
{
// Get the object by resolving IConfigService
// and use the Get() method.
// Not *really* important... but do we want to fail here or return default if section
// isn't available?
collection.AddTransient(provider => provider.GetRequiredService()
.Get(sectionName, /*failOnDefault*/ false));
}
}