Skip to content

Commit 24d76b9

Browse files
celluj34galvesribeiro
authored andcommitted
Use common base class for Local and Session storage implementations (#48)
* introduce new Storage base class * update LocalStorage and SessionStorage to use new base class * move the method constants to inside the Storage base class
1 parent 152b3b0 commit 24d76b9

File tree

4 files changed

+62
-84
lines changed

4 files changed

+62
-84
lines changed

src/Blazor.Extensions.Storage/Constants.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
namespace Blazor.Extensions
22
{
3-
internal class MethodNames
4-
{
5-
public const string LENGTH_METHOD = "BlazorExtensions.Storage.Length";
6-
public const string KEY_METHOD = "BlazorExtensions.Storage.Key";
7-
public const string GET_ITEM_METHOD = "BlazorExtensions.Storage.GetItem";
8-
public const string SET_ITEM_METHOD = "BlazorExtensions.Storage.SetItem";
9-
public const string REMOVE_ITEM_METHOD = "BlazorExtensions.Storage.RemoveItem";
10-
public const string CLEAR_METHOD = "BlazorExtensions.Storage.Clear";
11-
}
12-
133
internal class StorageTypeNames
144
{
155
public const string SESSION_STORAGE = "sessionStorage";
Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,10 @@
11
using Blazor.Extensions.Storage.Interfaces;
22
using Microsoft.JSInterop;
3-
using System;
4-
using System.Threading.Tasks;
53

64
namespace Blazor.Extensions.Storage
75
{
8-
internal class LocalStorage : ILocalStorage
6+
internal class LocalStorage : Storage, ILocalStorage
97
{
10-
private readonly IJSRuntime runtime;
11-
12-
public ValueTask<int> Length() => this.runtime.InvokeAsync<int>(MethodNames.LENGTH_METHOD, StorageTypeNames.LOCAL_STORAGE);
13-
14-
public ValueTask Clear() => this.runtime.InvokeVoidAsync(MethodNames.CLEAR_METHOD, StorageTypeNames.LOCAL_STORAGE);
15-
16-
public LocalStorage(IJSRuntime runtime)
17-
{
18-
this.runtime = runtime;
19-
}
20-
21-
public ValueTask<TItem> GetItem<TItem>(string key)
22-
{
23-
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
24-
25-
return this.runtime.InvokeAsync<TItem>(MethodNames.GET_ITEM_METHOD, StorageTypeNames.LOCAL_STORAGE, key);
26-
}
27-
28-
public ValueTask<string> Key(int index) => this.runtime.InvokeAsync<string>(MethodNames.KEY_METHOD, StorageTypeNames.LOCAL_STORAGE, index);
29-
30-
public ValueTask RemoveItem(string key)
31-
{
32-
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
33-
34-
return this.runtime.InvokeVoidAsync(MethodNames.REMOVE_ITEM_METHOD, StorageTypeNames.LOCAL_STORAGE, key);
35-
}
36-
37-
public ValueTask SetItem<TItem>(string key, TItem item)
38-
{
39-
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
40-
41-
return this.runtime.InvokeVoidAsync(MethodNames.SET_ITEM_METHOD, StorageTypeNames.LOCAL_STORAGE, key, item);
42-
}
8+
public LocalStorage(IJSRuntime runtime) : base(runtime, StorageTypeNames.LOCAL_STORAGE) {}
439
}
44-
}
10+
}
Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,10 @@
11
using Blazor.Extensions.Storage.Interfaces;
22
using Microsoft.JSInterop;
3-
using System;
4-
using System.Threading.Tasks;
53

64
namespace Blazor.Extensions.Storage
75
{
8-
internal class SessionStorage : ISessionStorage
6+
internal class SessionStorage : Storage, ISessionStorage
97
{
10-
private readonly IJSRuntime runtime;
11-
12-
public ValueTask<int> Length() => this.runtime.InvokeAsync<int>(MethodNames.LENGTH_METHOD, StorageTypeNames.SESSION_STORAGE);
13-
14-
public ValueTask Clear() => this.runtime.InvokeVoidAsync(MethodNames.CLEAR_METHOD, StorageTypeNames.SESSION_STORAGE);
15-
16-
public SessionStorage(IJSRuntime runtime)
17-
{
18-
this.runtime = runtime;
19-
}
20-
21-
public ValueTask<TItem> GetItem<TItem>(string key)
22-
{
23-
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
24-
25-
return this.runtime.InvokeAsync<TItem>(MethodNames.GET_ITEM_METHOD, StorageTypeNames.SESSION_STORAGE, key);
26-
}
27-
28-
public ValueTask<string> Key(int index) => this.runtime.InvokeAsync<string>(MethodNames.KEY_METHOD, StorageTypeNames.SESSION_STORAGE, index);
29-
30-
public ValueTask RemoveItem(string key)
31-
{
32-
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
33-
34-
return this.runtime.InvokeVoidAsync(MethodNames.REMOVE_ITEM_METHOD, StorageTypeNames.SESSION_STORAGE, key);
35-
}
36-
37-
public ValueTask SetItem<TItem>(string key, TItem item)
38-
{
39-
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
40-
41-
return this.runtime.InvokeVoidAsync(MethodNames.SET_ITEM_METHOD, StorageTypeNames.SESSION_STORAGE, key, item);
42-
}
8+
public SessionStorage(IJSRuntime runtime) : base(runtime, StorageTypeNames.SESSION_STORAGE) {}
439
}
44-
}
10+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Blazor.Extensions.Storage.Interfaces;
4+
using Microsoft.JSInterop;
5+
6+
namespace Blazor.Extensions.Storage
7+
{
8+
internal abstract class Storage : IStorage
9+
{
10+
private readonly IJSRuntime runtime;
11+
private readonly string storageName;
12+
13+
protected Storage(IJSRuntime runtime, string storageName)
14+
{
15+
this.runtime = runtime;
16+
this.storageName = storageName;
17+
}
18+
19+
public ValueTask<int> Length() => this.runtime.InvokeAsync<int>(MethodNames.LENGTH_METHOD, this.storageName);
20+
21+
public ValueTask Clear() => this.runtime.InvokeVoidAsync(MethodNames.CLEAR_METHOD, this.storageName);
22+
23+
public ValueTask<TItem> GetItem<TItem>(string key)
24+
{
25+
if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
26+
27+
return this.runtime.InvokeAsync<TItem>(MethodNames.GET_ITEM_METHOD, this.storageName, key);
28+
}
29+
30+
public ValueTask<string> Key(int index) => this.runtime.InvokeAsync<string>(MethodNames.KEY_METHOD, this.storageName, index);
31+
32+
public ValueTask RemoveItem(string key)
33+
{
34+
if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
35+
36+
return this.runtime.InvokeVoidAsync(MethodNames.REMOVE_ITEM_METHOD, this.storageName, key);
37+
}
38+
39+
public ValueTask SetItem<TItem>(string key, TItem item)
40+
{
41+
if(string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
42+
43+
return this.runtime.InvokeVoidAsync(MethodNames.SET_ITEM_METHOD, this.storageName, key, item);
44+
}
45+
46+
private class MethodNames
47+
{
48+
public const string LENGTH_METHOD = "BlazorExtensions.Storage.Length";
49+
public const string KEY_METHOD = "BlazorExtensions.Storage.Key";
50+
public const string GET_ITEM_METHOD = "BlazorExtensions.Storage.GetItem";
51+
public const string SET_ITEM_METHOD = "BlazorExtensions.Storage.SetItem";
52+
public const string REMOVE_ITEM_METHOD = "BlazorExtensions.Storage.RemoveItem";
53+
public const string CLEAR_METHOD = "BlazorExtensions.Storage.Clear";
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)