Table of Contents

Class Decorator<TIn, TOut>.Builder

Namespace
PatternKit.Structural.Decorator
Assembly
PatternKit.Core.dll

Fluent builder for Decorator<TIn, TOut>.

public sealed class Decorator<TIn, TOut>.Builder
Inheritance
Decorator<TIn, TOut>.Builder
Inherited Members
Extension Methods

Remarks

The builder collects decorators in registration order. Decorators are applied from outermost to innermost for input transformation, and innermost to outermost for output transformation.

Builders are mutable and not thread-safe. Each call to Build() snapshots the current decorator chain into an immutable instance.

Methods

After(AfterTransform)

Adds a decorator that transforms the output after it returns from the next layer.

public Decorator<TIn, TOut>.Builder After(Decorator<TIn, TOut>.AfterTransform transform)

Parameters

transform Decorator<TIn, TOut>.AfterTransform

The transformation function.

Returns

Decorator<TIn, TOut>.Builder

The same builder instance for chaining.

Examples

.After(static (_, result) => result * 2)
.After(static (_, result) => result + 100)  // Applied to the result of the first After

Remarks

Multiple After decorators are applied in registration order (innermost first on the way out).

Around(AroundTransform)

Adds a decorator that wraps the entire execution with custom logic.

public Decorator<TIn, TOut>.Builder Around(Decorator<TIn, TOut>.AroundTransform transform)

Parameters

transform Decorator<TIn, TOut>.AroundTransform

The wrapper function that receives the input and a delegate to the next layer.

Returns

Decorator<TIn, TOut>.Builder

The same builder instance for chaining.

Examples

.Around((in int x, next) => {
    Console.WriteLine($"Before: {x}");
    var result = next(in x);
    Console.WriteLine($"After: {result}");
    return result;
})

Remarks

Around decorators have full control over whether and how the next layer is invoked. This is useful for cross-cutting concerns like logging, caching, error handling, or retries.

The transform delegate receives:

Before(BeforeTransform)

Adds a decorator that transforms the input before it reaches the next layer.

public Decorator<TIn, TOut>.Builder Before(Decorator<TIn, TOut>.BeforeTransform transform)

Parameters

transform Decorator<TIn, TOut>.BeforeTransform

The transformation function.

Returns

Decorator<TIn, TOut>.Builder

The same builder instance for chaining.

Examples

.Before(static x => x + 10)
.Before(static x => x * 2)  // Applied after the first Before

Remarks

Multiple Before decorators are applied in registration order (outermost first).

Build()

Builds an immutable Decorator<TIn, TOut> with the registered decorators.

public Decorator<TIn, TOut> Build()

Returns

Decorator<TIn, TOut>

A new Decorator<TIn, TOut> instance.

Remarks

The builder can be reused after calling Build() to create variations with additional decorators.