The Stunt class has four entry points:
| API | Returns | Use it to |
|---|---|---|
Stunt.Of<T>(...) |
T |
Get a stunt, with no behavior to configure yet. |
Stunt.For<T>(...) |
StuntReference<T> |
Create a stunt and configure its behaviors. |
Stunt.Get<T>(T stunt) |
StuntReference<T> |
Configure a stunt created earlier. |
Stunt.Builder() |
StuntBuilder |
Configure behaviors once, and build many stunts with them. |
StuntReference<T> implements IStunt, so AddBehavior and InsertBehavior apply to it and
return it for chaining. ToObject() returns the stunt:
var stunt = Stunt.For<ICalculator>();
ICalculator calc = stunt.ToObject();
stunt.AddBehavior((call, next) => ...);
Stunt.Of<T> returns the stunt directly, and Stunt.Get gets a reference to it later:
ICalculator calc = Stunt.Of<ICalculator>();
Stunt.Get(calc).AddBehavior((call, next) => ...);
Behaviors added after the stunt exists change its live pipeline: the next call already sees them.
StuntReference<T> also converts implicitly to T, but C# doesn't allow user-defined
conversions to interfaces, so the conversion only applies to class and delegate stunts.
ToObject() always works. Members called on the reference itself (ToString, Equals,
GetHashCode) are the reference's, not the stunt's.
Stunt.For<T> doesn't construct the stunt right away: the first ToObject() call does, with
the behaviors added so far already in place. Those behaviors see the constructor call, and virtual
members a base class constructor invokes:
public class Greeter
{
public Greeter() => Seen = Name();
public string Seen { get; }
public virtual string Name() => "base";
}
Greeter greeter = Stunt.For<Greeter>()
.AddBehavior((call, next) => call.MethodBase.Name == nameof(Greeter.Name)
? call.CreateValueReturn("stunt")
: next(call, next))
.ToObject();
// greeter.Seen == "stunt"
A class stunt overrides the virtual and abstract members of its base class. Arguments passed to
Of, For or Build go to the base class constructor with matching parameters:
Repository repository = Stunt.Of<Repository>("Server=.;Database=test");
The base class can't be sealed (ST003) and needs an accessible constructor (ST006).
Non-virtual members can't be intercepted: they run as declared.
The T1 to T8 type parameters add interfaces to the stunt. A base class, when there is one,
goes first, as in a C# type declaration (ST001):
var stunt = Stunt.For<Repository, IDisposable>("Server=.;Database=test");
IDisposable disposable = (IDisposable)stunt.ToObject();
A delegate stunt is a delegate whose invocations go through the pipeline. The delegate must be the
only type argument (ST010). Pass an implementation, and next reaches it:
var add = Stunt.For<Func<int, int, int>>((x, y) => x + y)
.AddBehavior((call, next) =>
{
call.Arguments.Set(0, 10);
return next(call, next);
})
.ToObject();
add(1, 2); // 12
When several stunts need the same behaviors, Stunt.Builder() collects them once. Its Build<T>
overloads mirror Stunt.Of<T> (constructor arguments, delegates, additional interfaces):
var builder = Stunt.Builder()
.AddBehavior(new RecordingBehavior())
.AddBehavior(new DefaultValueBehavior());
ICalculator calculator = builder.Build<ICalculator>();
IServiceProvider services = builder.Build<IServiceProvider>();
The behaviors are in the pipeline before each stunt is constructed, so they also intercept virtual members called from base class constructors.
Each Build call takes a snapshot of the behaviors configured at that point: behaviors added to the
builder afterwards don't reach the stunts already built. The behavior instances are shared, so a
single RecordingBehavior records the calls of every stunt from that builder. A behavior with state
of its own per stunt is registered as a factory instead, and each stunt gets a new instance:
var builder = Stunt.Builder()
.AddBehavior(() => new RecordingBehavior()) // one recorder per stunt
.AddBehavior<NotifyPropertyChangeBehavior>() // the same, for a parameterless constructor
.InsertBehavior<SynchronizedBehavior>(0); // insert a per-stunt behavior at index 0
Behaviors that implement ICloneable, such as NotifyPropertyChangeBehavior and
SynchronizedBehavior, are cloned for each built stunt even when added as instances.
Stunt.vb has the same API. Two differences come from the language:
Widening conversion to T has the same interface limitation as in C#, so use ToObject().System.Delegate, so the delegate overloads of Of and
For take any T. A call passing a lone Nothing is ambiguous: write
Stunt.Of(Of IFoo)(New Object() {Nothing}) instead.Dim stunt = Stunt.For(Of ICalculator)()
Dim calc As ICalculator = stunt.ToObject()
stunt.AddBehavior(New DefaultValueBehavior())
Visual Basic projects create stunts at run time, with the Stunts.DynamicProxy package: see Compile time and run time.