Demolite.Db 0.2.0

dotnet add package Demolite.Db --version 0.2.0
                    
NuGet\Install-Package Demolite.Db -Version 0.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Demolite.Db" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Demolite.Db" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Demolite.Db" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Demolite.Db --version 0.2.0
                    
#r "nuget: Demolite.Db, 0.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Demolite.Db@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Demolite.Db&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Demolite.Db&version=0.2.0
                    
Install as a Cake Tool

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> from Demolite.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 Custom methods take an Expression<Func<TEntity, bool>>, so the filter runs in the database.
  • GetOrCreateAsync returns 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 Many methods save all entities in a single SaveChanges call, 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:

  • CreateContext creates the context for one call.
  • GetSet selects the DbSet the 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0 58 10/11/2026
0.1.0 293 12/11/2024
0.0.3 339 9/29/2024
0.0.2 213 9/29/2024