Asp.net Boilerplate - Implement setting manager with database

5.3k views Asked by At

I've been building an asp.net core website, using the asp.net boilerplate template. As of now, I've been storing all of the settings in the appsettings.json file. As the application gets bigger, I'm thinking I should start storing some settings via ABP's SettingProvider and ISettingStore.

My question is, does anyone have, or know of, a sample application that show's how to implement ISettingStore and storing the settings in the database?

The only post I could find so far is this, but the link hikalkan supplies is broken.

Thanks for any help, Joe

1

There are 1 answers

10
Alper Ebicoglu On

ABP stores settings on memory with default values. When you insert a new setting value into database, then it reads from database and overrides the default value. So basically when database has no settings then it means all the settings are on default values. Setting values are stored in AbpSettings table. To start using settings mechanism. Create your own SettingProvider inherited from SettingProvider. Initialize it in your module (eg: ModuleZeroSampleProjectApplicationModule). As SettingProvider is automatically registed to dependency injection; You can inject ISettingManager wherever you want.

public class MySettingProvider : SettingProvider
{
    public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
    {
        return new[]
                {
                    new SettingDefinition(
                        "SmtpServerAddress",
                        "127.0.0.1"
                        ),

                    new SettingDefinition(
                        "PassiveUsersCanNotLogin",
                        "true",
                        scopes: SettingScopes.Application | SettingScopes.Tenant
                        ),

                    new SettingDefinition(
                        "SiteColorPreference",
                        "red",
                        scopes: SettingScopes.User,
                        isVisibleToClients: true
                        )

                };
    }
}

In application services and controllers you don't need to inject ISettingManager (because there's already property injected) and you can directly use SettingManager property. Forexample :

   //Getting a boolean value (async call)
    var value1 = await SettingManager.GetSettingValueAsync<bool>("PassiveUsersCanNotLogin");

And for the other classes (like Domain Services) can inject ISettingManager

   public class UserEmailer :  ITransientDependency
   {

            private readonly ISettingManager _settingManager;

            public UserEmailer(ISettingManager settingManager)
            {
                _settingManager = settingManager;            
            }

            [UnitOfWork]
            public virtual async Task TestMethod()
            {
                 var settingValue = _settingManager.GetSettingValueForUser("SmtpServerAddress", tenantAdmin.TenantId, tenantAdmin.Id);
            }
   }

Note: To modify a setting you can use these methods in SettingManager ChangeSettingForApplicationAsync, ChangeSettingForTenantAsync and ChangeSettingForUserAsync