library for .NET

Every operation as a function.

A delegate type, made by a factory that takes its dependencies, registered as a service. A source generator writes the registrations; the endpoint asks for the operation itself.

Package
FunctionInjection
Version
0.1.0-preview.1
Targets
.NET 10, .NET 11
Language
C# 15 ready
Dependencies
none on .NET 11
Generator
included
License
MIT
Author
Kevin Valmorbida
dotnet add package FunctionInjection --prerelease

01

Declare it, ask for it.

The parameters of the factory are the dependencies of the function. The endpoint names the delegate and gets the operation, with nothing in between.

Orders.cs · Program.cs
// Orders.cs
public delegate Task<Order?> GetOrderById(Guid id, CancellationToken cancellationToken);

public static class OrderFunctions
{
    [Function]
    public static GetOrderById GetOrderById(IOrderRepository orders) => orders.GetAsync;
}

// Program.cs
builder.Services.AddFunctions();

app.MapGet("/orders/{id}", (Guid id, GetOrderById getOrderById, CancellationToken ct) => getOrderById(id, ct));

02

C# 15 ready: results as unions.

A function that can end in more than one way returns a union. Each outcome is a type of its own; the endpoint switches on them, and the compiler checks that every case is handled.

Orders.cs · Program.cs · C# 15
// Orders.cs
public sealed record NotFound(Guid Id);
public readonly union OrderResult(Order, NotFound);

public delegate Task<OrderResult> GetOrderById(Guid id, CancellationToken cancellationToken);

public static class OrderFunctions
{
    [Function]
    public static GetOrderById GetOrderById(IOrderRepository orders) => async (id, cancellationToken) =>
        await orders.FindAsync(id, cancellationToken) is { } order ? order : new NotFound(id);
}

// Program.cs
app.MapGet("/orders/{id}", async (Guid id, GetOrderById getOrderById, CancellationToken ct) =>
    await getOrderById(id, ct) switch
    {
        Order order => Results.Ok(order),
        NotFound => Results.NotFound(),
    });

No default case: add an outcome to the union and the compiler names it, with warning CS8509, in every switch that does not handle it. Built and run on .NET 11 with the package from nuget.org; the tests of the library cover the generator and the decorators with unions.

03

As fast as an interface.

The same operation four ways, from a scoped repository. Mean time of one call, of one call with a behaviour in front, and of a whole request with its scope.

Way One call With a behaviour Whole request
Interface2.5 ns2.8 ns91.0 ns
Function2.6 ns21.8 ns110.3 ns
MediatR 12.569.6 ns103.3 ns130.7 ns
Mediator 3.049.0 ns74.2 ns154.8 ns

A call is a call of a delegate.

Nothing allocated, nothing looked up: what an interface costs. A mediator pays its dispatch every time.

Behaviours without reflection.

A decorator for many functions goes through the wrapper the generator writes: the fastest of the three.

The container weighs more.

In a whole request a function is 1.2 times an interface: a closure made in the scope.

BenchmarkDotNet, .NET 11, Ryzen 7 7800X3D. All the results

04

What comes with it.

  • Source generator

    [Function] on a factory. What cannot be registered fails the build, FI001 to FI009.

  • Many projects

    [FunctionRegistrations] lets another project call the registration, once however many times.

  • Catalog

    Name, description, tags, lifetime and dependencies of every function.

  • Decorators

    Around one function, or many at once. Tracing and metrics come with it.

  • AI and MCP tools

    The catalog as tools for Microsoft.Extensions.AI and for MCP servers.

  • Tests

    ReplaceFunction swaps in a fake; ValidateFunctions creates them all at startup.