Slothful CRUD helps you generate CRUD API endpoints for your entities with minimal setup.
Implement ISlothfulEntity, register the library, and expose REST endpoints backed by your DbContext.
Full documentation is available at slothful.dev.
- Install package:
dotnet add package slothful-crud- Register services and endpoint generation:
using SlothfulCrud.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<SlothfulDbContext>(options =>
options.UseInMemoryDatabase("AppDb"));
builder.Services.AddSlothfulCrud<SlothfulDbContext, Program>();
var app = builder.Build();
app.UseSlothfulCrud<SlothfulDbContext, Program>();
app.Run();The Program type parameter serves as an assembly marker for entity discovery. You can use any type from the assembly containing your entities.
- Implement
ISlothfulEntityon your domain type:
using SlothfulCrud.Domain;
public class Sloth : ISlothfulEntity
{
public Guid Id { get; private set; }
public string Name { get; private set; }
public int Age { get; private set; }
public string DisplayName => Name;
public Sloth(Guid id, string name, int age)
{
Id = id;
Name = name;
Age = age;
}
public void Update(string name, int age)
{
Name = name;
Age = age;
}
}By default, the following endpoints are generated per entity:
GET /{segment}/{id}- detailsGET /{segment}/list/{page}- browseGET /{segment}/selectable-list/{page}- browse selectablePOST /{segment}- createPUT /{segment}/{id}- updateDELETE /{segment}/{id}- delete
POST createrequest body does not includeid; the key is generated server-side.201 CreatedincludesLocation: /{segment}/{id}and usesIApiSegmentProvider.- Validation is enabled by default (
HasValidation = true). Register validators, e.g.:
builder.Services.AddValidatorsFromAssemblyContaining<YourValidator>();- Endpoint segment strategy (
IApiSegmentProvider) - Key generation strategy (
IEntityPropertyKeyValueProvider<TEntity>) - Create constructor selection (
ICreateConstructorBehavior) - Entity and endpoint configuration (
ISlothEntityConfiguration<T>) - Problem handling (
UseSlothfulProblemHandling) - Query customization (
QueryCustomizerinSlothfulOptions)
BenchmarkDotNet benchmarks are included to track performance regressions and compare internal changes.
Run all benchmarks:
dotnet run -c Release --project library/SlothfulCrud/Tests/SlothfulCrud.Benchmarks -- --filter "*"Results are exported to:
library/SlothfulCrud/Tests/SlothfulCrud.Benchmarks/BenchmarkDotNet.Artifacts/results/
See details in docs:
- NuGet: slothful-crud
- License: MIT