Base solution for your next web application
Open Closed

Dynamic Settings #5077


User avatar
0
jefftindall created

I am trying to refresh the settings cache to "discover" a new setting without having to recycle the entire site, but can't seem to find a reasonable way to do so in the framework.

I believe that if I were to be able to call SettingDefinitionManager.Initialize(), then it would cause the new definitions to be discovered and included. Am I missing anything else that is obvious?

Thanks, Jeff


2 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    First of all, you cannot achieve this. Because ABP needs a pre-defined setting. SettingManager.Initialize() will not work. You cannot grab a setting value with SettingManager, just inserting into the database. But you can get it via Repository.

  • User Avatar
    0
    alper created
    Support Team

    Hey, I created a Gist to show you how to get/set setting value via the repository. <a class="postlink" href="https://gist.github.com/ebicoglu/7f217f26a1d94e374aa1ad41910a0e82">https://gist.github.com/ebicoglu/7f217f ... 41910a0e82</a>

    using System.Linq;
    using Abp.Collections.Extensions;
    using Abp.Configuration;
    using Abp.Dependency;
    using Abp.Domain.Repositories;
    using Abp.Domain.Uow;
    
    namespace MyCompanyName.AbpZeroTemplate.MySettingService
    {
        public class DynamicSettingService : ITransientDependency
        {
            private readonly IRepository<Setting, long> _settingRepository;
            private readonly IUnitOfWorkManager _unitOfWorkManager;
    
            public DynamicSettingService(IRepository<Setting, long> settingRepository, 
                IUnitOfWorkManager unitOfWorkManager)
            {
                _settingRepository = settingRepository;
                _unitOfWorkManager = unitOfWorkManager;
            }
    
            public string GetSettingValue(string settingName, int? tenantId = null, int? userId = null)
            {
                var setting = _settingRepository
                    .GetAll()
                    .Where(x => x.Name == settingName)
                    .WhereIf(tenantId.HasValue, x => x.TenantId == tenantId.Value)
                    .WhereIf(userId.HasValue, x => x.UserId == userId.Value)
                    .FirstOrDefault();
    
                return setting?.Value;
            }
    
            public void SetSettingValue(string settingName, string settingValue, int? tenantId = null, int? userId = null)
            {
                var setting = _settingRepository
                    .GetAll()
                    .Where(x => x.Name == settingName)
                    .WhereIf(tenantId.HasValue, x => x.TenantId == tenantId.Value)
                    .WhereIf(userId.HasValue, x => x.UserId == userId.Value)
                    .FirstOrDefault();
    
                if (setting == null)
                {
                    _settingRepository.InsertAndGetId(new Setting(tenantId, userId, settingName, settingValue));
                }
                else
                {
                    setting.Value = settingValue;
                    _settingRepository.Update(setting);
                }
    
                _unitOfWorkManager.Current.SaveChanges();
            }
        }
    
        public class MyTestUsageClass : ITransientDependency
        {
            private readonly DynamicSettingService _dynamicSettingService;
    
            public MyTestUsageClass(DynamicSettingService dynamicSettingService)
            {
                _dynamicSettingService = dynamicSettingService;
            }
    
            private void Test()
            {
                var mySettingValue = _dynamicSettingService.GetSettingValue("MySettingName");
                _dynamicSettingService.SetSettingValue("MySettingName", "123456");
            }
        }
    }