public SettingEntry GetSettingByName(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            var cacheKey = CacheKey.With(GetType(), "GetSettingByName", name);
            var result   = _memoryCache.GetOrCreateExclusive(cacheKey, (cacheEntry) =>
            {
                SettingEntry setting = null;
                //Get setting definition from module manifest first
                var moduleSetting = GetModuleSettingByName(name);
                if (moduleSetting != null)
                {
                    setting = moduleSetting.ToSettingEntry();
                }
                using (var repository = _repositoryFactory())
                {
                    //try to load setting from db
                    var settingEntity = repository.GetSettingByName(name);
                    if (settingEntity != null)
                    {
                        setting = settingEntity.ToModel(setting ?? AbstractTypeFactory <SettingEntry> .TryCreateInstance());
                    }
                }
                //Add cache  expiration token for setting
                cacheEntry.AddExpirationToken(SettingsCacheRegion.CreateChangeToken(setting));

                return(setting);
            });

            return(result);
        }
Exemple #2
0
 protected virtual void ClearCache(IEnumerable <ObjectSettingEntry> objectSettings)
 {
     //Clear setting from cache
     foreach (var setting in objectSettings)
     {
         SettingsCacheRegion.ExpireSetting(setting);
     }
 }
 private void ClearCache(SettingEntry[] settings)
 {
     //Clear setting from cache
     foreach (var setting in settings)
     {
         SettingsCacheRegion.ExpireSetting(setting);
     }
 }
        public virtual async Task <IEnumerable <ObjectSettingEntry> > GetObjectSettingsAsync(IEnumerable <string> names, string objectType = null, string objectId = null)
        {
            if (names == null)
            {
                throw new ArgumentNullException(nameof(names));
            }
            var cacheKey = CacheKey.With(GetType(), "GetSettingByNamesAsync", string.Join(";", names), objectType, objectId);
            var result   = await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                var resultObjectSettings = new List <ObjectSettingEntry>();
                var dbStoredSettings     = new List <SettingEntity>();

                //Try to load setting value from DB
                using (var repository = _repositoryFactory())
                {
                    repository.DisableChangesTracking();
                    //try to load setting from db
                    dbStoredSettings.AddRange(await repository.GetObjectSettingsByNamesAsync(names.ToArray(), objectType, objectId));
                }

                foreach (var name in names)
                {
                    var settingDescriptor = _registeredSettingsByNameDict[name];
                    if (settingDescriptor == null)
                    {
                        throw new PlatformException($"Setting with name {name} is not registered");
                    }
                    var objectSetting = new ObjectSettingEntry(settingDescriptor)
                    {
                        ObjectType = objectType,
                        ObjectId   = objectId
                    };
                    var dbSetting = dbStoredSettings.FirstOrDefault(x => x.Name.EqualsInvariant(name));
                    if (dbSetting != null)
                    {
                        objectSetting = dbSetting.ToModel(objectSetting);
                    }
                    resultObjectSettings.Add(objectSetting);

                    //Add cache  expiration token for setting
                    cacheEntry.AddExpirationToken(SettingsCacheRegion.CreateChangeToken(objectSetting));
                }
                return(resultObjectSettings);
            });

            return(result);
        }
Exemple #5
0
        public virtual async Task <SettingEntry> GetSettingByNameAsync(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var cacheKey = CacheKey.With(GetType(), "GetSettingByNameAsync", name);
            var result   = await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                SettingEntry setting = null;
                //Get setting definition from module manifest first
                var moduleSetting = GetModuleSettingByName(name);
                if (moduleSetting != null)
                {
                    setting = moduleSetting.ToSettingEntry();
                }
                using (var repository = _repositoryFactory())
                {
                    //try to load setting from db
                    var settingEntity = await repository.GetSettingByNameAsync(name);
                    if (settingEntity != null)
                    {
                        setting = settingEntity.ToModel(setting ?? AbstractTypeFactory <SettingEntry> .TryCreateInstance());
                    }
                }
                //Create new setting for unregistered setting name
                if (setting == null)
                {
                    setting      = AbstractTypeFactory <SettingEntry> .TryCreateInstance();
                    setting.Name = name;
                }
                //Add cache  expiration token for setting
                cacheEntry.AddExpirationToken(SettingsCacheRegion.CreateChangeToken(setting));

                return(setting);
            });

            return(result);
        }
        public void LoadEntitySettingsValues(Entity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            if (entity.IsTransient())
            {
                throw new ArgumentException("entity must have Id");
            }

            var entityType     = entity.GetType().Name;
            var cacheKey       = CacheKey.With(GetType(), "LoadEntitySettingsValues", entityType, entity.Id);
            var storedSettings = _memoryCache.GetOrCreateExclusive(cacheKey, (cacheEntry) =>
            {
                var settingEntries = new List <SettingEntry>();
                using (var repository = _repositoryFactory())
                {
                    foreach (var settingEntry in repository.GetAllObjectSettings(entityType, entity.Id).Select(x => x.ToModel(AbstractTypeFactory <SettingEntry> .TryCreateInstance())))
                    {
                        //Add cache  expiration token for setting
                        cacheEntry.AddExpirationToken(SettingsCacheRegion.CreateChangeToken(settingEntry));
                        settingEntries.Add(settingEntry);
                    }
                    return(settingEntries);
                }
            });

            //Deep load settings values for all object contains settings
            var haveSettingsObjects = entity.GetFlatObjectsListWithInterface <IHaveSettings>();

            foreach (var haveSettingsObject in haveSettingsObjects)
            {
                // Replace settings values with stored in database
                if (haveSettingsObject.Settings != null)
                {
                    //Need clone settings entry because it may be shared for multiple instances
                    haveSettingsObject.Settings = haveSettingsObject.Settings.Select(x => (SettingEntry)x.Clone()).ToList();

                    foreach (var setting in haveSettingsObject.Settings)
                    {
                        var storedSetting = storedSettings.FirstOrDefault(x => x.Name.EqualsInvariant(setting.Name));
                        //First try to used stored object setting values
                        if (storedSetting != null)
                        {
                            setting.Value       = storedSetting.Value;
                            setting.ArrayValues = storedSetting.ArrayValues;
                        }
                        else if (setting.Value == null && setting.ArrayValues == null)
                        {
                            //try to use global setting value
                            var globalSetting = GetSettingByName(setting.Name);
                            if (setting.IsArray)
                            {
                                setting.ArrayValues = globalSetting.ArrayValues ?? new[] { globalSetting.DefaultValue };
                            }
                            else
                            {
                                setting.Value = globalSetting.Value ?? globalSetting.DefaultValue;
                            }
                        }
                    }
                }
            }
        }