diff --git a/src/Blazored.LocalStorage.TestExtensions/BUnitLocalStorageTestExtensions.cs b/src/Blazored.LocalStorage.TestExtensions/BUnitLocalStorageTestExtensions.cs index 1018400..5f0c19d 100644 --- a/src/Blazored.LocalStorage.TestExtensions/BUnitLocalStorageTestExtensions.cs +++ b/src/Blazored.LocalStorage.TestExtensions/BUnitLocalStorageTestExtensions.cs @@ -15,7 +15,7 @@ public static class BUnitLocalStorageTestExtensions public static ILocalStorageService AddBlazoredLocalStorage(this TestContextBase context) => AddBlazoredLocalStorage(context, null); - public static ILocalStorageService AddBlazoredLocalStorage(this TestContextBase context, Action configure) + public static ILocalStorageService AddBlazoredLocalStorage(this TestContextBase context, Action? configure) { if (context is null) throw new ArgumentNullException(nameof(context)); diff --git a/src/Blazored.LocalStorage.TestExtensions/Blazored.LocalStorage.TestExtensions.csproj b/src/Blazored.LocalStorage.TestExtensions/Blazored.LocalStorage.TestExtensions.csproj index a907a80..c8ed1fc 100644 --- a/src/Blazored.LocalStorage.TestExtensions/Blazored.LocalStorage.TestExtensions.csproj +++ b/src/Blazored.LocalStorage.TestExtensions/Blazored.LocalStorage.TestExtensions.csproj @@ -2,6 +2,7 @@ net6.0;net7.0 + enable Chris Sainty Chris Sainty diff --git a/src/Blazored.LocalStorage.TestExtensions/InMemoryStorageProvider.cs b/src/Blazored.LocalStorage.TestExtensions/InMemoryStorageProvider.cs index a09b688..12c2bc6 100644 --- a/src/Blazored.LocalStorage.TestExtensions/InMemoryStorageProvider.cs +++ b/src/Blazored.LocalStorage.TestExtensions/InMemoryStorageProvider.cs @@ -7,7 +7,7 @@ namespace Blazored.LocalStorage.TestExtensions { internal class InMemoryStorageProvider : IStorageProvider { - private readonly Dictionary _dataStore = new Dictionary(); + private readonly Dictionary _dataStore = new(); public void Clear() => _dataStore.Clear(); @@ -22,34 +22,31 @@ public bool ContainKey(string key) => _dataStore.ContainsKey(key); public ValueTask ContainKeyAsync(string key, CancellationToken cancellationToken = default) - => new ValueTask(ContainKey(key)); + => new(ContainKey(key)); - public string GetItem(string key) + public string? GetItem(string key) => _dataStore.ContainsKey(key) ? _dataStore[key] : default; - public ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default) - => new ValueTask(GetItem(key)); + public ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default) + => new(GetItem(key)); - public string Key(int index) + public string? Key(int index) => index > _dataStore.Count - 1 ? default : _dataStore.ElementAt(index).Key; - public ValueTask KeyAsync(int index, CancellationToken cancellationToken = default) - => new ValueTask(Key(index)); + public ValueTask KeyAsync(int index, CancellationToken cancellationToken = default) + => new(Key(index)); - public IEnumerable Keys() - { - return _dataStore.Keys.ToList(); - } - - public ValueTask> KeysAsync(CancellationToken cancellationToken = default) - => new ValueTask>(_dataStore.Keys.ToList()); + public IEnumerable Keys() + => _dataStore.Keys.ToList(); + public ValueTask> KeysAsync(CancellationToken cancellationToken = default) + => new(_dataStore.Keys.ToList()); public int Length() => _dataStore.Count; public ValueTask LengthAsync(CancellationToken cancellationToken = default) - => new ValueTask(Length()); + => new(Length()); public void RemoveItem(string key) => _dataStore.Remove(key); diff --git a/src/Blazored.LocalStorage/Blazored.LocalStorage.csproj b/src/Blazored.LocalStorage/Blazored.LocalStorage.csproj index 7dd4b2d..35c6f08 100644 --- a/src/Blazored.LocalStorage/Blazored.LocalStorage.csproj +++ b/src/Blazored.LocalStorage/Blazored.LocalStorage.csproj @@ -2,6 +2,7 @@ net6.0;net7.0;net8.0 + enable Chris Sainty Chris Sainty diff --git a/src/Blazored.LocalStorage/BrowserStorageProvider.cs b/src/Blazored.LocalStorage/BrowserStorageProvider.cs index f5ce6f0..0d98f9b 100644 --- a/src/Blazored.LocalStorage/BrowserStorageProvider.cs +++ b/src/Blazored.LocalStorage/BrowserStorageProvider.cs @@ -1,6 +1,7 @@ using Microsoft.JSInterop; using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Threading; using System.Threading.Tasks; using Blazored.LocalStorage.Exceptions; @@ -12,7 +13,7 @@ internal class BrowserStorageProvider : IStorageProvider private const string StorageNotAvailableMessage = "Unable to access the browser storage. This is most likely due to the browser settings."; private readonly IJSRuntime _jSRuntime; - private readonly IJSInProcessRuntime _jSInProcessRuntime; + private readonly IJSInProcessRuntime? _jSInProcessRuntime; public BrowserStorageProvider(IJSRuntime jSRuntime) { @@ -37,11 +38,11 @@ public async ValueTask ClearAsync(CancellationToken cancellationToken = default) } } - public async ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default) + public async ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default) { try { - return await _jSRuntime.InvokeAsync("localStorage.getItem", cancellationToken, key); + return await _jSRuntime.InvokeAsync("localStorage.getItem", cancellationToken, key); } catch (Exception exception) { @@ -54,11 +55,11 @@ public async ValueTask GetItemAsync(string key, CancellationToken cancel } } - public async ValueTask KeyAsync(int index, CancellationToken cancellationToken = default) + public async ValueTask KeyAsync(int index, CancellationToken cancellationToken = default) { try { - return await _jSRuntime.InvokeAsync("localStorage.key", cancellationToken, index); + return await _jSRuntime.InvokeAsync("localStorage.key", cancellationToken, index); } catch (Exception exception) { @@ -175,7 +176,7 @@ public async ValueTask RemoveItemsAsync(IEnumerable keys, CancellationTo throw; } } - + public void Clear() { CheckForInProcessRuntime(); @@ -341,6 +342,7 @@ public IEnumerable Keys() } } + [MemberNotNull(nameof(_jSInProcessRuntime))] private void CheckForInProcessRuntime() { if (_jSInProcessRuntime == null) diff --git a/src/Blazored.LocalStorage/ChangedEventArgs.cs b/src/Blazored.LocalStorage/ChangedEventArgs.cs index 1f55deb..85562f8 100644 --- a/src/Blazored.LocalStorage/ChangedEventArgs.cs +++ b/src/Blazored.LocalStorage/ChangedEventArgs.cs @@ -5,8 +5,8 @@ namespace Blazored.LocalStorage [ExcludeFromCodeCoverage] public class ChangedEventArgs { - public string Key { get; set; } - public object OldValue { get; set; } - public object NewValue { get; set; } + public string Key { get; set; } = null!; // Since .NET 6 is supported, `required` is not available yet + public object? OldValue { get; set; } + public object? NewValue { get; set; } } -} \ No newline at end of file +} diff --git a/src/Blazored.LocalStorage/ILocalStorageService.cs b/src/Blazored.LocalStorage/ILocalStorageService.cs index cbdd4c7..7f92b5b 100644 --- a/src/Blazored.LocalStorage/ILocalStorageService.cs +++ b/src/Blazored.LocalStorage/ILocalStorageService.cs @@ -22,7 +22,7 @@ public interface ILocalStorageService /// () from being applied. /// /// A representing the completion of the operation. - ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default); + ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default); /// /// Retrieve the specified data from local storage as a . @@ -33,7 +33,7 @@ public interface ILocalStorageService /// () from being applied. /// /// A representing the completion of the operation. - ValueTask GetItemAsStringAsync(string key, CancellationToken cancellationToken = default); + ValueTask GetItemAsStringAsync(string key, CancellationToken cancellationToken = default); /// /// Return the name of the key at the specified . @@ -44,7 +44,7 @@ public interface ILocalStorageService /// () from being applied. /// /// A representing the completion of the operation. - ValueTask KeyAsync(int index, CancellationToken cancellationToken = default); + ValueTask KeyAsync(int index, CancellationToken cancellationToken = default); /// /// Returns a collection of strings representing the names of the keys in the local storage. diff --git a/src/Blazored.LocalStorage/IStorageProvider.cs b/src/Blazored.LocalStorage/IStorageProvider.cs index d7c181a..cf69df9 100644 --- a/src/Blazored.LocalStorage/IStorageProvider.cs +++ b/src/Blazored.LocalStorage/IStorageProvider.cs @@ -10,10 +10,10 @@ internal interface IStorageProvider ValueTask ClearAsync(CancellationToken cancellationToken = default); bool ContainKey(string key); ValueTask ContainKeyAsync(string key, CancellationToken cancellationToken = default); - string GetItem(string key); - ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default); - string Key(int index); - ValueTask KeyAsync(int index, CancellationToken cancellationToken = default); + string? GetItem(string key); + ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default); + string? Key(int index); + ValueTask KeyAsync(int index, CancellationToken cancellationToken = default); IEnumerable Keys(); ValueTask> KeysAsync(CancellationToken cancellationToken = default); int Length(); diff --git a/src/Blazored.LocalStorage/ISyncLocalStorageService.cs b/src/Blazored.LocalStorage/ISyncLocalStorageService.cs index ac886bb..d29a653 100644 --- a/src/Blazored.LocalStorage/ISyncLocalStorageService.cs +++ b/src/Blazored.LocalStorage/ISyncLocalStorageService.cs @@ -15,21 +15,21 @@ public interface ISyncLocalStorageService /// /// A value specifying the name of the local storage slot to use /// The data from the specified as a - T GetItem(string key); + T? GetItem(string key); /// /// Retrieve the specified data from local storage as a . /// /// A value specifying the name of the storage slot to use /// The data associated with the specified as a - string GetItemAsString(string key); + string? GetItemAsString(string key); /// /// Return the name of the key at the specified . /// /// /// The name of the key at the specified - string Key(int index); + string? Key(int index); /// /// Checks if the exists in local storage, but does not check its value. diff --git a/src/Blazored.LocalStorage/LocalStorageService.cs b/src/Blazored.LocalStorage/LocalStorageService.cs index 4114374..c61cb35 100644 --- a/src/Blazored.LocalStorage/LocalStorageService.cs +++ b/src/Blazored.LocalStorage/LocalStorageService.cs @@ -52,7 +52,7 @@ public async ValueTask SetItemAsStringAsync(string key, string data, Cancellatio RaiseOnChanged(key, e.OldValue, data); } - public async ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default) + public async ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); @@ -74,7 +74,7 @@ public async ValueTask GetItemAsync(string key, CancellationToken cancella } } - public ValueTask GetItemAsStringAsync(string key, CancellationToken cancellationToken = default) + public ValueTask GetItemAsStringAsync(string key, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); @@ -96,7 +96,7 @@ public ValueTask ClearAsync(CancellationToken cancellationToken = default) public ValueTask LengthAsync(CancellationToken cancellationToken = default) => _storageProvider.LengthAsync(cancellationToken); - public ValueTask KeyAsync(int index, CancellationToken cancellationToken = default) + public ValueTask KeyAsync(int index, CancellationToken cancellationToken = default) => _storageProvider.KeyAsync(index, cancellationToken); public ValueTask> KeysAsync(CancellationToken cancellationToken = default) @@ -134,8 +134,7 @@ public void SetItemAsString(string key, string data) if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); - if (data is null) - throw new ArgumentNullException(nameof(data)); + ArgumentNullException.ThrowIfNull(data); var e = RaiseOnChangingSync(key, data); @@ -147,7 +146,7 @@ public void SetItemAsString(string key, string data) RaiseOnChanged(key, e.OldValue, data); } - public T GetItem(string key) + public T? GetItem(string key) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); @@ -169,7 +168,7 @@ public T GetItem(string key) } } - public string GetItemAsString(string key) + public string? GetItemAsString(string key) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); @@ -202,14 +201,14 @@ public void Clear() public int Length() => _storageProvider.Length(); - public string Key(int index) + public string? Key(int index) => _storageProvider.Key(index); public bool ContainKey(string key) => _storageProvider.ContainKey(key); - public event EventHandler Changing; - private async Task RaiseOnChangingAsync(string key, object data) + public event EventHandler? Changing; + private async Task RaiseOnChangingAsync(string key, object? data) { var e = new ChangingEventArgs { @@ -223,7 +222,7 @@ private async Task RaiseOnChangingAsync(string key, object da return e; } - private ChangingEventArgs RaiseOnChangingSync(string key, object data) + private ChangingEventArgs RaiseOnChangingSync(string key, object? data) { var e = new ChangingEventArgs { @@ -237,7 +236,7 @@ private ChangingEventArgs RaiseOnChangingSync(string key, object data) return e; } - private async Task GetItemInternalAsync(string key, CancellationToken cancellationToken = default) + private async Task GetItemInternalAsync(string key, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(key)) throw new ArgumentNullException(nameof(key)); @@ -256,7 +255,7 @@ private async Task GetItemInternalAsync(string key, CancellationToken canc } } - private object GetItemInternal(string key) + private object? GetItemInternal(string key) { if (string.IsNullOrEmpty(key)) throw new ArgumentNullException(nameof(key)); @@ -276,8 +275,8 @@ private object GetItemInternal(string key) } } - public event EventHandler Changed; - private void RaiseOnChanged(string key, object oldValue, object data) + public event EventHandler? Changed; + private void RaiseOnChanged(string key, object? oldValue, object? data) { var e = new ChangedEventArgs { diff --git a/src/Blazored.LocalStorage/Serialization/IJsonSerializer.cs b/src/Blazored.LocalStorage/Serialization/IJsonSerializer.cs index a1fdc9b..f1689ba 100644 --- a/src/Blazored.LocalStorage/Serialization/IJsonSerializer.cs +++ b/src/Blazored.LocalStorage/Serialization/IJsonSerializer.cs @@ -3,6 +3,6 @@ namespace Blazored.LocalStorage.Serialization public interface IJsonSerializer { string Serialize(T obj); - T Deserialize(string text); + T? Deserialize(string text); } } diff --git a/src/Blazored.LocalStorage/Serialization/SystemTextJsonSerializer.cs b/src/Blazored.LocalStorage/Serialization/SystemTextJsonSerializer.cs index 3bc7011..9db26e1 100644 --- a/src/Blazored.LocalStorage/Serialization/SystemTextJsonSerializer.cs +++ b/src/Blazored.LocalStorage/Serialization/SystemTextJsonSerializer.cs @@ -13,12 +13,12 @@ public SystemTextJsonSerializer(IOptions options) _options = options.Value.JsonSerializerOptions; } - public SystemTextJsonSerializer(LocalStorageOptions localStorageOptions) - { - _options = localStorageOptions.JsonSerializerOptions; + public SystemTextJsonSerializer(LocalStorageOptions localStorageOptions) + { + _options = localStorageOptions.JsonSerializerOptions; } - public T Deserialize(string data) + public T? Deserialize(string data) => JsonSerializer.Deserialize(data, _options); public string Serialize(T data) diff --git a/src/Blazored.LocalStorage/ServiceCollectionExtensions.cs b/src/Blazored.LocalStorage/ServiceCollectionExtensions.cs index e59eb97..60c0342 100644 --- a/src/Blazored.LocalStorage/ServiceCollectionExtensions.cs +++ b/src/Blazored.LocalStorage/ServiceCollectionExtensions.cs @@ -16,7 +16,7 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddBlazoredLocalStorage(this IServiceCollection services) => AddBlazoredLocalStorage(services, null); - public static IServiceCollection AddBlazoredLocalStorage(this IServiceCollection services, Action configure) + public static IServiceCollection AddBlazoredLocalStorage(this IServiceCollection services, Action? configure) { services.TryAddScoped(); services.TryAddScoped(); @@ -49,7 +49,7 @@ public static IServiceCollection AddBlazoredLocalStorageAsSingleton(this IServic /// /// /// - public static IServiceCollection AddBlazoredLocalStorageAsSingleton(this IServiceCollection services, Action configure) + public static IServiceCollection AddBlazoredLocalStorageAsSingleton(this IServiceCollection services, Action? configure) { services.TryAddSingleton(); services.TryAddSingleton();