Update GenericSettingsProvider to use real version objects and be more careful about version order when getting previous

This commit is contained in:
2014-12-13 08:04:24 -05:00
parent a26f4686ab
commit d59618bda2

View File

@@ -19,10 +19,10 @@ namespace Common.Settings
public delegate object OpenDataStoreDelegate(); public delegate object OpenDataStoreDelegate();
public delegate void CloseDataStoreDelegate(object dataStore); public delegate void CloseDataStoreDelegate(object dataStore);
public delegate string GetSettingValueDelegate(object dataStore, string name, string version); public delegate string GetSettingValueDelegate(object dataStore, string name, Version version);
public delegate void SetSettingValueDelegate(object dataStore, string name, string version, string value); public delegate void SetSettingValueDelegate(object dataStore, string name, Version version, string value);
public delegate List<string> GetVersionListDelegate(object dataStore); public delegate List<Version> GetVersionListDelegate(object dataStore);
public delegate void DeleteSettingsForVersionDelegate(object dataStore, string version); public delegate void DeleteSettingsForVersionDelegate(object dataStore, Version version);
#endregion #endregion
@@ -61,7 +61,7 @@ namespace Common.Settings
var values = new SettingsPropertyValueCollection(); var values = new SettingsPropertyValueCollection();
// Get the current version number // Get the current version number
var version = GetCurrentVersionNumber(); var version = GetCurrentVersion();
// Open the data store // Open the data store
var dataStore = OpenDataStore(); var dataStore = OpenDataStore();
@@ -85,7 +85,7 @@ namespace Common.Settings
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection properties) public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection properties)
{ {
// Get the current version number // Get the current version number
var version = GetCurrentVersionNumber(); var version = GetCurrentVersion();
// Open the data store // Open the data store
var dataStore = OpenDataStore(); var dataStore = OpenDataStore();
@@ -109,27 +109,21 @@ namespace Common.Settings
#region Version numbers #region Version numbers
private static string GetCurrentVersionNumber() private static Version GetCurrentVersion()
{ {
return Assembly.GetEntryAssembly().GetName().Version.ToString(); return Assembly.GetEntryAssembly().GetName().Version;
} }
private string GetPreviousVersionNumber(object dataStore) private Version GetPreviousVersion(object dataStore)
{ {
// Get the current version number // Get the current version number
var currentVersion = GetCurrentVersionNumber(); var currentVersion = GetCurrentVersion();
// Get a distinct list of version numbers // Get a distinct list of version numbers
var versionList = GetVersionList(dataStore); var versionList = GetVersionList(dataStore);
// Remove the current version
versionList.Remove(currentVersion);
// Remove the empty version for the database version
versionList.Remove(string.Empty);
// Sort the list using the Version object and get the first value // Sort the list using the Version object and get the first value
var previousVersion = versionList.OrderByDescending(v => new Version(v)).FirstOrDefault(); var previousVersion = versionList.Where(v => v < currentVersion).OrderByDescending(v => v).FirstOrDefault();
return previousVersion; return previousVersion;
} }
@@ -138,7 +132,7 @@ namespace Common.Settings
#region Value get and set #region Value get and set
private SettingsPropertyValue GetPropertyValue(object dataStore, SettingsProperty property, string version) private SettingsPropertyValue GetPropertyValue(object dataStore, SettingsProperty property, Version version)
{ {
// Create the value for the property // Create the value for the property
var value = new SettingsPropertyValue(property); var value = new SettingsPropertyValue(property);
@@ -155,7 +149,7 @@ namespace Common.Settings
return value; return value;
} }
private void SetPropertyValue(object dataStore, SettingsPropertyValue value, string version) private void SetPropertyValue(object dataStore, SettingsPropertyValue value, Version version)
{ {
// Set the value for this version // Set the value for this version
SetSettingValue(dataStore, value.Property.Name, version, value.SerializedValue.ToString()); SetSettingValue(dataStore, value.Property.Name, version, value.SerializedValue.ToString());
@@ -168,7 +162,7 @@ namespace Common.Settings
public void Reset(SettingsContext context) public void Reset(SettingsContext context)
{ {
// Get the current version number // Get the current version number
var version = GetCurrentVersionNumber(); var version = GetCurrentVersion();
// Open the data store // Open the data store
var dataStore = OpenDataStore(); var dataStore = OpenDataStore();
@@ -186,12 +180,12 @@ namespace Common.Settings
var dataStore = OpenDataStore(); var dataStore = OpenDataStore();
// Get the previous version number // Get the previous version number
var previousVersion = GetPreviousVersionNumber(dataStore); var previousVersion = GetPreviousVersion(dataStore);
SettingsPropertyValue value; SettingsPropertyValue value;
// If there is no previous version we have a return a setting with a null value // If there is no previous version we have a return a setting with a null value
if (string.IsNullOrEmpty(previousVersion)) if (previousVersion == null)
{ {
// Create a new property value with the value set to null // Create a new property value with the value set to null
value = new SettingsPropertyValue(property) { PropertyValue = null }; value = new SettingsPropertyValue(property) { PropertyValue = null };
@@ -217,17 +211,17 @@ namespace Common.Settings
return; return;
// Get the previous version number // Get the previous version number
var previousVersion = GetPreviousVersionNumber(dataStore); var previousVersion = GetPreviousVersion(dataStore);
// If there is no previous version number just do nothing // If there is no previous version number just do nothing
if (string.IsNullOrEmpty(previousVersion)) if (previousVersion == null)
return; return;
// Delete everything for the current version // Delete everything for the current version
Reset(context); Reset(context);
// Get the current version number // Get the current version number
var currentVersion = GetCurrentVersionNumber(); var currentVersion = GetCurrentVersion();
// Loop over each property // Loop over each property
foreach (SettingsProperty property in properties) foreach (SettingsProperty property in properties)
@@ -245,10 +239,10 @@ namespace Common.Settings
// Get a distinct list of version numbers // Get a distinct list of version numbers
var versionList = GetVersionList(dataStore); var versionList = GetVersionList(dataStore);
// Remove the current version // Get everything before the current version
versionList.Remove(currentVersion); versionList = versionList.Where(v => v < currentVersion).ToList();
// Delete settings for anything left // Delete settings for anything in the list
foreach (var version in versionList) foreach (var version in versionList)
DeleteSettingsForVersion(dataStore, version); DeleteSettingsForVersion(dataStore, version);
} }