Built-in behaviors

The Stunts package includes these behaviors. Add them with AddBehavior; the order matters, since the first behavior runs outermost.

DefaultValueBehavior

Answers every call with default values for the return value and the out arguments. Add it last, to answer what the behaviors before it don't:

ICalculator calc = Stunt.For<ICalculator>()
    .AddBehavior(new DefaultValueBehavior())
    .ToObject();

The values come from a DefaultValueProvider. Besides default(T), it returns completed Task and ValueTask instances (with a default result), empty arrays, enumerables and queryables, and value tuples of defaults. Register a factory for any other type, or replace a built-in one:

var defaults = new DefaultValueProvider();
defaults.Register(() => "Hello!");

var greet = Stunt.For<Func<string>>()
    .AddBehavior(new DefaultValueBehavior(defaults))
    .ToObject();

greet(); // "Hello!"

Deregister removes a factory, and a subclass can override GetFallbackDefaultValue for types with no factory.

DefaultEqualityBehavior

Implements Equals and GetHashCode the way System.Object does: reference equality and the object's hash code.

RecordingBehavior

Keeps every call and its result in Invocations. Add it first to record what the caller sees:

var recorder = new RecordingBehavior();
var calc = Stunt.For<ICalculator>()
    .AddBehavior(recorder)
    .AddBehavior(new DefaultValueBehavior())
    .ToObject();

calc.Add(2, 3);
Console.WriteLine(recorder); // one line per call, with its result

ExceptionMappingBehavior

Translates the exceptions thrown by the rest of the pipeline. The map returns a replacement, or null to leave the exception unchanged:

stunt.AddBehavior(new ExceptionMappingBehavior(ex =>
    ex is SqlException sql ? new DataException(sql.Message, sql) : null));

Pass swallow: true to turn a null result into the DefaultValueProvider value instead, and pass a provider to reuse its custom defaults. A faulted Task or ValueTask stays a fault of the returned awaitable, with the mapped exception. Behaviors added before this one are outside the translated call.

NotifyPropertyChangeBehavior

Raises PropertyChanging before a property setter runs and PropertyChanged after it succeeds. The stunt implements INotifyPropertyChanged, and INotifyPropertyChanging for the second event:

var stunt = Stunt.For<IPerson, INotifyPropertyChanged, INotifyPropertyChanging>()
    .AddBehavior(new NotifyPropertyChangeBehavior());

Assigning the value last written raises nothing; new NotifyPropertyChangeBehavior(shortCircuitUnchanged: true) also skips the setter. The behavior compares values without calling getters, and keeps state per instance, so the builder clones it for each stunt.

TargetBehavior

Forwards every call to the same member of another object, or of an object a factory creates on the first call:

var stunt = Stunt.For<ICalculator>()
    .AddBehavior(new RecordingBehavior())
    .AddBehavior(new TargetBehavior(new Calculator()));

Constructors, Equals, GetHashCode and ToString go to the rest of the pipeline, and so does everything when the factory returns null. The target's exceptions propagate unchanged.

More behaviors

Stunts.Extensions adds logging, metrics and tracing, synchronization, data annotation validation and guards.