From b0a33c559b498fb595cf2c742d64d1ceacd7550c Mon Sep 17 00:00:00 2001 From: Shane Weaver Date: Thu, 29 Jul 2021 11:10:17 -0700 Subject: [PATCH] Fixed warnings in ObjectStorage classes --- .../ApplicationDataStorageHelper.cs | 118 +++++++++--------- .../ObjectStorage/IObjectStorageHelper.cs | 66 +++++----- .../ObjectStorage/LocalObjectStorageHelper.cs | 6 +- 3 files changed, 95 insertions(+), 95 deletions(-) diff --git a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/ApplicationDataStorageHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/ApplicationDataStorageHelper.cs index ed487ec2cae..2cf1c568f54 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/ApplicationDataStorageHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/ApplicationDataStorageHelper.cs @@ -20,37 +20,25 @@ namespace Microsoft.Toolkit.Uwp.Helpers public partial class ApplicationDataStorageHelper : IFileStorageHelper, ISettingsStorageHelper { /// - /// Get a new instance using ApplicationData.Current and the provided serializer. - /// - /// Serializer for converting stored values. Defaults to . - /// A new instance of ApplicationDataStorageHelper. - public static ApplicationDataStorageHelper GetCurrent(Toolkit.Helpers.IObjectSerializer? objectSerializer = null) - { - var appData = ApplicationData.Current; - return new ApplicationDataStorageHelper(appData, objectSerializer); - } - - /// - /// Get a new instance using the ApplicationData for the provided user and serializer. + /// Initializes a new instance of the class. /// - /// App data user owner. + /// The data store to interact with. /// Serializer for converting stored values. Defaults to . - /// A new instance of ApplicationDataStorageHelper. - public static async Task GetForUserAsync(User user, Toolkit.Helpers.IObjectSerializer? objectSerializer = null) + public ApplicationDataStorageHelper(ApplicationData appData, Toolkit.Helpers.IObjectSerializer? objectSerializer = null) { - var appData = await ApplicationData.GetForUserAsync(user); - return new ApplicationDataStorageHelper(appData, objectSerializer); + this.AppData = appData ?? throw new ArgumentNullException(nameof(appData)); + this.Serializer = objectSerializer ?? new Toolkit.Helpers.SystemSerializer(); } /// /// Gets the settings container. /// - public ApplicationDataContainer Settings => AppData.LocalSettings; + public ApplicationDataContainer Settings => this.AppData.LocalSettings; /// /// Gets the storage folder. /// - public StorageFolder Folder => AppData.LocalFolder; + public StorageFolder Folder => this.AppData.LocalFolder; /// /// Gets the storage host. @@ -63,14 +51,26 @@ public static async Task GetForUserAsync(User user protected Toolkit.Helpers.IObjectSerializer Serializer { get; } /// - /// Initializes a new instance of the class. + /// Get a new instance using ApplicationData.Current and the provided serializer. /// - /// The data store to interact with. /// Serializer for converting stored values. Defaults to . - public ApplicationDataStorageHelper(ApplicationData appData, Toolkit.Helpers.IObjectSerializer? objectSerializer = null) + /// A new instance of ApplicationDataStorageHelper. + public static ApplicationDataStorageHelper GetCurrent(Toolkit.Helpers.IObjectSerializer? objectSerializer = null) + { + var appData = ApplicationData.Current; + return new ApplicationDataStorageHelper(appData, objectSerializer); + } + + /// + /// Get a new instance using the ApplicationData for the provided user and serializer. + /// + /// App data user owner. + /// Serializer for converting stored values. Defaults to . + /// A new instance of ApplicationDataStorageHelper. + public static async Task GetForUserAsync(User user, Toolkit.Helpers.IObjectSerializer? objectSerializer = null) { - AppData = appData ?? throw new ArgumentNullException(nameof(appData)); - Serializer = objectSerializer ?? new Toolkit.Helpers.SystemSerializer(); + var appData = await ApplicationData.GetForUserAsync(user); + return new ApplicationDataStorageHelper(appData, objectSerializer); } /// @@ -80,7 +80,7 @@ public ApplicationDataStorageHelper(ApplicationData appData, Toolkit.Helpers.IOb /// True if a value exists. public bool KeyExists(string key) { - return Settings.Values.ContainsKey(key); + return this.Settings.Values.ContainsKey(key); } /// @@ -89,23 +89,23 @@ public bool KeyExists(string key) /// Type of object retrieved. /// Key of the object. /// Default value of the object. - /// The TValue object - public T Read(string key, T @default = default) + /// The TValue object. + public T? Read(string key, T? @default = default) { - if (!Settings.Values.TryGetValue(key, out var valueObj) || valueObj == null) + if (this.Settings.Values.TryGetValue(key, out var valueObj) && valueObj is string valueString) { - return @default; + return this.Serializer.Deserialize(valueString); } - return Serializer.Deserialize(valueObj as string); + return @default; } /// - public bool TryRead(string key, out T value) + public bool TryRead(string key, out T? value) { - if (Settings.Values.TryGetValue(key, out var valueObj) && valueObj != null) + if (this.Settings.Values.TryGetValue(key, out var valueObj) && valueObj is string valueString) { - value = Serializer.Deserialize(valueObj as string); + value = this.Serializer.Deserialize(valueString); return true; } @@ -116,19 +116,19 @@ public bool TryRead(string key, out T value) /// public void Save(string key, T value) { - Settings.Values[key] = Serializer.Serialize(value); + this.Settings.Values[key] = this.Serializer.Serialize(value); } /// public bool TryDelete(string key) { - return Settings.Values.Remove(key); + return this.Settings.Values.Remove(key); } /// public void Clear() { - Settings.Values.Clear(); + this.Settings.Values.Clear(); } /// @@ -139,7 +139,7 @@ public void Clear() /// True if a value exists. public bool KeyExists(string compositeKey, string key) { - if (TryRead(compositeKey, out ApplicationDataCompositeValue composite)) + if (this.TryRead(compositeKey, out ApplicationDataCompositeValue? composite) && composite != null) { return composite.ContainsKey(key); } @@ -155,14 +155,14 @@ public bool KeyExists(string compositeKey, string key) /// Key of the object. /// The value of the object retrieved. /// The T object. - public bool TryRead(string compositeKey, string key, out T value) + public bool TryRead(string compositeKey, string key, out T? value) { - if (TryRead(compositeKey, out ApplicationDataCompositeValue composite)) + if (this.TryRead(compositeKey, out ApplicationDataCompositeValue? composite) && composite != null) { string compositeValue = (string)composite[key]; if (compositeValue != null) { - value = Serializer.Deserialize(compositeValue); + value = this.Serializer.Deserialize(compositeValue); return true; } } @@ -179,13 +179,13 @@ public bool TryRead(string compositeKey, string key, out T value) /// Key of the object. /// Default value of the object. /// The T object. - public T Read(string compositeKey, string key, T @default = default) + public T? Read(string compositeKey, string key, T? @default = default) { - if (TryRead(compositeKey, out ApplicationDataCompositeValue composite)) + if (this.TryRead(compositeKey, out ApplicationDataCompositeValue? composite) && composite != null) { if (composite.TryGetValue(key, out object valueObj) && valueObj is string value) { - return Serializer.Deserialize(value); + return this.Serializer.Deserialize(value); } } @@ -202,17 +202,17 @@ public T Read(string compositeKey, string key, T @default = default) /// Objects to save. public void Save(string compositeKey, IDictionary values) { - if (TryRead(compositeKey, out ApplicationDataCompositeValue composite)) + if (this.TryRead(compositeKey, out ApplicationDataCompositeValue? composite) && composite != null) { foreach (KeyValuePair setting in values) { if (composite.ContainsKey(setting.Key)) { - composite[setting.Key] = Serializer.Serialize(setting.Value); + composite[setting.Key] = this.Serializer.Serialize(setting.Value); } else { - composite.Add(setting.Key, Serializer.Serialize(setting.Value)); + composite.Add(setting.Key, this.Serializer.Serialize(setting.Value)); } } } @@ -221,10 +221,10 @@ public void Save(string compositeKey, IDictionary values) composite = new ApplicationDataCompositeValue(); foreach (KeyValuePair setting in values) { - composite.Add(setting.Key, Serializer.Serialize(setting.Value)); + composite.Add(setting.Key, this.Serializer.Serialize(setting.Value)); } - Settings.Values[compositeKey] = composite; + this.Settings.Values[compositeKey] = composite; } } @@ -236,7 +236,7 @@ public void Save(string compositeKey, IDictionary values) /// A boolean indicator of success. public bool TryDelete(string compositeKey, string key) { - if (TryRead(compositeKey, out ApplicationDataCompositeValue composite)) + if (this.TryRead(compositeKey, out ApplicationDataCompositeValue? composite) && composite != null) { return composite.Remove(key); } @@ -245,33 +245,33 @@ public bool TryDelete(string compositeKey, string key) } /// - public Task ReadFileAsync(string filePath, T @default = default) + public Task ReadFileAsync(string filePath, T? @default = default) { - return ReadFileAsync(Folder, filePath, @default); + return this.ReadFileAsync(this.Folder, filePath, @default); } /// public Task> ReadFolderAsync(string folderPath) { - return ReadFolderAsync(Folder, folderPath); + return this.ReadFolderAsync(this.Folder, folderPath); } /// public Task CreateFileAsync(string filePath, T value) { - return SaveFileAsync(Folder, filePath, value); + return this.SaveFileAsync(this.Folder, filePath, value); } /// public Task CreateFolderAsync(string folderPath) { - return CreateFolderAsync(Folder, folderPath); + return this.CreateFolderAsync(this.Folder, folderPath); } /// public Task DeleteItemAsync(string itemPath) { - return DeleteItemAsync(Folder, itemPath); + return this.DeleteItemAsync(this.Folder, itemPath); } /// @@ -283,13 +283,13 @@ public Task DeleteItemAsync(string itemPath) /// Waiting task until completion. public Task SaveFileAsync(string filePath, T value) { - return SaveFileAsync(Folder, filePath, value); + return this.SaveFileAsync(this.Folder, filePath, value); } - private async Task ReadFileAsync(StorageFolder folder, string filePath, T @default = default) + private async Task ReadFileAsync(StorageFolder folder, string filePath, T? @default = default) { string value = await StorageFileHelper.ReadTextFromFileAsync(folder, filePath); - return (value != null) ? Serializer.Deserialize(value) : @default; + return (value != null) ? this.Serializer.Deserialize(value) : @default; } private async Task> ReadFolderAsync(StorageFolder folder, string folderPath) @@ -309,7 +309,7 @@ private async Task ReadFileAsync(StorageFolder folder, string filePath, T private Task SaveFileAsync(StorageFolder folder, string filePath, T value) { - return StorageFileHelper.WriteTextToFileAsync(folder, Serializer.Serialize(value)?.ToString(), filePath, CreationCollisionOption.ReplaceExisting); + return StorageFileHelper.WriteTextToFileAsync(folder, this.Serializer.Serialize(value)?.ToString(), filePath, CreationCollisionOption.ReplaceExisting); } private async Task CreateFolderAsync(StorageFolder folder, string folderPath) diff --git a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/IObjectStorageHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/IObjectStorageHelper.cs index e742fe18586..ecdb7e8bcc1 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/IObjectStorageHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/IObjectStorageHelper.cs @@ -18,43 +18,43 @@ public interface IObjectStorageHelper /// /// Determines whether a setting already exists. /// - /// Key of the setting (that contains object) - /// True if a value exists + /// Key of the setting (that contains object). + /// True if a value exists. bool KeyExists(string key); /// /// Determines whether a setting already exists in composite. /// - /// Key of the composite (that contains settings) - /// Key of the setting (that contains object) - /// True if a value exists + /// Key of the composite (that contains settings). + /// Key of the setting (that contains object). + /// True if a value exists. bool KeyExists(string compositeKey, string key); /// /// Retrieves a single item by its key. /// - /// Type of object retrieved - /// Key of the object - /// Default value of the object - /// The T object - T Read(string key, T @default = default(T)); + /// Type of object retrieved. + /// Key of the object. + /// Default value of the object. + /// The T object. + T Read(string key, T @default = default); /// /// Retrieves a single item by its key in composite. /// - /// Type of object retrieved - /// Key of the composite (that contains settings) - /// Key of the object - /// Default value of the object - /// The T object - T Read(string compositeKey, string key, T @default = default(T)); + /// Type of object retrieved. + /// Key of the composite (that contains settings). + /// Key of the object. + /// Default value of the object. + /// The T object. + T Read(string compositeKey, string key, T @default = default); /// /// Saves a single item by its key. /// - /// Type of object saved - /// Key of the value saved - /// Object to save + /// Type of object saved. + /// Key of the value saved. + /// Object to save. void Save(string key, T value); /// @@ -63,34 +63,34 @@ public interface IObjectStorageHelper /// (refers to for complex/large objects) and for groups of settings which /// need to be treated in an atomic way. /// - /// Type of object saved - /// Key of the composite (that contains settings) - /// Objects to save + /// Type of object saved. + /// Key of the composite (that contains settings). + /// Objects to save. void Save(string compositeKey, IDictionary values); /// /// Determines whether a file already exists. /// - /// Key of the file (that contains object) - /// True if a value exists + /// Key of the file (that contains object). + /// True if a value exists. Task FileExistsAsync(string filePath); /// /// Retrieves an object from a file. /// - /// Type of object retrieved - /// Path to the file that contains the object - /// Default value of the object - /// Waiting task until completion with the object in the file - Task ReadFileAsync(string filePath, T @default = default(T)); + /// Type of object retrieved. + /// Path to the file that contains the object. + /// Default value of the object. + /// Waiting task until completion with the object in the file. + Task ReadFileAsync(string filePath, T @default = default); /// /// Saves an object inside a file. /// - /// Type of object saved - /// Path to the file that will contain the object - /// Object to save - /// Waiting task until completion + /// Type of object saved. + /// Path to the file that will contain the object. + /// Object to save. + /// Waiting task until completion. Task SaveFileAsync(string filePath, T value); } } \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs index 45905f73051..30df84933e3 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/LocalObjectStorageHelper.cs @@ -17,14 +17,14 @@ public class LocalObjectStorageHelper : BaseObjectStorageHelper /// Initializes a new instance of the class, /// which can read and write data using the provided ; /// In 6.1 and older the default Serializer was based on Newtonsoft.Json. - /// To implement an based on System.Text.Json, Newtonsoft.Json, or DataContractJsonSerializer see https://aka.ms/wct/storagehelper-migration + /// To implement an based on System.Text.Json, Newtonsoft.Json, or DataContractJsonSerializer see https://aka.ms/wct/storagehelper-migration. /// /// The serializer to use. public LocalObjectStorageHelper(IObjectSerializer objectSerializer) : base(objectSerializer) { - Settings = ApplicationData.Current.LocalSettings; - Folder = ApplicationData.Current.LocalFolder; + this.Settings = ApplicationData.Current.LocalSettings; + this.Folder = ApplicationData.Current.LocalFolder; } } } \ No newline at end of file