Storage in H.Necessaire: Services, Filters and Providers
The storage contracts, how to query with filters and paging, and how to pick a provider.
Applies to H.Necessaire 4.0.5.
Persistence in H.Necessaire is defined by contracts in the core package and implemented by interchangeable providers. Your code depends on the contracts; you choose SQLite, SQL Server, RavenDB, Cosmos DB or Firestore by registering one dependency group.
Install
dotnet add package H.NecessaireThe contracts
interface ImAStorageService<TId, TEntity> where TEntity : IDentityType<TId>
{
Task<OperationResult> Save(TEntity entity);
Task<OperationResult<TEntity>> LoadByID(TId id);
Task<OperationResult<TEntity>[]> LoadByIDs(params TId[] ids);
Task<OperationResult> DeleteByID(TId id);
Task<OperationResult<TId>[]> DeleteByIDs(params TId[] ids);
}
interface ImAStorageBrowserService<TEntity, TFilter> where TFilter : IPageFilter, ISortFilter
{
Task<OperationResult<Page<TEntity>>> LoadPage(TFilter filter);
Task<OperationResult<IDisposableEnumerable<TEntity>>> Stream(TFilter filter);
Task<OperationResult<IDisposableEnumerable<TEntity>>> StreamAll();
}Everything returns an OperationResult, so a storage failure is a value, not a surprise exception. Entities implement IGuidIdentity or IStringIdentity (both IDentityType<T>), which just means they have an ID.
Using a service
The QD action daemon is a good real-world example of the pattern:
var storage = deps.Get<ImAStorageService<Guid, QdAction>>();
var browser = deps.Get<ImAStorageBrowserService<QdAction, QdActionFilter>>();
await storage.Save(QdAction.New("Ping", "{}"));
QdAction[] queued = (await browser.LoadPage(new QdActionFilter
{
Statuses = new[] { QdActionStatus.Queued },
PageFilter = new PageFilter { PageSize = 10 },
}))
.ThrowOnFailOrReturn()
.Content;Filters combine criteria (here Statuses, Types, run counts and a date range), paging (PageFilter) and sorting (each filter lists the sort names it accepts). A Page<T> carries the Content for that page.
Choosing a provider
| Package | Register | Backing store |
|---|---|---|
H.Necessaire.Runtime.Sqlite | SqliteDependencyGroup | SQLite via Dapper, with migrations. Resources for auditing, consumer identity, data bins, logs, QD actions and results, and sync requests. |
H.Necessaire.Runtime.SqlServer | SqlServerRuntimeDependencyGroup | SQL Server via Dapper. |
H.Necessaire.Runtime.RavenDB | RavenDbRuntimeDependencyGroup | RavenDB. |
H.Necessaire.Runtime.Azure.CosmosDB | AzureCosmosDbRuntimeDependencyGroup | Azure Cosmos DB. |
H.Necessaire.Runtime.Google.FirestoreDB | GoogleFirestoreDbRuntimeDependencyGroup | Google Firestore. |
deps.Register<RuntimeDependencyGroup>(() => new RuntimeDependencyGroup());
deps.Register<SqliteDependencyGroup>(() => new SqliteDependencyGroup());Each provider reads its connection settings from the runtime config (see the configuration reference): SqlConnections.* for the SQL providers, RavenDbConnections.*, AzureCosmosDB.* or Google.Firestore.*. The SQLite data bin storage also needs AppDataFolderPath.
Binary data: DataBin
Large binary payloads go through DataBin, a metadata-plus-stream model. The document-database providers split the data into batches to stay under per-document limits: the Cosmos DB provider writes batches of about 1.44 MB, and the Firestore provider keeps each document under its 1 MiB cap.
No database yet?
The core ships base classes for lightweight storage: InMemoryStorageService<TId, TEntity, TFilter> (you supply the function that applies a filter to an in-memory collection), plus FileSystemStorageServiceBase and a cached variant for file-system-backed services rooted at FileSystemStorageRootFolder. They are a good fit for tests and prototypes, and because consumers only see the contracts, swapping in a real provider later doesn't change calling code.