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
transformDecorator<TIn, TOut>.AfterTransformThe transformation function.
Returns
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
transformDecorator<TIn, TOut>.AroundTransformThe wrapper function that receives the input and a delegate to the next layer.
Returns
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:
- The input value.
- A Decorator<TIn, TOut>.Component delegate representing the next layer (call it to proceed).
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
transformDecorator<TIn, TOut>.BeforeTransformThe transformation function.
Returns
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.