Blazor Browser History–part 03–implementation

01 Introduction

Because I said that I have 2 implementations for same sqlite database (SqliteWasmBlazor and BitBeSql), there will be interfaces .

The first one is to save data and retrieve data – a CRUD

02 Interface IBrowserUserHistoryRepositoryDatabase

Description: Base interface defining database operations for browser history. This is the core abstraction for data persistence.

Members:

  • Task Save(params BrowserUserHistoryData[] historyData) – Saves history records to database
  • Task<BrowserVisits[]> Retrieve(DateTime date) – Retrieves and consolidates visits for a specific date
  • Task<DateOnly[]> RetrieveLastDates(int nrDates) – Retrieves the last N distinct dates with history data
  • string NameProvider() – Returns the name/identifier of the database provider

03 Interface IBrowserUserHistoryRepository

Description: It retrieves data for the GUI . Also inherits from IBrowserUserHistoryRepositoryDatabase – the class that will implement will be composing by DI.

Members:

  • int MaxMemoryDataBeforeSave { get; set; } – Controls when in-memory data is persisted
  • Task AddToMemory(BrowserUserHistoryData historyData) – Adds history data to memory cache
  • BrowserUserHistoryData[] DebugData() – Returns current in-memory data for debugging
  • BrowserVisits[] FromMemory() – Retrieves consolidated visits from memory
  • Task SaveMemory() – Persists all in-memory data to database

Implementation

There will be 2 nuget packages – and each one will implement the DI

builder.Services.SqliteWasmBlazor_AddDependencies("HistorySqliteWasmBlazor.db");

or

 builder.Services.BitBesql_AddDependencies("HistoryBitBesql.db");

That will call the EFCore database

 Services.AddBesqlDbContextFactory<BBHContextSqlite_BitBesql>(optionsAction: options =>
 {
     options.UseSqlite($"Data Source={name}");
 }
 );
 Services.AddSingleton<IBrowserUserHistoryRepositoryDatabase, SqliteDatabase_BitBesql>();

or

    Services.AddDbContextFactory<BBHContextSqlite_SqliteWasmBlazor>(options =>
    {
        var connection = new SqliteWasmConnection($"Data Source={name}");
        options.UseSqliteWasm(connection);
    });
    Services.AddSingleton<IBrowserUserHistoryRepositoryDatabase, SqliteDatabase_SqliteWasmBlazor>();
    Services.AddSingleton<IDBInitializationService, DBInitializationService>();

This can be explained better by this interface hierarchy ( keep in mind that IBrowserUserHistoryRepositoryDatabase )

04 Hierarchy


IBrowserUserHistoryRepositoryDatabase (Base Interface)
    ↑                           ↑  
    │ (Inherited by)            │ (Implemented by)
    │                           ├─ SqliteDatabase_SqliteWasmBlazor  
    │                           └─ SqliteDatabase_BitBesql  
    │                           │
IBrowserUserHistoryRepository (Extended Interface)
    ↑
    │ (Implemented by)
    │
BrowserUserHistoryRepository (Main Implementation)
    │
    └─── Uses (Composition)
         ├─ IBrowserUserHistoryRepositoryDatabase (injected as databaseOps)
         │   ├─ SqliteDatabase_SqliteWasmBlazor
         │   └─ SqliteDatabase_BitBesql

The BrowserUserHistoryRepository has this call

builder.Services.AddSingleton<IBrowserUserHistoryRepository, BrowserUserHistoryRepository>();

and the constructor is

 public BrowserUserHistoryRepository(IBrowserUserHistoryRepositoryDatabase databaseOps)

in order to profit from each implementation

Demo at https://ignatandrei.github.io/BlazorBrowserHistory/

Nuget Packages at https://www.nuget.org/packages?q=BBH+owner%3Aignatandrei&includeComputedFrameworks=true&prerel=false&sortby=relevance


Posted

in

, , ,

by

Tags: