Search code examples
c#settingsmulti-tenantaspnetboilerplate

Set tenant settings


I am working on a multi-tenant application, and my application has a Currency setting.

public class MyAppSettingProvider : SettingProvider
{
    public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
    {
        return new[]
        {
            new SettingDefinition(MyAppSettingDefinition.Currency, string.Empty, scopes: SettingScopes.Tenant, isVisibleToClients: true),
        };
    }
}

public static class MyAppSettingDefinition
{
    public const string Currency = "MyApp.UserManagement.Currency";
}

I have added it in PreInitialize() method like below.

Configuration.Settings.Providers.Add<MyAppSettingProvider>();

My issue:

Now, this setting only creates entry for default Tenant, and there are no entries for other tenants.

So, my questions are:

  • How can I set Tenant specific setting?

  • Is it going to be on tenant creation time? If yes, then can I use MyAppSettingProvider class again?

  • Is it ok if I remove default Tenant? Because host does not have Tenant Id, and I have a fixed number of tenants where default is not required.

Guide me, what will be best practice that also supports boilerplate architecture..


Solution

  • how can I set Tenant Specific setting?

    Inject ISettingManager and do:

    _settingManager.ChangeSettingForTenantAsync(tenantId, MyAppSettingDefinition.Currency, "$");
    

    Is it going to be on tenant creation time? if yes, then can I use MyAppSettingProvider class again?

    Settings are not created automatically. You can configure in Create method of TenantAppService. You can (but shouldn't have to) use MyAppSettingProvider. ISettingManager is what you'll want.

    Is it ok if I remove default Tenant? because host does not have Tenant Id, and I have fixed number of tenants where default is not required.

    Yes. It's created by the template for convenience and demonstration.

    what will be best practice that also supports boilerplate architecture..

    Read the documentation on Setting Management.