Exemplo n.º 1
0
        public SettingsManager(SettingsPersistance persistance)
        {
            this._persistance = persistance;

            if ( _persistance == SettingsPersistance.LocalXmlFile)
            {
                _localXmlFile = AssemblyPath + ".config.xml";
            }
        }
Exemplo n.º 2
0
        private void PersistTestSettings <T>(T settings, string matchingId, SettingsType settingsType) where T : IPersistableSettings
        {
            XmlDocument suiteSettings = GetSettingsXmlFile(settingsType);

            XmlNode matchingElement = suiteSettings.SelectSingleNode(string.Format(TestSettingsXPathQuery, matchingId));

            if (matchingElement == null)
            {
                matchingElement = suiteSettings.CreateElement("testSetting");
                XmlAttribute elementId = suiteSettings.CreateAttribute("id");
                elementId.InnerText = matchingId;
                matchingElement.Attributes.Append(elementId);
                suiteSettings.DocumentElement.AppendChild(matchingElement);
            }

            matchingElement.InnerText = SettingsPersistance.Serialize(settings);

            SaveSettingsXmlFile(suiteSettings, settingsType);
        }
Exemplo n.º 3
0
        private T RehydrateSettings <T>(string settingId, string jsonString, SettingsType settingsType) where T : IPersistableSettings, new()
        {
            var retObject = SettingsPersistance.Deserialize <T>(jsonString);

            retObject.AssignSettingId(settingId);

            // check object for any null properties, if there are any, hydrate a new object and copy the values ito retObject
            Type oType = typeof(T);

            var nullProperties = new List <string>();

            bool anyChanged = false;

            PropertyInfo[] properties = oType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo property in properties)
            {
                object value = property.GetValue(retObject, null);

                // if the object is a IHydratableSettings
                if (typeof(ISelfHydratableSetting).IsAssignableFrom(property.PropertyType))
                {
                    // if the value is null, just add it to the null list and move on
                    if (value == null)
                    {
                        nullProperties.Add(property.Name);
                    }
                    else
                    {
                        anyChanged = RehydrateChildSettings((ISelfHydratableSetting)value);
                        // the property isn't null, so check each property of it
                    }
                }
                else
                {
                    bool bNullable = property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>);

                    // if the type is nullable, we want to ignore it as a null might be legit
                    if (value == null && !bNullable)
                    {
                        nullProperties.Add(property.Name);
                    }
                }
            }

            if (nullProperties.Any())
            {
                var defaultT = new T();
                defaultT.HydrateWithDefaults();

                foreach (string propName in nullProperties)
                {
                    string       name     = propName;
                    PropertyInfo property = properties.First(s => s.Name == name);

                    object defaultValue = property.GetValue(defaultT, null);

                    if (defaultValue != null)
                    {
                        property.SetValue(retObject, defaultValue);
                        anyChanged = true;
                    }
                }
            }

            if (anyChanged)
            {
                PersistTestSettings(retObject, settingId, settingsType);
            }

            return(retObject);
        }