|
| 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.Collections.Generic; |
| 7 | +using System.Text; |
| 8 | +using Microsoft.Toolkit.Helpers; |
| 9 | + |
| 10 | +namespace Microsoft.Toolkit.Extensions |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Helpers methods for working with <see cref="ISettingsStorageHelper{TKey}"/> implementations. |
| 14 | + /// </summary> |
| 15 | + public static class ISettingsStorageHelperExtensions |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Attempts to read the provided key and return the value. |
| 19 | + /// If the key is not found, the fallback value will be used instead. |
| 20 | + /// </summary> |
| 21 | + /// <typeparam name="TKey">The type of key used to lookup the object.</typeparam> |
| 22 | + /// <typeparam name="TValue">The type of object value expected.</typeparam> |
| 23 | + /// <param name="storageHelper">The storage helper instance fo read from.</param> |
| 24 | + /// <param name="key">The key of the target object.</param> |
| 25 | + /// <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) |
| 28 | + where TKey : notnull |
| 29 | + { |
| 30 | + try |
| 31 | + { |
| 32 | + return storageHelper.Read<TValue>(key); |
| 33 | + } |
| 34 | + catch (KeyNotFoundException) |
| 35 | + { |
| 36 | + return fallback; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// <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. |
| 43 | + /// </summary> |
| 44 | + /// <typeparam name="TKey">The type of key used to lookup the object.</typeparam> |
| 45 | + /// <typeparam name="TValue">The type of object value expected.</typeparam> |
| 46 | + /// <param name="storageHelper">The storage helper instance to read from.</param> |
| 47 | + /// <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) |
| 52 | + where TKey : notnull |
| 53 | + { |
| 54 | + try |
| 55 | + { |
| 56 | + value = storageHelper.Read<TValue>(key); |
| 57 | + return true; |
| 58 | + } |
| 59 | + catch (KeyNotFoundException) |
| 60 | + { |
| 61 | + value = fallback; |
| 62 | + return false; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// <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. |
| 69 | + /// </summary> |
| 70 | + /// <typeparam name="TKey">The type of key used to lookup the object.</typeparam> |
| 71 | + /// <param name="storageHelper">The storage helper instance to delete from.</param> |
| 72 | + /// <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) |
| 75 | + where TKey : notnull |
| 76 | + { |
| 77 | + try |
| 78 | + { |
| 79 | + storageHelper.Delete(key); |
| 80 | + return true; |
| 81 | + } |
| 82 | + catch (KeyNotFoundException) |
| 83 | + { |
| 84 | + return false; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments