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 databaseTask<BrowserVisits[]> Retrieve(DateTime date)– Retrieves and consolidates visits for a specific dateTask<DateOnly[]> RetrieveLastDates(int nrDates)– Retrieves the last N distinct dates with history datastring 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 persistedTask AddToMemory(BrowserUserHistoryData historyData)– Adds history data to memory cacheBrowserUserHistoryData[] DebugData()– Returns current in-memory data for debuggingBrowserVisits[] FromMemory()– Retrieves consolidated visits from memoryTask 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