Scrutor
Scrutor copied to clipboard
[API Proposal]: Ioc can load configuration from config file
Background and motivation
ITNOA
I think it is very useful to add ability to load configuration from file in Ioc, Some of values of this features
- We can change configuration without needing recompile application.
- In this scenario we can create multiple config set in multiple files and load them in specific situation without needing to recompile application.
- We can change app behavior in runtime (or in deployment) without needing to reinstall app.
API Proposal
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for adding services to an <see cref="IServiceCollection" />.
/// </summary>
public static class ServiceCollectionServiceExtensions
{
public static IServiceCollection AddFromConfig(this IServiceCollection services, IConfigurationSection config) {}
}
API Usage
Ioc.Default.ConfigureServices(
new ServiceCollection()
.AddFromConfig(config.GetSection("Ioc"))
.BuildServiceProvider());
Alternative Designs
// Register services
Ioc.Default.ConfigureServices(
new ServiceCollection()
.AddSingleton<IDialogService, DialogService>() //Services
.AddSingleton<IFilesService, FilesService>()
.AddSingleton<ISettingsService, SettingsService>()
.AddSingleton(RestService.For<IRedditService>("https://www.reddit.com/"))
.AddSingleton(RestService.For<IContactsService>("https://randomuser.me/"))
.AddTransient<PostWidgetViewModel>() //ViewModels
.AddTransient<SubredditWidgetViewModel>()
.AddTransient<ContactsListWidgetViewModel>()
.AddTransient<AsyncRelayCommandPageViewModel>()
.AddTransient<IocPageViewModel>()
.AddTransient<MessengerPageViewModel>()
.AddTransient<ObservableObjectPageViewModel>()
.AddTransient<ObservableValidatorPageViewModel>()
.AddTransient<ValidationFormWidgetViewModel>()
.AddTransient<RelayCommandPageViewModel>()
.AddTransient<CollectionsPageViewModel>()
.AddTransient<SamplePageViewModel>()
.BuildServiceProvider());
}
Risks
No response
related to https://github.com/dotnet/runtime/issues/71832 related to https://github.com/CommunityToolkit/dotnet/issues/328
@khellang Any note about this issue?
thanks