Skip to content

Commit 82c2ba3

Browse files
committed
PR updates
1 parent c36db17 commit 82c2ba3

File tree

10 files changed

+317
-77
lines changed

10 files changed

+317
-77
lines changed

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,16 @@ public T Read<T>(string key, T @default = default)
9999
}
100100

101101
/// <inheritdoc />
102-
public T Read<T>(string key)
102+
public bool TryRead<T>(string key, out T value)
103103
{
104104
if (Settings.Values.TryGetValue(key, out var valueObj) && valueObj != null)
105105
{
106-
return Serializer.Deserialize<T>(valueObj as string);
106+
value = Serializer.Deserialize<T>(valueObj as string);
107+
return true;
107108
}
108109

109-
throw new KeyNotFoundException(key);
110+
value = default;
111+
return false;
110112
}
111113

112114
/// <inheritdoc />
@@ -116,13 +118,9 @@ public void Save<T>(string key, T value)
116118
}
117119

118120
/// <inheritdoc />
119-
public void Delete(string key)
121+
public bool TryDelete(string key)
120122
{
121-
var removed = Settings.Values.Remove(key);
122-
if (!removed)
123-
{
124-
throw new KeyNotFoundException(key);
125-
}
123+
return Settings.Values.Remove(key);
126124
}
127125

128126
/// <inheritdoc />

Microsoft.Toolkit/Extensions/ISettingsStorageHelperExtensions.cs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,66 +22,59 @@ public static class ISettingsStorageHelperExtensions
2222
/// <typeparam name="TValue">The type of object value expected.</typeparam>
2323
/// <param name="storageHelper">The storage helper instance fo read from.</param>
2424
/// <param name="key">The key of the target object.</param>
25+
/// <param name="value">The value of the target object, or the fallback value.</param>
2526
/// <param name="fallback">An alternative value returned if the read fails.</param>
26-
/// <returns>The value of the target object, or the fallback value.</returns>
27-
public static TValue? GetValueOrDefault<TKey, TValue>(this ISettingsStorageHelper<TKey> storageHelper, TKey key, TValue? fallback = default)
27+
/// <returns>A boolean indicator of success.</returns>
28+
public static bool TryGetValueOrDefault<TKey, TValue>(this ISettingsStorageHelper<TKey> storageHelper, TKey key, out TValue? value, TValue? fallback = default)
2829
where TKey : notnull
2930
{
30-
try
31+
if (storageHelper.TryRead<TValue>(key, out TValue? storedValue))
3132
{
32-
return storageHelper.Read<TValue>(key);
33+
value = storedValue;
34+
return true;
3335
}
34-
catch (KeyNotFoundException)
36+
else
3537
{
36-
return fallback;
38+
value = fallback;
39+
return false;
3740
}
3841
}
3942

4043
/// <summary>
41-
/// Attempts to perform read the provided key and returns a boolean indicator of success.
42-
/// If the key is not found, the fallback value will be used instead.
44+
/// Read the key in the storage helper instance and get the value.
4345
/// </summary>
4446
/// <typeparam name="TKey">The type of key used to lookup the object.</typeparam>
4547
/// <typeparam name="TValue">The type of object value expected.</typeparam>
46-
/// <param name="storageHelper">The storage helper instance to read from.</param>
48+
/// <param name="storageHelper">The storage helper instance fo read from.</param>
4749
/// <param name="key">The key of the target object.</param>
48-
/// <param name="value">The value of the target object, or the fallback value.</param>
49-
/// <param name="fallback">An alternate value returned if the read fails.</param>
50-
/// <returns>A boolean indicator of success.</returns>
51-
public static bool TryRead<TKey, TValue>(this ISettingsStorageHelper<TKey> storageHelper, TKey key, out TValue? value, TValue? fallback = default)
50+
/// <returns>The value of the target object</returns>
51+
/// <exception cref="KeyNotFoundException">Throws when the key is not found in storage.</exception>
52+
public static TValue? Read<TKey, TValue>(this ISettingsStorageHelper<TKey> storageHelper, TKey key)
5253
where TKey : notnull
5354
{
54-
try
55+
if (storageHelper.TryRead<TValue>(key, out TValue? value))
5556
{
56-
value = storageHelper.Read<TValue>(key);
57-
return true;
57+
return value;
5858
}
59-
catch (KeyNotFoundException)
59+
else
6060
{
61-
value = fallback;
62-
return false;
61+
throw new KeyNotFoundException();
6362
}
6463
}
6564

