HookContext class

Immutable context passed to every hook during UseCase execution.

Contains everything a hook needs to decide whether to act and how. The metadata map is mutable and shared by reference across all three phases of a single UseCase invocation. Hooks can write values in the pre phase and read them back in success or failure.

Example

class MyHook extends Hook {
  @override
  String get id => 'my-hook';

  @override
  Future<void> execute(HookContext context, HookPhase phase) async {
    switch (phase) {
      case HookPhase.pre:
        context.metadata['_start'] = DateTime.now();
      case HookPhase.success:
        final start = context.metadata['_start'] as DateTime;
        print('${context.useCaseName} took ${DateTime.now().difference(start)}');
      case HookPhase.failure:
        print('${context.useCaseName} failed: ${context.failure}');
    }
  }
}

Constructors

HookContext({required String useCaseName, required DateTime timestamp, Object? params, Object? result, AppFailure? failure, Duration? duration, String? traceId, String? spanId, Map<String, dynamic>? metadata})
Creates a new hook context.

Properties

duration → Duration?
Execution duration (null in the pre phase).
final
failure → AppFailure?
The failure (only set in the failure phase).
final
hashCode → int
The hash code for this object.
no setterinherited
metadata → Map<String, dynamic>
Mutable shared metadata bag for cross-phase data.
final
params → Object?
The input parameters passed to the UseCase.
final
result → Object?
The successful result (only set in the success phase).
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
spanId → String?
W3C span ID from the active OTel span (if configured).
final
timestamp → DateTime
When the UseCase was invoked.
final
traceId → String?
W3C trace ID from the active OTel span (if configured).
final
useCaseName → String
Runtime type of the UseCase (e.g. 'GetDealListUseCase').
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
paramsAs<P>() → P
Convenience: cast params to expected type.
resultAs<R>() → R
Convenience: cast result to expected type.
toString() → String
A string representation of this object.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited