Exemplo n.º 1
0
        /// <summary>
        /// Resolve all effective settings within a 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>
        /// <returns>All effective settings for the specified entity and purpose</returns>
        /// <exception cref="UnknownEntityType">When specified entity type is not known within settings schema</exception>
        public IDictionary <string, Setting> ResolveSettings(EntityIdentifier entity, string purpose)
        {
            #region input validation

            ValidateEntityIdentifier(entity);

            #endregion

            #region input clean-up

            entity = Normalize(entity);

            purpose = NormalizePurposeName(purpose);

            #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);

            IDictionary <string, Setting> storedSettings = _settingStore.GetSettings(entity, purpose).ToDictionary(x => x.Name);

            IDictionary <string, Setting> resolvedSettings = new Dictionary <string, Setting>(settingDefinitions.Count());

            Setting setting;

            foreach (SettingDefinition definition in settingDefinitions)
            {
                setting = null;

                if (storedSettings.ContainsKey(definition.Name))
                {
                    setting = storedSettings[definition.Name];
                }

                resolvedSettings.Add(definition.Name, ResolveSetting(purpose, definition.Name, setting, definition));
            }

            return(resolvedSettings);
        }