Open Closed

SettingProvider and persistence #452


0
sarausan created

Hi,

after reading <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Setting-Management">http://www.aspnetboilerplate.com/Pages/ ... Management</a> it is not clear to me how to store settings in a dbcontext.

I have a context created using the Module-Zero migrations and the following code:

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

public class AppSettingProvider : SettingProvider { public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context) { return new List<SettingDefinition> { new SettingDefinition("CustomSetting1", "TestValue1", scopes: SettingScopes.Application), new SettingDefinition("CustomSetting2", "TestValue2", scopes: SettingScopes.Application) }; } }

How can I store those settings in the database?

Thanks


3 Answer(s)
  • 0
    hikalkan created
    Support Team

    Settings are not stored in the database, setting values are stored. To store/get setting values, use ISettingManager.

    Additional info: Setting definitions are programmatic things. In some place of your code, you will need to a setting and you will call ISettingManager.GetSettingValue("CustomSetting1"). So, this setting name will be hardcoded. You don't get "CustomSetting1" string from database (How could you?). You get it's current value (for a tenant or user). So, storing setting names in database is not needed.

  • 0
    sarausan created

    Thank you for clarifying those aspects, my idea was to implement a mechanism to store some of the settings (e.g. server/Web Service URLs, port numbers, common prefixes) in the database in order to avoid to hardcode them.

    I though that something like:

    SettingDefinition("CustomSetting1", "TestValue1", scopes: SettingScopes.Application),

    Would/could have created an entry in the database with CustomSetting1 as the key and TestValue1 as the corresponding value. It's not hard to implement a mechanism like that from scratch, but I was wondering if ASP.NET Boilerplate already provides that kind of features.

    I'd use it mainly to store values [by module / by application... not by user] that I don't want to hardcode.

  • 0
    hikalkan created
    Support Team

    You can store them in database (in AbpSettings table), no problem. As I understood, you want to store 'default values' of settings in the database. ABP does not save default values in the database since it's not needed. If you store default value in the database and then change default value, you should update database too.

    But, if you want that; this is the way:

    1. Define your default values as null as below: new SettingDefinition("CustomSetting1", null, scopes: SettingScopes.Application)

    2. Then write a database seed code as TenantId = null, UserId=null, Name="CustomSetting1", Value="YourDefaultValue"

    These approach may work fine for SettingScopes.Application. But what if your setting is per tenant?

    • When you create a new tenant, you should insert a row for each settings.
    • When you add a new setting, you should insert a row for each tenant. If setting is per user, it will be similar too.

    So, it's your choice.