6665
/// <summary>
67-
/// Attempts to remove an object by key and returns a boolean indicator of success.
68-
/// If the key is not found, the method will return false.
66+
/// Deletes a key from storage.
6967
/// </summary>
7068
/// <typeparam name="TKey">The type of key used to lookup the object.</typeparam>
7169
/// <param name="storageHelper">The storage helper instance to delete from.</param>
7270
/// <param name="key">The key of the target object.</param>
73-
/// <returns>A boolean indicator of success.</returns>
74-
public static bool TryDelete<TKey>(this ISettingsStorageHelper<TKey> storageHelper, TKey key)
71+
/// <exception cref="KeyNotFoundException">Throws when the key is not found in storage.</exception>
72+
public static void Delete<TKey>(this ISettingsStorageHelper<TKey> storageHelper, TKey key)
7573
where TKey : notnull
7674
{
77-
try
75+
if (!storageHelper.TryDelete(key))
7876
{
79-
storageHelper.Delete(key);
80-
return true;
81-
}
82-
catch (KeyNotFoundException)
83-
{
84-
return false;
77+
throw new KeyNotFoundException();
8578
}
8679
}
8780
}

Microsoft.Toolkit/Helpers/ObjectStorage/ISettingsStorageHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public interface ISettingsStorageHelper<in TKey>
1818
/// </summary>
1919
/// <typeparam name="TValue">Type of object retrieved.</typeparam>
2020
/// <param name="key">Key of the object.</param>
21-
/// <exception cref="KeyNotFoundException">Throws when the specified key is not found.</exception>
22-
/// <returns>The <see typeparamref="TValue"/> object for <see typeparamref="TKey"/> key.</returns>
23-
TValue? Read<TValue>(TKey key);
21+
/// <param name="value">The <see typeparamref="TValue"/> object for <see typeparamref="TKey"/> key.</param>
22+
/// <returns>A boolean indicator of success.</returns>
23+
bool TryRead<TValue>(TKey key, out TValue? value);
2424

2525
/// <summary>
2626
/// Saves a single item by its key.
@@ -34,8 +34,8 @@ public interface ISettingsStorageHelper<in 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>
38-
void Delete(TKey key);
37+
/// <returns>A boolean indicator of success.</returns>
38+
bool TryDelete(TKey key);
3939

4040
/// <summary>
4141
/// Clear all keys and values from the settings store.

UnitTests/UnitTests.UWP/Helpers/JsonObjectSerializer.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
using System;
66
using System.Reflection;
7-
using Microsoft.Toolkit.Helpers;
7+
using Microsoft.Toolkit.Uwp.Helpers;
88
using Newtonsoft.Json;
99

