NetcodeHub.Packages.Extensions.SessionStorage 1.0.0

dotnet add package NetcodeHub.Packages.Extensions.SessionStorage --version 1.0.0                
NuGet\Install-Package NetcodeHub.Packages.Extensions.SessionStorage -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NetcodeHub.Packages.Extensions.SessionStorage" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetcodeHub.Packages.Extensions.SessionStorage --version 1.0.0                
#r "nuget: NetcodeHub.Packages.Extensions.SessionStorage, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NetcodeHub.Packages.Extensions.SessionStorage as a Cake Addin
#addin nuget:?package=NetcodeHub.Packages.Extensions.SessionStorage&version=1.0.0

// Install NetcodeHub.Packages.Extensions.SessionStorage as a Cake Tool
#tool nuget:?package=NetcodeHub.Packages.Extensions.SessionStorage&version=1.0.0                

Install the package

    	*NetcodeHub.Packages.Extensions.SessionStorage*

Register package service

    	*builder.Services.AddNetcodeHubSessionStorageService();*

inject package and in the import.razor file

    	*@inject ISessionStorageService sessionStorageService*

Use the package in razor components

Without Encryption

    	*GetItemAsStringAsync(string key) : This interface gets and returns item from the browser's session storage in the form of string data type.*

    	*SaveAsStringAsync(string key, string content) : This interface sets or saves item in the form of string to the browser's session storage.*

    	*GetItemAsModelAsync<T>(string key) : This interface converts T Value being specified to string content before saving to browser's session storage.*

    	*DeleteItemAsync(string key) : This interface deletes or removes item from the session storage with the key provided.*

    	*AddItemToListAsync<T>(string key, T content) : This interface creates T List of items, add T Value (spcified) and save to browser's session storage.*
    	
    	*GetItemListAsync<T>(string key) : This interface returns T List of items specified from browser's session storage.*

    	*GetItemsCountAsync<T>(string key) : This interface returns the count of T List of item from the browser's session storage.*

With Encryption

    	*AddItemToEncryptedListAsync<T>(string key, T content) : This interface creates T List of items, add T Value (specified), encrypt and save to browser's session storage.*

    	*GetEncryptedItemAsStringAsync(string key) : This interface gets, decrypts and return item from the browser's session storage in the form of string data type.*

    	*GetEncryptedItemsCountAsync<T>(string key) : This interface decrypts and return the count of T List of item from the browser's session storage.*

    	*GetEncryptedItemListAsync<T>(string key) : This interface decrypts and return T List of items specified from browser's session storage.*

    	*GetEncryptedItemAsModelAsync<T>(string key) : This interface encrypts and convert T Value being specified to string content before saving to browser's session storage.*

    	*SaveEncryptedItemAsModelAsync<T>(string key, T content) : This interface encrypts and convert T Value being specified to string content before saving to browser's session storage.*

    	*SaveAsEncryptedStringAsync(string key, string content) : This interface encrypts and save item in the form of string to the browser's session storage.*

EXAMPLES

     @page "/"

          @rendermode InteractiveServer

          @using NetcodeHub.Packages.Extensions.LocalStorage

          @using NetcodeHub.Packages.Extensions.SessionStorage

          @inject ILocalStorageService localStorageService

          @inject ISessionStorageService sessionStorageService


         <h4>Set and Get String</h4>
         <h2>Content: @Content</h2>

          <button class="btn btn-primary m-2" @onclick="SetString">Set String </button>

         <button class="btn btn-primary m-2" @onclick="GetString">Get String </button> 

         <div class="m-2 p-3">
             Id: @entity.Id <br />
             Name: @entity.Name  <br />
             Email: @entity.Email <br />
         </div>

        <button class="btn btn-primary m-2" @onclick="SetModel">Set Model </button>

       <button class="btn btn-primary m-2" @onclick="GetModel">Get Model </button>  

      <div class="m-2 p-2">

           @if (Entities != null)
           {
              @foreach (var i in Entities)
              {
                  <div class="m-2 p-3">
                      Id: @i.Id <br />
                      Name: @i.Name  <br />
                      Email: @i.Email <br />
                  </div>
              }
          }

      </div>

         <button class="btn btn-primary m-2" @onclick="SetListModel">Set List Model </button>
         <button class="btn btn-primary m-2" @onclick="GetListModel">Get List Model </button>
     

          Count: [@Count]
         <button class="btn btn-primary m-2" @onclick="GetCount">Get Count</button>
         <button class="btn btn-primary m-2" @onclick="AddItemToLocal">Add Item To Local</button>

