Extract

Pulls a nested property or field out of a Memory object into its own Memory slot. Uses reflection to find the first public instance property or field of type TOut on the TIn object.

Signatures

Extract<TIn, TOut>()

Retrieves TIn from Memory, then extracts a TOut property/field from it.

protected Monad<TInput, TReturn> Extract<TIn, TOut>()

Extract<TIn, TOut>(TIn input)

Extracts a TOut property/field from the provided TIn object.

protected Monad<TInput, TReturn> Extract<TIn, TOut>(TIn input)

These are the train's own methods, which start a chain in Junctions(). Later in a chain, after a Chain or ShortCircuit, the call is MonadTask<TInput, TReturn>'s method of the same name, which takes the same arguments and returns MonadTask<TInput, TReturn>.

Type Parameters

Type ParameterDescription
TInThe source type to extract from. Must have a public instance property or field of type TOut.
TOutThe target type to extract and store in Memory.

Parameters

ParameterTypeRequiredDescription
inputTInNoThe source object (second overload only). If omitted, TIn is retrieved from Memory.

Returns

The chain, for fluent chaining: Monad<TInput, TReturn> from the train's method, MonadTask<TInput, TReturn> from a later link.

Example

public record OrderInput(string CustomerId, OrderDetails Details);
public record OrderDetails(string ItemId, int Quantity);
 
protected override Task<Either<Exception, OrderResult>> Junctions() =>
        Extract<OrderInput, OrderDetails>()       // Pulls OrderDetails out of OrderInput
        .Chain<ProcessOrder>().Resolve();               // ProcessOrder receives OrderDetails from Memory

Behavior

  1. Looks for the first public instance property of type TOut on TIn. Static properties and indexers are skipped: a static value is not this object's, and an indexer cannot be read without an argument.
  2. If not found, looks for the first public instance field of type TOut.
  3. If found, stores the value in Memory under typeof(TOut).
  4. If not found or null, sets the exception: "Could not find non-null value of type: ({TOut}) in properties or fields for ({TIn}). Is it public?"

Remarks

  • Only the first matching property/field is extracted. If TIn has multiple properties of type TOut, only the first one found is used.
  • Properties and fields must be public and belong to the instance to be discoverable.
  • If TIn is not found in Memory (parameterless overload), sets the exception, naming the step, the train and the missing type: "Extract<OrderInput, OrderDetails> (train 'ProcessOrderTrain') found no 'OrderInput' in Memory. Chain a junction that outputs 'OrderInput' before the Extract.". That holds for a value type too.