Skip to content

Latest commit

 

History

History

docs

Folders and files

readme.md

title Home
layout simple
og_type website

Interception for .NET

Objects that pull any stunt you choreograph.

Stunts generates proxies at compile time and lets you configure them in code with a pipeline of behaviors. Nothing is emitted at run time, so it runs everywhere .NET does: Native AOT, iOS devices and game consoles included.

dotnet add package Stunts
The Stunts helmet

Create, choreograph, call

A stunt implements the interface, class or delegate you ask for. Every call goes through its behaviors, in order, and any of them can answer, change the arguments, call the next one or throw.

  • Interface
  • Class
  • Delegate
  • Builder
var stunt = Stunt.For<ICalculator>();
ICalculator calc = stunt.ToObject();

stunt.AddBehavior(
    (call, next) => call.CreateValueReturn(call.Arguments.Get<int>("x") + call.Arguments.Get<int>("y")),
    appliesTo: call => call.MethodBase.Name == nameof(ICalculator.Add));

calc.Add(2, 3); // 5
public class Greeter
{
    public virtual string Greet(string name) => $"Hello, {name}";
}

Greeter greeter = Stunt.For<Greeter>()
    .AddBehavior((call, next) =>
    {
        call.Arguments.Set(0, "Stunts");
        return next(call, next);  // runs the base implementation
    })
    .ToObject();

greeter.Greet("world"); // "Hello, Stunts"
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
var recorder = new RecordingBehavior();
var builder = Stunt.Builder()
    .AddBehavior(recorder)
    .AddBehavior(new DefaultValueBehavior());

ICalculator calculator = builder.Build<ICalculator>();
IServiceProvider services = builder.Build<IServiceProvider>();

calculator.Add(2, 3);                   // 0, from DefaultValueBehavior
services.GetService(typeof(string));    // null
// recorder.Invocations has the calls of both: one recorder for every stunt the builder built

What you get

Compile-time proxies A Roslyn source generator writes the proxy for each type you create a stunt of, and caches it between builds. Native AOT ready Stunts are created without reflection, from typed constructors a module initializer registers. Behavior pipeline A chain of responsibility you configure with lambdas or reusable IStuntBehavior classes. Built-in behaviors Default values, equality, recording, exception mapping, property change notifications, forwarding. Production behaviors Logging, metrics and tracing, synchronization, data annotations and guards, in Stunts.Extensions. Your own API Mark a factory with [StuntGenerator] and the generator follows it, generic wrappers included. Errors at build time Analyzers report what cannot be proxied at the call that asks for it, before anything runs. Debugger friendly Behaviors, invocations and results show up concisely in the debugger.

Packages

Stunts is all most projects need. The others add behaviors, switch to run-time proxies, or give you the pipeline without any proxy generation.

The name comes from test doubles, which in turn come from the stunt doubles of film making. Stunts provides the low-level building blocks; higher-level APIs, such as mocking libraries, are built on top.

Stunts is open source under the MIT license. Its use is subject to the Open Source Maintenance Fee EULA: if you use it to generate revenue, sponsoring is required.