Local Storage // Session Storage

    @code {
     string? Content;

Get and Set String as Unprotected

        async Task SetString() => await localStorageService.SaveAsEncryptedStringAsync("key", "Netcode-Hub");
        async Task GetString() => Content = (await localStorageService.GetEncryptedItemAsStringAsync("key"));

        async Task SetString() => await sessionStorageService.SaveAsEncryptedStringAsync("key", "Netcode-Hub");
        async Task GetString() => Content = (await sessionStorageService.GetEncryptedItemAsStringAsync("key"));

Get and Set String as Protected

        async Task SetString() => await localStorageService.SaveAsEncryptedStringAsync("key", "Netcode-Hub");
        async Task GetString() => Content = (await localStorageService.GetEncryptedItemAsStringAsync("key"));

        async Task SetString() => await sessionStorageService.SaveAsEncryptedStringAsync("key", "Netcode-Hub");
        async Task GetString() => Content = (await sessionStorageService.GetEncryptedItemAsStringAsync("key"));

Get and Set Model as Unprotected string

        Entity entity = new();
        Entity entity1 = new();
      async Task SetModel()
        {

            entity1.Id = 1;
            entity1.Name = "Netcode-Hub";
            entity1.Email = "NetcodeHub@gmail.com";
            await localStorageService.SaveItemAsModelAsync<Entity>("key", entity1);
        }
        async Task GetModel() => entity = await localStorageService.GetItemAsModelAsync<Entity>("key");

        async Task SetModel()
        {

            entity1.Id = 1;
            entity1.Name = "Netcode-Hub";
            entity1.Email = "NetcodeHub@gmail.com";
            await sessionStorageService.SaveItemAsModelAsync<Entity>("key", entity1);
        }
        async Task GetModel() => entity = await sessionStorageService.GetItemAsModelAsync<Entity>("key");

Get and Set Model as Protected string

       async Task SetModel()
        {

            entity1.Id = 1;
            entity1.Name = "Netcode-Hub";
            entity1.Email = "NetcodeHub@gmail.com";
            await localStorageService.SaveEncryptedItemAsModelAsync<Entity>("key", entity1);
        }
        async Task GetModel() => entity = await localStorageService.GetEncryptedItemAsModelAsync<Entity>("key");


        async Task SetModel()
        {

            entity1.Id = 1;
            entity1.Name = "Netcode-Hub";
            entity1.Email = "NetcodeHub@gmail.com";
            await sessionStorageService.SaveEncryptedItemAsModelAsync<Entity>("key", entity1);
        }
        async Task GetModel() => entity = await sessionStorageService.GetEncryptedItemAsModelAsync<Entity>("key");



        

Get and Set List<T> as Unprotected

     List<Entity> Entities = [];
      async Task SetListModel()
        {

            entity1.Id = 1;
            entity1.Name = "Netcode-Hub";
            entity1.Email = "NetcodeHub@gmail.com";
            await localStorageService.AddItemToListAsync<Entity>("key", entity1);
        }
        async Task GetListModel() => Entities = await localStorageService.GetItemListAsync<Entity>("key");

        async Task SetListModel()
        {

            entity1.Id = 1;
            entity1.Name = "Netcode-Hub";
            entity1.Email = "NetcodeHub@gmail.com";
            await sessionStorageService.AddItemToListAsync<Entity>("key", entity1);
        }
        async Task GetListModel() => Entities = await sessionStorageService.GetItemListAsync<Entity>("key");

Get and Set List<T> as Unprotected

    List<Entity> Entities = [];
    async Task SetListModel()
        {

            entity1.Id = 1;
            entity1.Name = "Netcode-Hub";
            entity1.Email = "NetcodeHub@gmail.com";
            await localStorageService.AddItemToEncryptedListAsync<Entity>("key", entity1);
        }
        async Task GetListModel() => Entities = await localStorageService.GetEncryptedItemListAsync<Entity>("key");

        async Task SetListModel()
        {

            entity1.Id = 1;
            entity1.Name = "Netcode-Hub";
            entity1.Email = "NetcodeHub@gmail.com";
            await sessionStorageService.AddItemToEncryptedListAsync<Entity>("key", entity1);
        }

        async Task GetListModel() => Entities = await sessionStorageService.GetEncryptedItemListAsync<Entity>("key");

        async Task AddItemToLocal()
        {
            entity1.Id = 1;
            entity1.Name = "Netcode-Hub";
            entity1.Email = "NetcodeHub@gmail.com";
            await localStorageService.AddItemToListAsync<Entity>("key", entity1);
        }

        int Count;
        async Task GetCount() => Count = await localStorageService.GetItemsCountAsync<Entity>("key");
        async Task GetEncryptedCount() => Count = await localStorageService.GetEncryptedItemsCountAsync<Entity>("key");
        async Task GetSessionCount() => Count = await sessionStorageService.GetItemsCountAsync<Entity>("key");
        async Task DeleteLocalItem() => await localStorageService.DeleteItemAsync("key");
        async Task ClearLocalStorage() => await localStorageService.ClearAllItemsAsync();
        async Task DeleteSessionItem() => await sessionStorageService.DeleteItemAsync("key");
        async Task ClearSessionStorage() => await sessionStorageService.ClearAllItemsAsync();
        public class Entity
        {
            public int Id { get; set; }
            public string? Name { get; set; }
            public string? Email { get; set; }
        }
    }
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 364 3/17/2024

This package is of its first kind, your suggestions  may help improve the package