Class FixedWidthTransformer<TSource, TDestination>

Namespace
Wolfgang.Etl.FixedWidth
Assembly
Wolfgang.Etl.FixedWidth.dll

Transforms a stream of TSource records into TDestination records in a single streaming pass (#14) — the middle stage of an extract → transform → load pipeline. Compose it between a FixedWidthExtractor<TRecord> reading one fixed-width layout and a FixedWidthLoader<TRecord> writing another to reformat a file (reorder, add/remove, or reformat fields).

public sealed class FixedWidthTransformer<TSource, TDestination> : TransformerBase<TSource, TDestination, FixedWidthReport>, ITransformWithProgressAndCancellationAsync<TSource, TDestination, FixedWidthReport>, ITransformWithCancellationAsync<TSource, TDestination>, ITransformWithProgressAsync<TSource, TDestination, FixedWidthReport>, ITransformAsync<TSource, TDestination>, IReportsItemErrors, IAsyncDisposable, IDisposable where TSource : notnull where TDestination : notnull

Type Parameters

TSource

The source record type.

TDestination

The destination record type.

Inheritance
TransformerBase<TSource, TDestination, FixedWidthReport>
FixedWidthTransformer<TSource, TDestination>
Implements
ITransformWithProgressAndCancellationAsync<TSource, TDestination, FixedWidthReport>
ITransformWithCancellationAsync<TSource, TDestination>
ITransformWithProgressAsync<TSource, TDestination, FixedWidthReport>
ITransformAsync<TSource, TDestination>
IReportsItemErrors
Inherited Members
TransformerBase<TSource, TDestination, FixedWidthReport>.DisposeAsync()
TransformerBase<TSource, TDestination, FixedWidthReport>.Dispose()
TransformerBase<TSource, TDestination, FixedWidthReport>.ReportingInterval
TransformerBase<TSource, TDestination, FixedWidthReport>.CurrentItemCount
TransformerBase<TSource, TDestination, FixedWidthReport>.CurrentSkippedItemCount
TransformerBase<TSource, TDestination, FixedWidthReport>.CurrentErrorItemCount
TransformerBase<TSource, TDestination, FixedWidthReport>.MaximumItemCount
TransformerBase<TSource, TDestination, FixedWidthReport>.SkipItemCount
TransformerBase<TSource, TDestination, FixedWidthReport>.WorkerResilience
TransformerBase<TSource, TDestination, FixedWidthReport>.ErrorPolicy

Examples

using var extractor = new FixedWidthExtractor<LegacyRecord>(sourceReader);
using var transformer = new FixedWidthTransformer<LegacyRecord, ModernRecord>(
    legacy => new ModernRecord { Id = legacy.OldId, Name = legacy.FullName });
using var loader = new FixedWidthLoader<ModernRecord>(destinationWriter);

var modern = transformer.TransformAsync(extractor.ExtractAsync(token), token);
await loader.LoadAsync(modern, token);

Constructors

FixedWidthTransformer(Func<TSource, TDestination>, ILogger<FixedWidthTransformer<TSource, TDestination>>?)

Creates a transformer that projects each source record with transform. The projection handles every reformatting case — reordered, added, removed, or format-converted fields.

public FixedWidthTransformer(Func<TSource, TDestination> transform, ILogger<FixedWidthTransformer<TSource, TDestination>>? logger = null)

Parameters

transform Func<TSource, TDestination>

The per-record projection.

logger ILogger<FixedWidthTransformer<TSource, TDestination>>

An optional ILogger<TCategoryName> for diagnostic output. When null — or omitted — Instance is used and logging is disabled.

Exceptions

ArgumentNullException

transform is null.

FixedWidthTransformer(Func<TSource, TDestination>, FixedWidthTransformerOptions?, ILogger<FixedWidthTransformer<TSource, TDestination>>?)

Initializes a new FixedWidthTransformer<TSource, TDestination> with the base-stage configuration taken from options (ADR-0009).

public FixedWidthTransformer(Func<TSource, TDestination> transform, FixedWidthTransformerOptions? options, ILogger<FixedWidthTransformer<TSource, TDestination>>? logger = null)

Parameters

transform Func<TSource, TDestination>

The per-record transformation.

options FixedWidthTransformerOptions

The stage configuration (ReportingInterval, MaximumItemCount, SkipItemCount, ErrorPolicy); null keeps the defaults.

logger ILogger<FixedWidthTransformer<TSource, TDestination>>

The logger; null logs nothing.

Exceptions

ArgumentNullException

transform is null.

Methods

ByMatchingProperties()

Creates a transformer that copies every source property to the destination property of the same name and an assignable type. TDestination must have a public parameterless constructor. Use the projection constructor for anything beyond a straight same-name copy (reordering, format conversion, combining fields).

public static FixedWidthTransformer<TSource, TDestination> ByMatchingProperties()

Returns

FixedWidthTransformer<TSource, TDestination>

CreateProgressReport()

Creates a progress report object of type TProgress.

protected override FixedWidthReport CreateProgressReport()

Returns

FixedWidthReport

TProgress - A new instance of the progress report object.

CreateProgressTimer(IProgress<FixedWidthReport>)

Creates the Wolfgang.Etl.Abstractions.IProgressTimer used to drive progress callbacks. Override this method in a derived class to inject a custom timer (for example, a custom implementation that allows manual control in unit tests).

protected override IProgressTimer CreateProgressTimer(IProgress<FixedWidthReport> progress)

Parameters

progress IProgress<FixedWidthReport>

The progress sink that will receive callbacks.

Returns

IProgressTimer

A started Wolfgang.Etl.Abstractions.IProgressTimer instance.

TransformWorkerAsync(IAsyncEnumerable<TSource>, CancellationToken)

The worker method that performs the actual transformation.

protected override IAsyncEnumerable<TDestination> TransformWorkerAsync(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)

Parameters

source IAsyncEnumerable<TSource>
cancellationToken CancellationToken

Returns

IAsyncEnumerable<TDestination>

IAsyncEnumerable<TDestination> - The result may be an empty sequence if no data is available or if the transformation fails.