RoyalCode.SmartCommands.WorkContext
0.1.0
dotnet add package RoyalCode.SmartCommands.WorkContext --version 0.1.0
NuGet\Install-Package RoyalCode.SmartCommands.WorkContext -Version 0.1.0
<PackageReference Include="RoyalCode.SmartCommands.WorkContext" Version="0.1.0" />
<PackageVersion Include="RoyalCode.SmartCommands.WorkContext" Version="0.1.0" />
<PackageReference Include="RoyalCode.SmartCommands.WorkContext" />
paket add RoyalCode.SmartCommands.WorkContext --version 0.1.0
#r "nuget: RoyalCode.SmartCommands.WorkContext, 0.1.0"
#:package RoyalCode.SmartCommands.WorkContext@0.1.0
#addin nuget:?package=RoyalCode.SmartCommands.WorkContext&version=0.1.0
#tool nuget:?package=RoyalCode.SmartCommands.WorkContext&version=0.1.0
SmartCommands
SmartCommands is a family of .NET libraries that implements a command-pattern approach using source generators to eliminate boilerplate for handlers, DI registration, validation, unit of work, repository access, decorators, and HTTP endpoint mapping.
Projects target .NET 8, .NET 9, and .NET 10. The analyzer/generator targets .NET Standard 2.0.
- Runtime:
RoyalCode.SmartCommands— attributes and contracts for commands and handlers. - EF Core adapter:
RoyalCode.SmartCommands.EntityFramework—IUnitOfWorkAccessor<TContext>andIRepositoriesAccessor<TContext>. - WorkContext adapter:
RoyalCode.SmartCommands.WorkContext— adapters and DI forIWorkContext. - Source Generator:
RoyalCode.SmartCommands.Generators— creates handlers, DI wiring, find/edit flows, decorators pipeline, and HTTP mapping.
Features
- Attribute-driven commands with generated handlers (
I{Command}Handler,{Command}Handler). - Validation integration via
HasProblems(out Problems?)andWithValidateModel. - Additional validation methods with
CommandValidation(Orderdefaults to10) andResult/Task<Result>/ValueTask<Result>short-circuiting. - Unit of Work and repositories via
WithUnitOfWork<TContext>/WithDbContext/WithWorkContext. - Entity loading and editing with
WithFindEntities<TContext>andEditEntity<TEntity,TId>. - Decorators pipeline with
WithDecoratorsandIDecorator<TCommand, TResult>. - Minimal APIs mapping using
MapPost/Put/Patch/Delete/Get, metadata (WithSummary,WithDescription,WithAuthorization,WithPolicy,WithTags), created/accepted location (MapCreatedRoute,MapAcceptedRoute— the202location is optional), response composition (MapIdResultValue,MapResponseValues), explicit success status (WithResultStatus:Ok/Created/Accepted/NoContent), and repeatable endpoint filters (WithEndpointFilter<TFilter>). - Consistent results and problems modeling via SmartProblems (
Result,Result<T>,Problems).
Compatibility
- Commands: .NET 8, .NET 9, .NET 10.
- Generator: .NET Standard 2.0.
- Tested with Microsoft.NET.Test.Sdk 18.x; see
RoyalCode.SmartCommands.Testsfor coverage across sync/async, validate, decorators, find/edit, and mapping scenarios.
Installation
Add the packages you need:
RoyalCode.SmartCommandsRoyalCode.SmartCommands.EntityFramework(EF Core accessors)RoyalCode.SmartCommands.WorkContext(WorkContext accessors)RoyalCode.SmartCommands.Generators(analyzer/source generator)
RoyalCode.SmartCommands brings RoyalCode.SmartProblems.ApiResults transitively because Minimal API code
generated by SmartCommands uses its HTTP result unions and ProduceProblems metadata. Add
RoyalCode.SmartProblems.Http explicitly only when using its optional HTTP/filter integrations.
Quick start
- Define a partial command class with a method annotated with
Commandand required attributes. - Implement
HasProblems(out Problems?)when usingWithValidateModel. - Register Unit of Work adapters.
- Consume the generated handler interface via DI.
Example (create entity with validation + UoW):
using RoyalCode.SmartCommands;
using RoyalCode.SmartProblems;
using RoyalCode.SmartValidations;
public partial class CreateProduct
{
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
public bool HasProblems(out Problems? problems) =>
Rules.Set<CreateProduct>()
.NotEmpty(Name)
.GreaterThanOrEqual(Price, 0)
.HasProblems(out problems);
[Command, WithValidateModel, WithUnitOfWork<MyDbContext>]
public Result<Product> Execute()
{
var entity = new Product(Name, Price);
return entity;
}
}
EF Core registration:
services.AddUnitOfWorkAccessor<MyDbContext>();
WorkContext registration:
services.AddWorkContext<MyDbContext>()
.AddUnitOfWorkAccessor<MyDbContext>();
HTTP mapping (Minimal APIs):
[MapPost("/", "CreateProduct")]
[MapGroup("api/products")]
[WithSummary("Create product")]
[WithDescription("Creates a new product")]
[MapCreatedRoute("{id}", nameof(Product.Id))]
public partial class CreateProduct { /* ... */ }
Guidance
- Prefer returning
Result/Result<T>; avoid exceptions for expected flows. - Use partial classes to enable
WasValidatedgeneration for null-state. - Do not mix
WithUnitOfWorkwithWithDbContext/WithWorkContexton the same command. EditEntity<TEntity,TId>requires UoW and the first method parameter typed asTEntity.CancellationTokenonly in async methods.
Documentation
Tests
See RoyalCode.SmartCommands.Tests for scenarios covering:
- Validation-first pipeline (
WithValidateModel). - Additional validation ordering, DI, cancellation and short-circuiting.
- Decorators sync/async with and without results.
- Find entities and collections by id (
MapFind) or by an alternate/composite key (MapFindBy<TEntity>), edit flows, and NotFound problems that name the entity. - Created and accepted responses with
MapCreatedRoute,MapAcceptedRoute,MapIdResultValue, andMapResponseValues. - HTTP status selection, tags, endpoint filters, Find/Search and OpenAPI metadata.
| 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. 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
- Microsoft.Extensions.Options (>= 10.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.0)
- RoyalCode.SmartCommands (>= 0.1.0)
- RoyalCode.WorkContext.EntityFramework (>= 0.10.1)
-
net8.0
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- RoyalCode.SmartCommands (>= 0.1.0)
- RoyalCode.WorkContext.EntityFramework (>= 0.10.1)
-
net9.0
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- RoyalCode.SmartCommands (>= 0.1.0)
- RoyalCode.WorkContext.EntityFramework (>= 0.10.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.