Skip to content

Commit d69ee73

Browse files
committed
Updated features to match local storage package
1 parent 4895db8 commit d69ee73

39 files changed

Lines changed: 2486 additions & 1935 deletions

‎Directory.Build.props‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<Project>
33

44
<PropertyGroup Label="Package Versions">
5-
<DotNet3Version>3.1.14</DotNet3Version>
6-
<DotNet5Version>5.0.5</DotNet5Version>
5+
<DotNet6Version>6.0.12</DotNet6Version>
6+
<DotNet7Version>7.0.1</DotNet7Version>
77
</PropertyGroup>
88

99
</Project>

‎README.md‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ builder.Services.AddBlazoredSessionStorage(config => {
2525
```
2626

2727
### SetItem[Async] method now serializes string values
28-
Prior to v2 we bypassed the serialization of string values as it seemed a pointless as string can be stored directly. However, this led to some edge cases where nullable strings were being saved as the string `"null"`. Then when retrieved, instead of being null the value was `"null"`. By serializing strings this issue is taken care of.
28+
Prior to v2 we bypassed the serialization of string values as it seemed pointless as string can be stored directly. However, this led to some edge cases where nullable strings were being saved as the string `"null"`. Then when retrieved, instead of being null the value was `"null"`. By serializing strings this issue is taken care of.
2929
For those who wish to save raw string values, a new method `SetValueAsString[Async]` is available. This will save a string value without attempting to serialize it and will throw an exception if a null string is attempted to be saved.
3030

3131
## Installing
@@ -42,7 +42,7 @@ You can also install via the .NET CLI with the following command:
4242
dotnet add package Blazored.SessionStorage
4343
```
4444

45-
If you're using Visual Studio you can also install via the built in NuGet package manager.
45+
If you're using Jetbrains Rider or Visual Studio you can also install via the built in NuGet package manager.
4646

4747
## Setup
4848

@@ -69,6 +69,17 @@ public static async Task Main(string[] args)
6969
}
7070
```
7171

72+
### Registering services as Singleton - Blazor WebAssembly **ONLY**
73+
99% of developers will want to register Blazored SessionStorage using the method described above. However, in some very specific scenarios,
74+
developers may have a need to register services as Singleton as apposed to Scoped. This is possible by using the following method:
75+
76+
```csharp
77+
builder.Services.AddBlazoredSessionStorageAsSingleton();
78+
```
79+
80+
**This method will not work with Blazor Server applications as Blazor's JS interop services are registered as Scoped and cannot be injected into Singletons.**
81+
82+
7283
## Usage (Blazor WebAssembly)
7384
To use Blazored.SessionStorage in Blazor WebAssembly, inject the `ISessionStorageService` per the example below.
7485

@@ -128,9 +139,11 @@ The APIs available are:
128139
- GetItemAsync()
129140
- GetItemAsStringAsync()
130141
- RemoveItemAsync()
142+
- RemoveItemsAsync()
131143
- ClearAsync()
132144
- LengthAsync()
133145
- KeyAsync()
146+
- KeysAsync()
134147
- ContainKeyAsync()
135148

136149
- synchronous via `ISyncSessionStorageService` (Synchronous methods are **only** available in Blazor WebAssembly):
@@ -139,9 +152,11 @@ The APIs available are:
139152
- GetItem()
140153
- GetItemAsString()
141154
- RemoveItem()
155+
- RemovesItem()
142156
- Clear()
143157
- Length()
144158
- Key()
159+
- Keys()
145160
- ContainKey()
146161

147162
**Note:** Blazored.SessionStorage methods will handle the serialisation and de-serialisation of the data for you, the exception is the `GetItemAsString[Async]` method which will return the raw string value from session storage.
@@ -178,6 +193,24 @@ You can find an example of this in the Blazor Server sample project. The standar
178193
## Testing with bUnit
179194
This library provides test extensions for use with the [bUnit testing library](https://bunit.dev/). Using these test extensions will provide an in memory implementation which mimics session storage allowing more realistic testing of your components.
180195

196+
### Installing
197+
198+
To install the package add the following line to you csproj file replacing x.x.x with the latest version number (found at the top of this file):
199+
200+
```
201+
<PackageReference Include="Blazored.SessionStorage.TestExtensions" Version="x.x.x" />
202+
```
203+
204+
You can also install via the .NET CLI with the following command:
205+
206+
```
207+
dotnet add package Blazored.SessionStorage.TestExtensions
208+
```
209+
210+
If you're using Jetbrains Rider or Visual Studio you can also install via the built in NuGet package manager.
211+
212+
### Usage example
213+
181214
Below is an example test which uses these extensions. You can find an example project which shows this code in action in the samples folder.
182215

183216
```c#

‎src/Blazored.SessionStorage.TestExtensions/Blazored.SessionStorage.TestExtensions.csproj‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.1;net7.0</TargetFrameworks>
4+
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
55

66
<Authors>Chris Sainty</Authors>
77
<Company></Company>
@@ -26,12 +26,16 @@
2626
<AssemblyOriginatorKeyFile>Blazored.SessionStorage.snk</AssemblyOriginatorKeyFile>
2727
</PropertyGroup>
2828

29+
<ItemGroup>
30+
<SupportedPlatform Include="browser" />
31+
</ItemGroup>
32+
2933
<ItemGroup>
3034
<None Include="..\Blazored.SessionStorage\icon.png" Pack="true" PackagePath="\" />
3135
</ItemGroup>
3236

3337
<ItemGroup>
34-
<PackageReference Include="bunit.core" Version="1.12.6" />
38+
<PackageReference Include="bunit.core" Version="1.13.5" />
3539
</ItemGroup>
3640

3741
<ItemGroup>

‎src/Blazored.SessionStorage.TestExtensions/InMemoryStorageProvider.cs‎

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ namespace Blazored.SessionStorage.TestExtensions
77
{
88
internal class InMemoryStorageProvider : IStorageProvider
99
{
10-
private readonly Dictionary<string, string> _dataStore = new Dictionary<string, string>();
10+
private readonly Dictionary<string, string> _dataStore = new();
1111

1212
public void Clear()
1313
=> _dataStore.Clear();
1414

15-
public ValueTask ClearAsync(CancellationToken? cancellationToken = null)
15+
public ValueTask ClearAsync(CancellationToken cancellationToken = default)
1616
{
1717
_dataStore.Clear();
1818
return new ValueTask(Task.CompletedTask);
@@ -21,35 +21,56 @@ public ValueTask ClearAsync(CancellationToken? cancellationToken = null)
2121
public bool ContainKey(string key)
2222
=> _dataStore.ContainsKey(key);
2323

24-
public ValueTask<bool> ContainKeyAsync(string key, CancellationToken? cancellationToken = null)
25-
=> new ValueTask<bool>(ContainKey(key));
24+
public ValueTask<bool> ContainKeyAsync(string key, CancellationToken cancellationToken = default)
25+
=> new(ContainKey(key));
2626

2727
public string GetItem(string key)
28-
=> _dataStore.ContainsKey(key) ? _dataStore[key] : default;
28+
=> _dataStore.TryGetValue(key, out var value) ? value : default;
2929

30-
public ValueTask<string> GetItemAsync(string key, CancellationToken? cancellationToken = null)
31-
=> new ValueTask<string>(GetItem(key));
30+
public ValueTask<string> GetItemAsync(string key, CancellationToken cancellationToken = default)
31+
=> new(GetItem(key));
3232

3333
public string Key(int index)
3434
=> index > _dataStore.Count - 1 ? default : _dataStore.ElementAt(index).Key;
3535

36-
public ValueTask<string> KeyAsync(int index, CancellationToken? cancellationToken = null)
37-
=> new ValueTask<string>(Key(index));
36+
public IEnumerable<string> Keys()
37+
=> _dataStore.Keys.ToList();
38+
39+
public ValueTask<string> KeyAsync(int index, CancellationToken cancellationToken = default)
40+
=> new(Key(index));
41+
42+
public ValueTask<IEnumerable<string>> KeysAsync(CancellationToken cancellationToken = default)
43+
=> new(_dataStore.Keys.ToList());
3844

3945
public int Length()
4046
=> _dataStore.Count;
4147

42-
public ValueTask<int> LengthAsync(CancellationToken? cancellationToken = null)
43-
=> new ValueTask<int>(Length());
48+
public ValueTask<int> LengthAsync(CancellationToken cancellationToken = default)
49+
=> new(Length());
4450

4551
public void RemoveItem(string key)
4652
=> _dataStore.Remove(key);
4753

48-
public ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToken = null)
54+
public ValueTask RemoveItemAsync(string key, CancellationToken cancellationToken = default)
4955
{
5056
RemoveItem(key);
5157
return new ValueTask(Task.CompletedTask);
5258
}
59+
60+
public void RemoveItems(IEnumerable<string> keys)
61+
{
62+
foreach (var key in keys)
63+
{
64+
_dataStore.Remove(key);
65+
}
66+
}
67+
68+
public ValueTask RemoveItemsAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default)
69+
{
70+
RemoveItems(keys);
71+
72+
return new ValueTask(Task.CompletedTask);
73+
}
5374

5475
public void SetItem(string key, string data)
5576
{
@@ -63,7 +84,7 @@ public void SetItem(string key, string data)
6384
}
6485
}
6586

66-
public ValueTask SetItemAsync(string key, string data, CancellationToken? cancellationToken = null)
87+
public ValueTask SetItemAsync(string key, string data, CancellationToken cancellationToken = default)
6788
{
6889
SetItem(key, data);
6990
return new ValueTask(Task.CompletedTask);

‎src/Blazored.SessionStorage/Blazored.SessionStorage.csproj‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.1;net7.0</TargetFrameworks>
4+
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
55

66
<Authors>Chris Sainty</Authors>
77
<Company></Company>
@@ -26,20 +26,16 @@
2626
<AssemblyOriginatorKeyFile>Blazored.SessionStorage.snk</AssemblyOriginatorKeyFile>
2727
</PropertyGroup>
2828

29-
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.1' OR '$(TargetFramework)' == 'netcoreapp3.1'">
30-
<RazorLangVersion>3.0</RazorLangVersion>
31-
</PropertyGroup>
32-
33-
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
29+
<ItemGroup>
3430
<SupportedPlatform Include="browser" />
3531
</ItemGroup>
3632

37-
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1' OR '$(TargetFramework)' == 'netcoreapp3.1'">
38-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.31" />
33+
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
34+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="$(DotNet6Version)" />
3935
</ItemGroup>
40-
36+
4137
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
42-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="7.0.0" />
38+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="$(DotNet7Version)" />
4339
</ItemGroup>
4440

4541
<ItemGroup>
@@ -62,4 +58,4 @@
6258
</AssemblyAttribute>
6359
</ItemGroup>
6460

65-
</Project>
61+
</Project>

0 commit comments

Comments
 (0)