Kildetoft.SimpleSQLite
2.0.1
See the version list below for details.
dotnet add package Kildetoft.SimpleSQLite --version 2.0.1
NuGet\Install-Package Kildetoft.SimpleSQLite -Version 2.0.1
<PackageReference Include="Kildetoft.SimpleSQLite" Version="2.0.1" />
paket add Kildetoft.SimpleSQLite --version 2.0.1
#r "nuget: Kildetoft.SimpleSQLite, 2.0.1"
// Install Kildetoft.SimpleSQLite as a Cake Addin
#addin nuget:?package=Kildetoft.SimpleSQLite&version=2.0.1
// Install Kildetoft.SimpleSQLite as a Cake Tool
#tool nuget:?package=Kildetoft.SimpleSQLite&version=2.0.1
Kildetoft.SimpleSQLite
This package provides a wrapper for an async SQLite connection, which exposes a minimal interface for erforming CRUD operations, plus querying via specifications.
Setup
Create the classes that represent the desired datamodels, and have them implement the exposed IEntity interface. These classes must have the Table attribure from sqlite-net, as well as the PrimaryKey attribute on the Id field.
For each index desired, create a generic implementation of the IIndex<T> interface, where T is the IEntity representing the table for the index. Add the necessary choice of whether the index should be unique, as well as a LambdaExpression as the IndexDefinition that defines which property the index should be created on.
The main interfaces for interacting with the data will be the IDataAccessor and IAsyncDataAccessor, depending on whether synchronous or asynchronous operations are needed. Anywhere in the following where something is mentioned for IDataAccessor, a similar statement will hold for IAsyncDataAccessor, with the appropriate modifications for async methods. This is registered in an IServiceCollection by using the supplied extension method AddSimpleSQLite, providing it with the connection string for the database file to be used. This will return an IConnectionRegistration, on which it is possible to add tables and indexes. The simplest way is via the methods AddTablesFromAssemblyContaining<T>, AddIndexesFromAssemblyContaining<T>, and AddAllFromAssemblyContaining<T>. These will find all classes in the assemblies containing the specified type, and add either tables, indexes or both, to the database based on what it finds.
CRUD operations
The IDataAccessor interface exposes the basic CRUD operations, via the Create, GetById, Update, and Delete methods.
Querying data
IDataAccessor also exposes the Get method, which is used for querying the data. It takes an ISpecification, which determines what it returns.
The return type is based on whether the ISpecification is:
- an IAllSpecification (return all results),
- an IFirstSpecification (return first result, throwing an exception if no results are found),
- or an IFirstOrDefaultSpecification (returning the first result if it exists, and null otherwise)
The specifications can also be used to define:
- which data to return, using the IWhereSpecification
- How to sort the data, using the IOrderSpecification
- Whether to skip some of the data, using the ISkipSpecification
- How many items to take, using the ITakeSpecification (which extends the IAllSpecification)
Samples and testing
See the SimpleSQLite.SampleClasses project for examples of creating Entities and Specifications. See the SimpleSQLite.Tests project for examples of how to test Specifications using the provided DataAccessMock
Product | Versions 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- sqlite-net-pcl (>= 1.9.172)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Breaking change from version 1:
The previous asynchronous methods for interacting with data have been moved to the new IAsyncDataAccessor interface, and renamed with Async suffixes to match the standard naming conventions.
The IDataAccessor interface now exposes synchronous versions of the same methods.
Version 2.0.1 fixes a bug where the new interface was not registered in DI.