Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
soroshsabz opened this issue Jul 10, 2022 · 2 comments
Open

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

soroshsabz opened this issue Jul 10, 2022 · 2 comments

Comments

@soroshsabz
Copy link

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
Copy link
Author

related to dotnet/runtime#71832
related to CommunityToolkit/dotnet#328

@soroshsabz
Copy link
Author

@khellang Any note about this issue?

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant