Ejemplo n.º 1
0
        /// <summary>
        /// Resolve effective setting within the purpose for the specified entity
        /// </summary>
        /// <param name="entity">Entity to for which settings are to be resolved</param>
        /// <param name="purpose">The purpose of the settings which are to be resolved</param>
        /// <param name="name">Name of the setting for the specified entity and purpose</param>
        /// <returns>Effective setting for the specified entity, purpose, and name</returns>
        /// <exception cref="ArgumentException">One of the specified arguments fail validation</exception>
        /// <exception cref="UnknownEntityType">Specified entity type is not known within settings schema</exception>
        /// <exception cref="UnknownSetting">Specified setting name is not known within settings schema</exception>
        public Setting ResolveSetting(EntityIdentifier entity, string purpose, string name)
        {
            #region input validation

            ValidateEntityIdentifier(entity);

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(name));
            }

            #endregion

            #region input clean-up

            entity = Normalize(entity);

            purpose = NormalizePurposeName(purpose);

            name = name.Trim();

            #endregion

            IEntityType entityType = _settingsSchema.GetEntityType(entity.Type);

            /*
             *      An exception should be thrown if entity-type does not exist, e.g. incorrect or has been removed from schema,
             *      to alert developer of usage of removed/unknown entity-type
             */
            if (entityType == null)
            {
                throw new UnknownEntityType();
            }

            IEnumerable <SettingDefinition> settingDefinitions = entityType.GetSettings(purpose, name);

            SettingDefinition settingDefinition = settingDefinitions.FirstOrDefault();

            /*
             *      An exception should be thrown if setting definition does not exist, e.g. incorrect or has been removed from schema,
             *      to alert developer of usage of removed/unknown setting
             */
            if (settingDefinition == null)
            {
                throw new UnknownSetting();
            }

            Setting setting = _settingStore.GetSetting(entity, purpose, name);

            return(ResolveSetting(purpose, name, setting, settingDefinition));
        }
Ejemplo n.º 2
0
        private static Setting ResolveSetting(string purpose, string name, Setting setting, SettingDefinition settingDefinition)
        {
            if (setting != null)
            {
                // override stored setting default value with 'current' setting definition default value
                if (setting.ValueSource.Type == SettingValueSourceType.Default)
                {
                    if (settingDefinition.HasDefaultValue)
                    {
                        setting.Value = settingDefinition.DefaultValue;
                    }
                }

                return(setting);
            }

            // if no stored setting is found, return setting based on 'default' specified in SettingDefinition
            if (settingDefinition.HasDefaultValue)
            {
                return new Setting
                       {
                           Purpose     = purpose,
                           Name        = settingDefinition.Name,
                           Value       = settingDefinition.DefaultValue,
                           ValueSource = new SettingValueSource
                           {
                               Type = SettingValueSourceType.Default
                           }
                       }
            }
            ;

            // no stored setting found and setting definition has no 'default' value
            return(null);
        }