Scrutor icon indicating copy to clipboard operation
Scrutor copied to clipboard

[API Proposal]: Ioc can load configuration from config file

Open soroshsabz opened this issue 3 years ago • 2 comments

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

  1. We can change configuration without needing recompile application.
  2. In this scenario we can create multiple config set in multiple files and load them in specific situation without needing to recompile application.
  3. 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

soroshsabz avatar Jul 10 '22 06:07 soroshsabz

related to https://github.com/dotnet/runtime/issues/71832 related to https://github.com/CommunityToolkit/dotnet/issues/328

soroshsabz avatar Jul 10 '22 06:07 soroshsabz

@khellang Any note about this issue?

thanks

soroshsabz avatar Feb 24 '23 16:02 soroshsabz