Options

Mutable Options

To change options in the application, we can use IOptionsMutable<T> interface:

    public interface IOptionsMutable<out T> : IOptionsSnapshot<T> where T : class, new()
    {
        // Update T field by field
        Task<bool> UpdateAsync(Action<T> applyChanges);
    }

To use IOptionsMutable<T> we must also register some required services.

    ...
    using Juice.Extensions.Options.DependencyInjection;
    ...
    // your changed options will be write to {WorkingDirectory}/appsettings.Development.json
    services.UseOptionsMutableFileStore("appsettings.Development.json");

    // OR separated appsettings file for strongly typed
    // services.UseOptionsMutableFileStore<Options>($"appsettings.Separated.Development.json");

    // configure your Options class before use
    services.ConfigureMutable<Options>(configuration.GetSection("Options"));

After that, we can inject IOptionsMutable<Options> and use it like IOptionsSnapshot<Options>. We can also call UpdateAsync method to update options.

    var options = serviceProvider.GetRequiredService<IOptionsMutable<Options>>();
    await options.UpdateAsync(o => {
        //o.Something = "something else";
    });

Per-tenant Options

Use ITenantsOptions<T> to access per-tenant configured options.

    // Per-tenant options snapshot
    public interface ITenantsOptions<T> : IOptionsSnapshot<T> where T : class, new()
    {
    }

We will register tenant configuration services to use per-tenant options accessor.

    ...
    using Juice.Extensions.Options.DependencyInjection;
    ...

    // register tenant configuration services
    services.AddTenantsConfiguration().AddTenantsJsonFile("appsettings.Development.json");

    // configure typed options
    services.ConfigureTenantsOptions<Options>("Options");

    ...

    // use ITenantsOptions like IOptionsSnapshot
    var options = serviceProvider.GetRequiredService<ITenantsOptions<Options>>();

NOTE: It requires ITenant service

Per-tenant Mutable Options

This is combined of per-tenant options and mutable options.

    public interface ITenantsOptionsMutable<T> : ITenantsOptions<T>, IOptionsMutable<T>
        where T : class, new()
    {
    }
    ...
    using Juice.Extensions.Options.DependencyInjection;
    ...

    // register tenant configuration services
    services.AddTenantsConfiguration().AddTenantsJsonFile("appsettings.Development.json");

    // register multi-tenant mutable configuration source
    services.UseTenantsOptionsMutableFileStore("appsettings.Development.json");

    // configure multi-tenant mutable options
    services.ConfigureTenantsOptionsMutable<Options>("Options");

    ...

    // use ITenantsOptionsMutable like IOptionsSnapshot
    var options = serviceProvider.GetRequiredService<ITenantsOptionsMutable<Options>>();

    // now we can update Options
    await options.UpdateAsync(o => {
        //o.Something = "something else";
    });

NOTE: It requires ITenant service

The library can be accessed via Nuget: