dotnet add package Stunts.Extensions
The package adds behaviors for production code: observability, synchronization, validation and guards. It targets .NET 10 (matching NativeValidation) and is Native AOT compatible.
ObservabilityBehavior logs each call with an ILogger, records its duration in a histogram, and
writes an Activity named for the member:
using var meter = new Meter("MyApp");
var orders = Stunt.For<IOrders>()
.AddBehavior(new ObservabilityBehavior(logger, meter))
.AddBehavior(new TargetBehavior(new Orders()))
.ToObject();
The histogram is stunts.invocation.duration, in milliseconds, tagged with stunt.type,
member.name and status (ok, error or canceled). Activities come from the Stunts
ActivitySource (ObservabilityBehavior.Source), and log events have the id
ObservabilityBehavior.StuntInvocation. Successful calls log at Information, cancellations at
Warning and failures at Error. The duration of a Task or ValueTask member runs until it
completes.
Arguments are logged and tagged. A redaction drops or replaces them before that; the target still receives the originals:
var behavior = new ObservabilityBehavior(logger, meter, (call, arguments) =>
{
arguments.Drop("password");
arguments.Replace("card", "****");
});
Add this behavior first: one activity and one duration then cover every behavior after it. A failing logger or listener never changes the outcome of the call.
SynchronizedBehavior serializes overlapping calls on a stunt:
var cache = Stunt.For<ICache>()
.AddBehavior(new SynchronizedBehavior())
.AddBehavior(new TargetBehavior(new Cache()))
.ToObject();
Add it first, so the lock covers every behavior after it. A Task or ValueTask member keeps the
lock until it completes. Re-entering from the thread that holds the lock throws
InvalidOperationException instead of deadlocking; other threads wait. The parameterless
constructor gives each stunt its own lock (the builder clones it per stunt), and
new SynchronizedBehavior(syncRoot) shares one lock across every behavior constructed with the
same object.
DataAnnotationsBehavior rejects a call when a built-in System.ComponentModel.DataAnnotations
attribute fails:
public interface IAccount
{
[Required]
[EmailAddress]
[Display(Name = "Email address")]
string Email { get; set; }
void Rename([Required, StringLength(8, MinimumLength = 2)] string name, [Range(1, 10)] int attempt);
}
var stunt = Stunt.For<IAccount>();
stunt.AddBehavior(new DataAnnotationsBehavior());
IAccount account = stunt.ToObject();
account.Email = "ada@example.com";
Property attributes run on the setter. Method and constructor attributes run on the annotated
parameters. Getters are not checked. On a synchronous member, the first failure throws
ValidationException on the caller thread; on a Task or ValueTask member, it returns a faulted
awaitable of the same type containing that ValidationException (matching GuardBehavior), and
the rest of the pipeline does not run.
The checks come from NativeValidation: a source
generator turns each attribute into a direct call, so the behavior doesn't reflect and stays valid
for Native AOT. A [StuntGenerator] factory registers the closed types named at its call sites. A
factory that is not a stunt generator uses [Validated] from Devlooped.DataAnnotations, and a
closed type no factory call names uses [assembly: Validate<T>].
CompareAttribute reads the other property from the instance, which re-enters the pipeline, so a
SynchronizedBehavior wrapped outside this one throws on that re-entry.
CustomValidationAttribute calls the named static method. Any other ValidationAttribute
subclass warns as NVA001 and is not enforced.
GuardBehavior runs a predicate over the call: an exception rejects it, null lets it continue.
Use it for validation, null checks and authorization:
var stunt = Stunt.For<IOrders>();
stunt.AddBehavior(new GuardBehavior(call =>
call.MethodBase.Name == nameof(IOrders.Track) && call.Arguments.GetValue(0) is null
? new ArgumentNullException("sku")
: null));
IOrders orders = stunt.ToObject();
A synchronous member throws the exception on the caller thread. A Task or ValueTask member
returns a faulted awaitable of the same type, and the rest of the pipeline does not run. The
constructor is a call too, so a guard added before the stunt is constructed sees it.
Observability and synchronization go first, so they cover the whole call. Data annotations and guards go last, right before the target: behaviors added before them still see the rejection.