10-
namespace UnitTests.UWP.Helpers
10+
namespace UnitTests.Helpers
1111
{
1212
/// <summary>
1313
/// This is a Serializer which should mimic the previous functionality of 6.1.1 release of the Toolkit with Newtonsoft.Json.
14+
/// Based on <see cref="Microsoft.Toolkit.Helpers.Uwp.IObjectSerializer"/>.
1415
/// </summary>
1516
internal class JsonObjectSerializer : IObjectSerializer
1617
{
17-
public T Deserialize<T>(string value)
18+
public T Deserialize<T>(object value)
1819
{
1920
var type = typeof(T);
2021
var typeInfo = type.GetTypeInfo();
@@ -26,10 +27,10 @@ public T Deserialize<T>(string value)
2627
return (T)Convert.ChangeType(value, type);
2728
}
2829

29-
return JsonConvert.DeserializeObject<T>(value);
30+
return JsonConvert.DeserializeObject<T>((string)value);
3031
}
3132

32-
public string Serialize<T>(T value)
33+
public object Serialize<T>(T value)
3334
{
3435
var type = typeof(T);
3536
var typeInfo = type.GetTypeInfo();
@@ -38,7 +39,7 @@ public string Serialize<T>(T value)
3839
// This if/return combo is to maintain compatibility with 6.1.1
3940
if (typeInfo.IsPrimitive || type == typeof(string))
4041
{
41-
return value.ToString();
42+
return value;
4243
}
4344

4445
return JsonConvert.SerializeObject(value);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Reflection;
7+
using Microsoft.Toolkit.Helpers;
8+
using Newtonsoft.Json;
9+
10+
namespace UnitTests.Helpers
11+
{
12+
/// <summary>
13+
/// This is a Serializer which should mimic the previous functionality of 6.1.1 release of the Toolkit with Newtonsoft.Json.
14+
/// Based on <see cref="Microsoft.Toolkit.Helpers.IObjectSerializer"/>.
15+
/// </summary>
16+
internal class JsonObjectSerializer2 : IObjectSerializer
17+
{
18+
public T Deserialize<T>(string value)
19+
{
20+
var type = typeof(T);
21+
var typeInfo = type.GetTypeInfo();
22+
23+
// Note: If you're creating a new app, you could just use the serializer directly.
24+
// This if/return combo is to maintain compatibility with 6.1.1
25+
if (typeInfo.IsPrimitive || type == typeof(string))
26+
{
27+
return (T)Convert.ChangeType(value, type);
28+
}
29+
30+
return JsonConvert.DeserializeObject<T>(value);
31+
}
32+
33+
public string Serialize<T>(T value)
34+
{
35+
var type = typeof(T);
36+
var typeInfo = type.GetTypeInfo();
37+
38+
// Note: If you're creating a new app, you could just use the serializer directly.
39+
// This if/return combo is to maintain compatibility with 6.1.1
40+
if (typeInfo.IsPrimitive || type == typeof(string))
41+
{
42+
return value.ToString();
43+
}
44+
45+
return JsonConvert.SerializeObject(value);
46+
}
47+
}
48+
}

UnitTests/UnitTests.UWP/Helpers/SystemTextJsonSerializer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
using System;
66
using System.Text.Json;
7-
using Microsoft.Toolkit.Helpers;
7+
using Microsoft.Toolkit.Uwp.Helpers;
88

9-
namespace UnitTests.UWP.Helpers
9+
namespace UnitTests.Helpers
1010
{
1111
/// <summary>
1212
/// Example class of writing a new <see cref="IObjectSerializer"/> that uses System.Text.Json.
13+
/// Based on <see cref="Microsoft.Toolkit.Helpers.Uwp.IObjectSerializer"/>.
1314
/// </summary>
1415
internal class SystemTextJsonSerializer : IObjectSerializer
1516
{
16-
public T Deserialize<T>(string value) => JsonSerializer.Deserialize<T>(value);
17+
public T Deserialize<T>(object value) => JsonSerializer.Deserialize<T>(value as string);
1718

18-
public string Serialize<T>(T value) => JsonSerializer.Serialize(value);
19+
public object Serialize<T>(T value) => JsonSerializer.Serialize(value);
1920
}
2021
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Text.Json;
6+
using Microsoft.Toolkit.Helpers;
7+
8+
namespace UnitTests.Helpers
9+
{
10+
/// <summary>
11+
/// Example class of writing a new <see cref="IObjectSerializer"/> that uses System.Text.Json.
12+
/// Based on <see cref="Microsoft.Toolkit.Helpers.IObjectSerializer"/>.
13+
/// </summary>
14+
internal class SystemTextJsonSerializer2 : IObjectSerializer
15+
{
16+
public T Deserialize<T>(string value) => JsonSerializer.Deserialize<T>(value);
17+
18+
public string Serialize<T>(T value) => JsonSerializer.Serialize(value);
19+
}
20+
}

0 commit comments

Comments
 (0)