Exemple #1
0
        public static T Get <T>(this ISettingsProvider settingsProvider, object id = null) where T : SettingsObject
        {
            SettingsObject settings = null;

            if (!settingsProvider.ContainsSetting(typeof(T), id))
            {
                settings = settingsProvider.NewSetting(typeof(T), id) as T;
                if (settings == null)
                {
                    throw new Exception(string.Format("Settings provider did not create a settings object of requested type '{0}'", typeof(T).FullName));
                }

                if (settingsProvider.AutoSaveNewSettings)
                {
                    settingsProvider.SaveSetting(settings);
                }

                return((T)settings);
            }
            settings = settingsProvider.LoadSetting(typeof(T), id) as T;
            if (settings == null)
            {
                throw new Exception(string.Format("Settings provider did not load a settings object of requested type '{0}'", typeof(T).FullName));
            }
            return((T)settings);
        }
        public override void DeleteSetting(SettingsObject settings)
        {
            var filename = DetermineFilepath(settings);

            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
        }
        public virtual void SaveSetting(SettingsObject settings)
        {
            //var clone = Tools.Object.Clone(settings);
            //Tools.Object.EncryptMembers(clone);
            var undoEncryption = false;

            try {
                if (EncryptSettings)
                {
                    Tools.Object.EncryptMembers(settings);
                    undoEncryption = true;
                }
                SaveInternal(settings);
            } finally {
                if (undoEncryption)
                {
                    Tools.Object.DecryptMembers(settings);
                }
            }
        }
        protected override void SaveInternal(SettingsObject settings)
        {
            if (!Directory.Exists(BaseDirectory))
            {
                Directory.CreateDirectory(BaseDirectory);
            }
            var filePath   = DetermineFilepath(settings);
            var fileExists = File.Exists(filePath);

            try
            {
                XmlProvider.DeepWriteToFile(DetermineFilepath(settings), settings);
            } catch {
                if (!fileExists && File.Exists(filePath))
                {
                    Tools.Exceptions.ExecuteIgnoringException(() => File.Delete(filePath));
                }
                throw;
            }
        }
 protected abstract void SaveInternal(SettingsObject settings);
 public abstract void DeleteSetting(SettingsObject settings);
Exemple #7
0
        public static bool Has <T>(this ISettingsProvider settingsProvider, object id = null) where T : SettingsObject
        {
            SettingsObject settings = null;

            return(settingsProvider.ContainsSetting(typeof(T), id));
        }
Exemple #8
0
        public static void ReloadSetting(this ISettingsProvider settingsProvider, SettingsObject setting)
        {
            var reloaded = settingsProvider.LoadSetting(setting.GetType(), setting.ID);

            Tools.Object.CopyMembers(setting, reloaded);
        }
 protected string DetermineFilepath(SettingsObject settings)
 {
     return(DetermineFilepath(settings.GetType(), settings.ID));
 }
 protected virtual string DetermineFilename(SettingsObject settingsObject)
 {
     Preconditions.CheckNotNull(settingsObject, "settingsObject");
     return(DetermineFilename(settingsObject.GetType(), settingsObject.ID));
 }