Skip to content

Commit 0e414ec

Browse files
committed
Removed default param from ISettingsStorageHelper.Read method and updated exception handling in ApplicationDataStorageHelper for missing keys
1 parent 57d872b commit 0e414ec

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/ApplicationDataStorageHelper.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ public bool KeyExists(string key)
8181
return Settings.Values.ContainsKey(key);
8282
}
8383

84-
/// <inheritdoc />
84+
/// <summary>
85+
/// Retrieves a single item by its key.
86+
/// </summary>
87+
/// <typeparam name="T">Type of object retrieved.</typeparam>
88+
/// <param name="key">Key of the object.</param>
89+
/// <param name="default">Default value of the object.</param>
90+
/// <returns>The TValue object</returns>
8591
public T Read<T>(string key, T @default = default)
8692
{
8793
if (!Settings.Values.TryGetValue(key, out var valueObj) || valueObj == null)
@@ -92,6 +98,17 @@ public T Read<T>(string key, T @default = default)
9298
return Serializer.Deserialize<T>(valueObj as string);
9399
}
94100

101+
/// <inheritdoc />
102+
public T Read<T>(string key)
103+
{
104+
if (Settings.Values.TryGetValue(key, out var valueObj) && valueObj != null)
105+
{
106+
return Serializer.Deserialize<T>(valueObj as string);
107+
}
108+
109+
throw new KeyNotFoundException(key);
110+
}
111+
95112
/// <inheritdoc />
96113
public void Save<T>(string key, T value)
97114
{
@@ -101,7 +118,11 @@ public void Save<T>(string key, T value)
101118
/// <inheritdoc />
102119
public void Delete(string key)
103120
{
104-
Settings.Values.Remove(key);
121+
var removed = Settings.Values.Remove(key);
122+
if (!removed)
123+
{
124+
throw new KeyNotFoundException(key);
125+
}
105126
}
106127

107128
/// <inheritdoc />
@@ -196,12 +217,19 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
196217
/// </summary>
197218
/// <param name="compositeKey">Key of the composite (that contains settings).</param>
198219
/// <param name="key">Key of the object.</param>
220+
/// <exception cref="KeyNotFoundException">Throws when the specified composite/settings key is not found.</exception>
199221
public void Delete(string compositeKey, string key)
200222
{
201-
if (KeyExists(compositeKey))
223+
if (!KeyExists(compositeKey))
202224
{
203-
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)Settings.Values[compositeKey];
204-
composite.Remove(key);
225+
throw new KeyNotFoundException($"Composite key not found: {compositeKey}");
226+
}
227+
228+
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)Settings.Values[compositeKey];
229+
230+
if (!composite.Remove(key))
231+
{
232+
throw new KeyNotFoundException($"Settings key not found: {key}");
205233
}
206234
}
207235

Microsoft.Toolkit/Helpers/ObjectStorage/ISettingsStorageHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public interface ISettingsStorageHelper<TKey>
1818
/// </summary>
1919
/// <typeparam name="TValue">Type of object retrieved.</typeparam>
2020
/// <param name="key">Key of the object.</param>
21-
/// <param name="default">Default value of the object.</param>
21+
/// <exception cref="KeyNotFoundException">Throws when the specified key is not found.</exception>
2222
/// <returns>The TValue object</returns>
23-
TValue? Read<TValue>(TKey key, TValue? @default = default);
23+
TValue? Read<TValue>(TKey key);
2424

2525
/// <summary>
2626
/// Saves a single item by its key.
@@ -34,6 +34,7 @@ public interface ISettingsStorageHelper<TKey>
3434
/// Deletes a single item by its key.
3535
/// </summary>
3636
/// <param name="key">Key of the object.</param>
37+
/// <exception cref="KeyNotFoundException">Throws when the specified key is not found.</exception>
3738
void Delete(TKey key);
3839

3940
/// <summary>

UnitTests/UnitTests.UWP/Helpers/Test_StorageHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ namespace UnitTests.Helpers
1515
[TestClass]
1616
public class Test_StorageHelper
1717
{
18-
private readonly ISettingsStorageHelper<string> _settingsStorage_System = ApplicationDataStorageHelper.GetCurrent();
19-
private readonly ISettingsStorageHelper<string> _settingsStorage_JsonCompat = ApplicationDataStorageHelper.GetCurrent(new JsonObjectSerializer());
20-
private readonly ISettingsStorageHelper<string> _settingsStorage_JsonNew = ApplicationDataStorageHelper.GetCurrent(new SystemTextJsonSerializer());
18+
private readonly ApplicationDataStorageHelper _settingsStorage_System = ApplicationDataStorageHelper.GetCurrent();
19+
private readonly ApplicationDataStorageHelper _settingsStorage_JsonCompat = ApplicationDataStorageHelper.GetCurrent(new JsonObjectSerializer());
20+
private readonly ApplicationDataStorageHelper _settingsStorage_JsonNew = ApplicationDataStorageHelper.GetCurrent(new SystemTextJsonSerializer());
2121

2222
/// <summary>
2323
/// Checks that we're running 10.0.3 version of Newtonsoft.Json package which we used in 6.1.1.

0 commit comments

Comments
 (0)