Demolite.Db
0.2.0
dotnet add package Demolite.Db --version 0.2.0
NuGet\Install-Package Demolite.Db -Version 0.2.0
<PackageReference Include="Demolite.Db" Version="0.2.0" />
<PackageVersion Include="Demolite.Db" Version="0.2.0" />
<PackageReference Include="Demolite.Db" />
paket add Demolite.Db --version 0.2.0
#r "nuget: Demolite.Db, 0.2.0"
#:package Demolite.Db@0.2.0
#addin nuget:?package=Demolite.Db&version=0.2.0
#tool nuget:?package=Demolite.Db&version=0.2.0
Demolite.Db
An abstract EF Core repository built on IDbContextFactory<T>.
- Every call creates its own short-lived context and disposes it afterwards.
- Every call returns a
Result<T>fromDemolite.Core. Failures are returned, not thrown.
Entities
Entities inherit from AbstractDbItem. It provides an int key named Id.
public sealed class Item : AbstractDbItem
{
public required string Name { get; set; }
}
For another key type, use AbstractDbItem<TKey>. To change how the key is generated, override Id in your entity.
public sealed class Document : AbstractDbItem<Guid>
{
public required string Title { get; set; }
}
Repositories
Inherit from AbstractRepository<TEntity, TKey, TContext>. It needs the entity, the key type and your DbContext. The factory is passed to the base class.
public sealed class ItemRepository(IDbContextFactory<AppDbContext> factory)
: AbstractRepository<Item, int, AppDbContext>(factory);
Best practice: one base repository
Writing TKey and the context type in every repository is repetitive. Define one base repository for your application instead. It fixes the key type and the context. Your repositories then only name the entity.
public abstract class AppRepository<TEntity>(IDbContextFactory<AppDbContext> factory)
: AbstractRepository<TEntity, int, AppDbContext>(factory)
where TEntity : class, IDbItem<int>;
public sealed class ItemRepository(IDbContextFactory<AppDbContext> factory)
: AppRepository<Item>(factory);
The base repository is also the right place for behavior shared by all repositories, for example a different way to create the context.
Registration
Register the context factory. The repositories receive it through constructor injection.
services.AddDbContextFactory<AppDbContext>(options => options.UseSqlite(connectionString));
services.AddScoped<ItemRepository>();
Register the repository under its interface if you want to mock it in tests.
services.AddScoped<IDbRepository<Item, int>, ItemRepository>();
Usage
Check IsSuccess before you use Value. On failure, Error describes the problem.
var result = await repository.GetAsync(id, ct);
if (!result.IsSuccess)
return result.Error;
var item = result.Value;
Errors created by this package:
| Code | Meaning |
|---|---|
Db.NotFound |
A single entity was requested and does not exist. |
Db.SaveFailed |
EF Core threw a DbUpdateException while saving. The exception is attached to the error. |
Operations
| Operation | Methods |
|---|---|
| Read all | GetAll, GetAllAsync, GetAllCustomAsync |
| Read one | Get, GetAsync, GetCustomAsync, GetOrCreateAsync |
| Write one | Create, Update, Delete (sync and async) |
| Write many | CreateMany, UpdateMany, DeleteMany (sync and async) |
- The
Custommethods take anExpression<Func<TEntity, bool>>, so the filter runs in the database. GetOrCreateAsyncreturns the first entity. If the table is empty, it stores and returns the entity built by the factory method you pass in. This is useful for settings tables with one row.- The
Manymethods save all entities in a singleSaveChangescall, so they succeed or fail together. - Async methods take an optional
CancellationToken.
Customizing
All public members are virtual. Override them in your repository to add behavior, for example Include calls.
Two protected members control the context handling:
CreateContextcreates the context for one call.GetSetselects theDbSetthe repository works on.
public sealed class ItemRepository(IDbContextFactory<AppDbContext> factory)
: AppRepository<Item>(factory)
{
protected override DbSet<Item> GetSet(AppDbContext context) => context.Items;
}
To add your own queries, create a context with CreateContext and return a Result yourself.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Demolite.Core (>= 0.1.0)
- Microsoft.EntityFrameworkCore (>= 10.0.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.