diff --git a/README.md b/README.md index 0d80e43..e18bb17 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,12 @@ public class ViewSettings : SettingsBase } ``` +## OPTIONAL: Override Settings Cache Path + +```c# +AppInfo.OverrideSettingsCachePath(path); +``` + ## Create an instance or get existing Settings SettingsCache Install ReactiveMarbles.CacheDatabase.Settings diff --git a/src/ReactiveMarbles.CacheDatabase.EncryptedSettings.Tests/SettingsCacheTests.cs b/src/ReactiveMarbles.CacheDatabase.EncryptedSettings.Tests/SettingsCacheTests.cs index b8be749..01763d5 100644 --- a/src/ReactiveMarbles.CacheDatabase.EncryptedSettings.Tests/SettingsCacheTests.cs +++ b/src/ReactiveMarbles.CacheDatabase.EncryptedSettings.Tests/SettingsCacheTests.cs @@ -14,6 +14,7 @@ public class SettingsCacheTests /// /// Test1s this instance. /// + /// Name of the override database. [Fact] public async void TestCreateAndInsert() { @@ -43,5 +44,16 @@ public async void TestUpdateAndRead() Assert.Equal(EnumTestValue.Option2, viewSettings.EnumTest); await viewSettings.DisposeAsync(); } + + /// + /// Tests the override settings cache path. + /// + [Fact] + public void TestOverrideSettingsCachePath() + { + const string path = "c:\\SettingsStoreage\\ApplicationSettings\\"; + AppInfo.OverrideSettingsCachePath(path); + Assert.Equal(path, AppInfo.SettingsCachePath); + } } } diff --git a/src/ReactiveMarbles.CacheDatabase.Settings.Tests/SettingsCacheTests.cs b/src/ReactiveMarbles.CacheDatabase.Settings.Tests/SettingsCacheTests.cs index d0d6d13..a915a13 100644 --- a/src/ReactiveMarbles.CacheDatabase.Settings.Tests/SettingsCacheTests.cs +++ b/src/ReactiveMarbles.CacheDatabase.Settings.Tests/SettingsCacheTests.cs @@ -41,5 +41,16 @@ public async void TestUpdateAndRead() Assert.Equal(EnumTestValue.Option2, viewSettings.EnumTest); await viewSettings.DisposeAsync(); } + + /// + /// Tests the override settings cache path. + /// + [Fact] + public void TestOverrideSettingsCachePath() + { + const string path = "c:\\SettingsStoreage\\ApplicationSettings\\"; + AppInfo.OverrideSettingsCachePath(path); + Assert.Equal(path, AppInfo.SettingsCachePath); + } } } diff --git a/src/ReactiveMarbles.CacheDatabase.Settings/Core/AppInfo.cs b/src/ReactiveMarbles.CacheDatabase.Settings/Core/AppInfo.cs index eb1d1fa..51c9c0b 100644 --- a/src/ReactiveMarbles.CacheDatabase.Settings/Core/AppInfo.cs +++ b/src/ReactiveMarbles.CacheDatabase.Settings/Core/AppInfo.cs @@ -4,14 +4,19 @@ using ReactiveMarbles.CacheDatabase.Core; using ReactiveMarbles.CacheDatabase.Settings.Core; + #if ENCRYPTED + using ReactiveMarbles.CacheDatabase.EncryptedSqlite3; + #else using ReactiveMarbles.CacheDatabase.Sqlite3; #endif + using System.Reflection; #if ENCRYPTED + namespace ReactiveMarbles.CacheDatabase.EncryptedSettings #else namespace ReactiveMarbles.CacheDatabase.Settings @@ -46,7 +51,7 @@ static AppInfo() /// /// The settings cache path. /// - public static string? SettingsCachePath { get; } + public static string? SettingsCachePath { get; private set; } /// /// Gets the executing assembly. @@ -76,49 +81,71 @@ static AppInfo() internal static Dictionary SettingsStores { get; } + /// + /// Overrides the settings cache path. + /// + /// The path. + public static void OverrideSettingsCachePath(string path) + { + SettingsCachePath = path; + } + /// /// Deletes the settings store. /// /// The type of store to delete. - /// A Task. - public static async Task DeleteSettingsStore() + /// Name of the override database. + /// + /// A Task. + /// + public static async Task DeleteSettingsStore(string? overrideDatabaseName = null) { await DisposeSettingsStore().ConfigureAwait(false); - File.Delete(Path.Combine(SettingsCachePath!, $"{typeof(T).Name}.db")); + File.Delete(Path.Combine(SettingsCachePath!, $"{overrideDatabaseName ?? typeof(T).Name}.db")); } /// /// Gets the settings store. /// /// The store to get. - /// A Settings Store. - public static ISettingsStorage? GetSettingsStore() => - SettingsStores[typeof(T).Name]; + /// Name of the override database. + /// + /// A Settings Store. + /// + public static ISettingsStorage? GetSettingsStore(string? overrideDatabaseName = null) => + SettingsStores[overrideDatabaseName ?? typeof(T).Name]; /// /// Disposes the settings store. /// /// The type of store. - /// A Task. - public static async Task DisposeSettingsStore() + /// Name of the override database. + /// + /// A Task. + /// + public static async Task DisposeSettingsStore(string? overrideDatabaseName = null) { - await GetSettingsStore()!.DisposeAsync().ConfigureAwait(false); - await BlobCaches[typeof(T).Name]!.DisposeAsync().ConfigureAwait(false); + await GetSettingsStore(overrideDatabaseName)!.DisposeAsync().ConfigureAwait(false); + await BlobCaches[overrideDatabaseName ?? typeof(T).Name]!.DisposeAsync().ConfigureAwait(false); } #if ENCRYPTED + /// /// Setup the secure settings store. /// /// The Type of settings store. /// Secure password. /// Initialise the Settings values. - /// The Settings store. - public static async Task SetupSettingsStore(string password, bool initialise = true) + /// Name of the override database. + /// + /// The Settings store. + /// + public static async Task SetupSettingsStore(string password, bool initialise = true, string? overrideDatabaseName = null) where T : ISettingsStorage?, new() { Directory.CreateDirectory(SettingsCachePath!); - BlobCaches[typeof(T).Name] = new EncryptedSqliteBlobCache(Path.Combine(SettingsCachePath!, $"{typeof(T).Name}.db"), password); + BlobCaches[typeof(T).Name] = new EncryptedSqliteBlobCache(Path.Combine(SettingsCachePath!, $"{overrideDatabaseName ?? typeof(T).Name}.db"), password); var viewSettings = new T(); SettingsStores[typeof(T).Name] = viewSettings; @@ -129,6 +156,7 @@ public static async Task DisposeSettingsStore() return viewSettings; } + #else /// @@ -136,12 +164,15 @@ public static async Task DisposeSettingsStore() /// /// The Type of settings store. /// Initialise the Settings values. - /// The Settings store. - public static async Task SetupSettingsStore(bool initialise = true) + /// Name of the override database. + /// + /// The Settings store. + /// + public static async Task SetupSettingsStore(bool initialise = true, string? overrideDatabaseName = null) where T : ISettingsStorage?, new() { Directory.CreateDirectory(SettingsCachePath!); - BlobCaches[typeof(T).Name] = new SqliteBlobCache(Path.Combine(SettingsCachePath!, $"{typeof(T).Name}.db")); + BlobCaches[typeof(T).Name] = new SqliteBlobCache(Path.Combine(SettingsCachePath!, $"{overrideDatabaseName ?? typeof(T).Name}.db")); var viewSettings = new T(); SettingsStores[typeof(T).Name] = viewSettings;