Skip to content

Commit 12e1212

Browse files
phanronaldgalvesribeiro
authored andcommitted
added interfaces and seperate classes (#6)
1 parent e3c842b commit 12e1212

File tree

3 files changed

+55
-31
lines changed

3 files changed

+55
-31
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Blazor.Extensions.Storage.Interfaces
6+
{
7+
public interface IStorage
8+
{
9+
TItem GetItem<TItem>(string key);
10+
string Key(int index);
11+
void RemoveItem(string key);
12+
void SetItem<TItem>(string key, TItem item);
13+
void Clear();
14+
int Length { get; }
15+
}
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Blazor.Extensions.Storage.Interfaces;
2+
using Microsoft.AspNetCore.Blazor;
3+
using Microsoft.AspNetCore.Blazor.Browser.Interop;
4+
using System;
5+
6+
namespace Blazor.Extensions.Storage
7+
{
8+
public class LocalStorage : IStorage
9+
{
10+
public int Length => RegisteredFunction.Invoke<int>(MethodNames.LENGTH_METHOD, StorageTypeNames.LOCAL_STORAGE);
11+
public void Clear() => RegisteredFunction.InvokeUnmarshalled<object>(MethodNames.CLEAR_METHOD, StorageTypeNames.LOCAL_STORAGE);
12+
13+
public TItem GetItem<TItem>(string key)
14+
{
15+
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
16+
17+
return RegisteredFunction.Invoke<TItem>(MethodNames.GET_ITEM_METHOD, StorageTypeNames.LOCAL_STORAGE, key);
18+
}
19+
20+
public string Key(int index) => RegisteredFunction.Invoke<string>(MethodNames.KEY_METHOD, StorageTypeNames.LOCAL_STORAGE, index);
21+
22+
public void RemoveItem(string key)
23+
{
24+
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
25+
26+
RegisteredFunction.InvokeUnmarshalled<object>(MethodNames.REMOVE_ITEM_METHOD, StorageTypeNames.LOCAL_STORAGE, key);
27+
}
28+
29+
public void SetItem<TItem>(string key, TItem item)
30+
{
31+
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
32+
33+
RegisteredFunction.Invoke<object>(MethodNames.SET_ITEM_METHOD, StorageTypeNames.LOCAL_STORAGE, key, JsonUtil.Serialize(item));
34+
}
35+
}
36+
}
Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
using Blazor.Extensions.Storage.Interfaces;
12
using Microsoft.AspNetCore.Blazor;
23
using Microsoft.AspNetCore.Blazor.Browser.Interop;
34
using System;
45

5-
namespace Blazor.Extensions
6+
namespace Blazor.Extensions.Storage
67
{
7-
public class SessionStorage
8+
public class SessionStorage : IStorage
89
{
910
public int Length => RegisteredFunction.Invoke<int>(MethodNames.LENGTH_METHOD, StorageTypeNames.SESSION_STORAGE);
1011
public void Clear() => RegisteredFunction.InvokeUnmarshalled<object>(MethodNames.CLEAR_METHOD, StorageTypeNames.SESSION_STORAGE);
@@ -32,33 +33,4 @@ public void SetItem<TItem>(string key, TItem item)
3233
RegisteredFunction.Invoke<object>(MethodNames.SET_ITEM_METHOD, StorageTypeNames.SESSION_STORAGE, key, JsonUtil.Serialize(item));
3334
}
3435
}
35-
36-
public class LocalStorage
37-
{
38-
public int Length => RegisteredFunction.Invoke<int>(MethodNames.LENGTH_METHOD, StorageTypeNames.LOCAL_STORAGE);
39-
public void Clear() => RegisteredFunction.InvokeUnmarshalled<object>(MethodNames.CLEAR_METHOD, StorageTypeNames.LOCAL_STORAGE);
40-
41-
public TItem GetItem<TItem>(string key)
42-
{
43-
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
44-
45-
return RegisteredFunction.Invoke<TItem>(MethodNames.GET_ITEM_METHOD, StorageTypeNames.LOCAL_STORAGE, key);
46-
}
47-
48-
public string Key(int index) => RegisteredFunction.Invoke<string>(MethodNames.KEY_METHOD, StorageTypeNames.LOCAL_STORAGE, index);
49-
50-
public void RemoveItem(string key)
51-
{
52-
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
53-
54-
RegisteredFunction.InvokeUnmarshalled<object>(MethodNames.REMOVE_ITEM_METHOD, StorageTypeNames.LOCAL_STORAGE, key);
55-
}
56-
57-
public void SetItem<TItem>(string key, TItem item)
58-
{
59-
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
60-
61-
RegisteredFunction.Invoke<object>(MethodNames.SET_ITEM_METHOD, StorageTypeNames.LOCAL_STORAGE, key, JsonUtil.Serialize(item));
62-
}
63-
}
6436
}

0 commit comments

Comments
 (0)