Skip to content

Commit 5962eec

Browse files
authored
Feature Add OverrideSettingsCachePath and overrideDatabaseName (#99)
* Feature Add OverrideSettingsCachePath and overrideDatabaseName fix for #98 Allow manual configuration of the Directory and FileName for the db. * Add missing overrideDatabaseName
1 parent 2f9fcaf commit 5962eec

File tree

4 files changed

+77
-17
lines changed

4 files changed

+77
-17
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ public class ViewSettings : SettingsBase
127127
}
128128
```
129129

130+
## OPTIONAL: Override Settings Cache Path
131+
132+
```c#
133+
AppInfo.OverrideSettingsCachePath(path);
134+
```
135+
130136
## Create an instance or get existing Settings SettingsCache
131137

132138
Install ReactiveMarbles.CacheDatabase.Settings

src/ReactiveMarbles.CacheDatabase.EncryptedSettings.Tests/SettingsCacheTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class SettingsCacheTests
1414
/// <summary>
1515
/// Test1s this instance.
1616
/// </summary>
17+
/// <param name="overrideDatabaseName">Name of the override database.</param>
1718
[Fact]
1819
public async void TestCreateAndInsert()
1920
{
@@ -43,5 +44,16 @@ public async void TestUpdateAndRead()
4344
Assert.Equal(EnumTestValue.Option2, viewSettings.EnumTest);
4445
await viewSettings.DisposeAsync();
4546
}
47+
48+
/// <summary>
49+
/// Tests the override settings cache path.
50+
/// </summary>
51+
[Fact]
52+
public void TestOverrideSettingsCachePath()
53+
{
54+
const string path = "c:\\SettingsStoreage\\ApplicationSettings\\";
55+
AppInfo.OverrideSettingsCachePath(path);
56+
Assert.Equal(path, AppInfo.SettingsCachePath);
57+
}
4658
}
4759
}

src/ReactiveMarbles.CacheDatabase.Settings.Tests/SettingsCacheTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,16 @@ public async void TestUpdateAndRead()
4141
Assert.Equal(EnumTestValue.Option2, viewSettings.EnumTest);
4242
await viewSettings.DisposeAsync();
4343
}
44+
45+
/// <summary>
46+
/// Tests the override settings cache path.
47+
/// </summary>
48+
[Fact]
49+
public void TestOverrideSettingsCachePath()
50+
{
51+
const string path = "c:\\SettingsStoreage\\ApplicationSettings\\";
52+
AppInfo.OverrideSettingsCachePath(path);
53+
Assert.Equal(path, AppInfo.SettingsCachePath);
54+
}
4455
}
4556
}

src/ReactiveMarbles.CacheDatabase.Settings/Core/AppInfo.cs

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44

55
using ReactiveMarbles.CacheDatabase.Core;
66
using ReactiveMarbles.CacheDatabase.Settings.Core;
7+
78
#if ENCRYPTED
9+
810
using ReactiveMarbles.CacheDatabase.EncryptedSqlite3;
11+
912
#else
1013
using ReactiveMarbles.CacheDatabase.Sqlite3;
1114
#endif
15+
1216
using System.Reflection;
1317

1418
#if ENCRYPTED
19+
1520
namespace ReactiveMarbles.CacheDatabase.EncryptedSettings
1621
#else
1722
namespace ReactiveMarbles.CacheDatabase.Settings
@@ -46,7 +51,7 @@ static AppInfo()
4651
/// <value>
4752
/// The settings cache path.
4853
/// </value>
49-
public static string? SettingsCachePath { get; }
54+
public static string? SettingsCachePath { get; private set; }
5055

5156
/// <summary>
5257
/// Gets the executing assembly.
@@ -76,49 +81,71 @@ static AppInfo()
7681

7782
internal static Dictionary<string, ISettingsStorage?> SettingsStores { get; }
7883

84+
/// <summary>
85+
/// Overrides the settings cache path.
86+
/// </summary>
87+
/// <param name="path">The path.</param>
88+
public static void OverrideSettingsCachePath(string path)
89+
{
90+
SettingsCachePath = path;
91+
}
92+
7993
/// <summary>
8094
/// Deletes the settings store.
8195
/// </summary>
8296
/// <typeparam name="T">The type of store to delete.</typeparam>
83-
/// <returns>A Task.</returns>
84-
public static async Task DeleteSettingsStore<T>()
97+
/// <param name="overrideDatabaseName">Name of the override database.</param>
98+
/// <returns>
99+
/// A Task.
100+
/// </returns>
101+
public static async Task DeleteSettingsStore<T>(string? overrideDatabaseName = null)
85102
{
86103
await DisposeSettingsStore<T>().ConfigureAwait(false);
87-
File.Delete(Path.Combine(SettingsCachePath!, $"{typeof(T).Name}.db"));
104+
File.Delete(Path.Combine(SettingsCachePath!, $"{overrideDatabaseName ?? typeof(T).Name}.db"));
88105
}
89106

90107
/// <summary>
91108
/// Gets the settings store.
92109
/// </summary>
93110
/// <typeparam name="T">The store to get.</typeparam>
94-
/// <returns>A Settings Store.</returns>
95-
public static ISettingsStorage? GetSettingsStore<T>() =>
96-
SettingsStores[typeof(T).Name];
111+
/// <param name="overrideDatabaseName">Name of the override database.</param>
112+
/// <returns>
113+
/// A Settings Store.
114+
/// </returns>
115+
public static ISettingsStorage? GetSettingsStore<T>(string? overrideDatabaseName = null) =>
116+
SettingsStores[overrideDatabaseName ?? typeof(T).Name];
97117

98118
/// <summary>
99119
/// Disposes the settings store.
100120
/// </summary>
101121
/// <typeparam name="T">The type of store.</typeparam>
102-
/// <returns>A Task.</returns>
103-
public static async Task DisposeSettingsStore<T>()
122+
/// <param name="overrideDatabaseName">Name of the override database.</param>
123+
/// <returns>
124+
/// A Task.
125+
/// </returns>
126+
public static async Task DisposeSettingsStore<T>(string? overrideDatabaseName = null)
104127
{
105-
await GetSettingsStore<T>()!.DisposeAsync().ConfigureAwait(false);
106-
await BlobCaches[typeof(T).Name]!.DisposeAsync().ConfigureAwait(false);
128+
await GetSettingsStore<T>(overrideDatabaseName)!.DisposeAsync().ConfigureAwait(false);
129+
await BlobCaches[overrideDatabaseName ?? typeof(T).Name]!.DisposeAsync().ConfigureAwait(false);
107130
}
108131

109132
#if ENCRYPTED
133+
110134
/// <summary>
111135
/// Setup the secure settings store.
112136
/// </summary>
113137
/// <typeparam name="T">The Type of settings store.</typeparam>
114138
/// <param name="password">Secure password.</param>
115139
/// <param name="initialise">Initialise the Settings values.</param>
116-
/// <returns>The Settings store.</returns>
117-
public static async Task<T?> SetupSettingsStore<T>(string password, bool initialise = true)
140+
/// <param name="overrideDatabaseName">Name of the override database.</param>
141+
/// <returns>
142+
/// The Settings store.
143+
/// </returns>
144+
public static async Task<T?> SetupSettingsStore<T>(string password, bool initialise = true, string? overrideDatabaseName = null)
118145
where T : ISettingsStorage?, new()
119146
{
120147
Directory.CreateDirectory(SettingsCachePath!);
121-
BlobCaches[typeof(T).Name] = new EncryptedSqliteBlobCache(Path.Combine(SettingsCachePath!, $"{typeof(T).Name}.db"), password);
148+
BlobCaches[typeof(T).Name] = new EncryptedSqliteBlobCache(Path.Combine(SettingsCachePath!, $"{overrideDatabaseName ?? typeof(T).Name}.db"), password);
122149

123150
var viewSettings = new T();
124151
SettingsStores[typeof(T).Name] = viewSettings;
@@ -129,19 +156,23 @@ public static async Task DisposeSettingsStore<T>()
129156

130157
return viewSettings;
131158
}
159+
132160
#else
133161

134162
/// <summary>
135163
/// Setup the settings store.
136164
/// </summary>
137165
/// <typeparam name="T">The Type of settings store.</typeparam>
138166
/// <param name="initialise">Initialise the Settings values.</param>
139-
/// <returns>The Settings store.</returns>
140-
public static async Task<T?> SetupSettingsStore<T>(bool initialise = true)
167+
/// <param name="overrideDatabaseName">Name of the override database.</param>
168+
/// <returns>
169+
/// The Settings store.
170+
/// </returns>
171+
public static async Task<T?> SetupSettingsStore<T>(bool initialise = true, string? overrideDatabaseName = null)
141172
where T : ISettingsStorage?, new()
142173
{
143174
Directory.CreateDirectory(SettingsCachePath!);
144-
BlobCaches[typeof(T).Name] = new SqliteBlobCache(Path.Combine(SettingsCachePath!, $"{typeof(T).Name}.db"));
175+
BlobCaches[typeof(T).Name] = new SqliteBlobCache(Path.Combine(SettingsCachePath!, $"{overrideDatabaseName ?? typeof(T).Name}.db"));
145176

146177
var viewSettings = new T();
147178
SettingsStores[typeof(T).Name] = viewSettings;

0 commit comments

Comments
 (0)