Class Decorator<TIn, TOut>
- Namespace
- PatternKit.Structural.Decorator
- Assembly
- PatternKit.Core.dll
Fluent, allocation-light decorator that wraps a component and applies layered enhancements via ordered decorators. Build once, then call Execute(in TIn) to run the component through the decorator pipeline.
public sealed class Decorator<TIn, TOut>
Type Parameters
TInInput type passed to the component.
TOutOutput type produced by the component.
- Inheritance
-
Decorator<TIn, TOut>
- Inherited Members
- Extension Methods
Examples
var decorator = Decorator<int, int>.Create(static x => x * 2)
.Before(static x => x + 1) // Add 1 before
.After(static x => x * 10) // Multiply result by 10
.Build();
var result = decorator.Execute(5); // ((5 + 1) * 2) * 10 = 120
Remarks
Mental model: A base component is wrapped by zero or more decorators. Each decorator can:
- Transform the input before passing it to the next layer (Decorator<TIn, TOut>.BeforeTransform).
- Transform the output after receiving it from the next layer (Decorator<TIn, TOut>.AfterTransform).
- Wrap the entire execution with custom logic (Decorator<TIn, TOut>.AroundTransform).
Immutability: After Build(), the decorator chain is immutable and safe for concurrent reuse.
Methods
Create(Component)
Creates a new Decorator<TIn, TOut>.Builder for constructing a decorated component.
public static Decorator<TIn, TOut>.Builder Create(Decorator<TIn, TOut>.Component component)
Parameters
Returns
- Decorator<TIn, TOut>.Builder
A new Decorator<TIn, TOut>.Builder instance.
Examples
var dec = Decorator<string, int>.Create(static s => s.Length)
.Before(static s => s.Trim())
.After(static (_, len) => len * 2)
.Build();
var result = dec.Execute(" hello "); // 10
Execute(in TIn)
Executes the decorated component with the given input.
public TOut Execute(in TIn input)
Parameters
inputTInThe input value (readonly via
in).
Returns
- TOut
The result after applying all decorators in order, ending with the base component execution.
Remarks
The execution flows outward-to-inward for Decorator<TIn, TOut>.BeforeTransform decorators (transforming input), then executes the base component, then flows inward-to-outward for Decorator<TIn, TOut>.AfterTransform decorators (transforming output). Decorator<TIn, TOut>.AroundTransform decorators control the entire flow at their layer.