Class DbLoader<TRecord, TProgress>
Loads records into a database via INSERT or UPDATE commands. Uses Dapper for parameter mapping from POCO properties.
public class DbLoader<TRecord, TProgress> : LoaderBase<TRecord, TProgress>, ILoadWithProgressAndCancellationAsync<TRecord, TProgress>, ILoadWithProgressAsync<TRecord, TProgress>, ILoadWithCancellationAsync<TRecord>, ILoadAsync<TRecord> where TRecord : notnull where TProgress : notnull
Type Parameters
TRecordThe POCO type representing a single row. Properties are mapped to command parameters by name or
[Column("name")]attribute.TProgressThe type of the progress object reported during loading. Use DbReport for the default implementation.
- Inheritance
-
LoaderBase<TRecord, TProgress>DbLoader<TRecord, TProgress>
- Implements
-
ILoadWithProgressAndCancellationAsync<TRecord, TProgress>ILoadWithProgressAsync<TRecord, TProgress>ILoadWithCancellationAsync<TRecord>ILoadAsync<TRecord>
- Inherited Members
-
LoaderBase<TRecord, TProgress>.CreateProgressReport()LoaderBase<TRecord, TProgress>.IncrementCurrentItemCount()LoaderBase<TRecord, TProgress>.IncrementCurrentSkippedItemCount()LoaderBase<TRecord, TProgress>.ReportingIntervalLoaderBase<TRecord, TProgress>.CurrentItemCountLoaderBase<TRecord, TProgress>.CurrentSkippedItemCountLoaderBase<TRecord, TProgress>.MaximumItemCountLoaderBase<TRecord, TProgress>.SkipItemCount
Remarks
Two transaction modes are supported:
- Caller-managed — pass a DbTransaction to the constructor. The loader uses it but never commits or rolls back. The caller is responsible for transaction lifetime.
- Auto-managed — pass null for the transaction parameter. The loader creates its own transaction, commits on success, and rolls back on exception.
The caller owns the DbConnection lifetime — the loader does not
open, close, or dispose it. The connection must be open before calling
LoadAsync.
Constructors
DbLoader(DbConnection, string, DbTransaction?, ILogger<DbLoader<TRecord, TProgress>>?)
Initializes a new DbLoader<TRecord, TProgress> with a custom SQL command.
public DbLoader(DbConnection connection, string commandText, DbTransaction? transaction = null, ILogger<DbLoader<TRecord, TProgress>>? logger = null)
Parameters
connectionDbConnectionAn open DbConnection. The caller owns its lifetime.
commandTextstringThe SQL INSERT or UPDATE command to execute per record.
transactionDbTransactionAn optional DbTransaction. If null, the loader creates and manages its own transaction (commit on success, rollback on failure).
loggerILogger<DbLoader<TRecord, TProgress>>An optional logger for diagnostic output.
Exceptions
- ArgumentNullException
connectionorcommandTextis null.
DbLoader(DbConnection, WriteMode, DbTransaction?, ILogger<DbLoader<TRecord, TProgress>>?)
Initializes a new DbLoader<TRecord, TProgress> that auto-generates
an INSERT statement from [Table] and [Column] attributes on
TRecord.
public DbLoader(DbConnection connection, WriteMode writeMode, DbTransaction? transaction = null, ILogger<DbLoader<TRecord, TProgress>>? logger = null)
Parameters
connectionDbConnectionAn open DbConnection. The caller owns its lifetime.
writeModeWriteModeDetermines whether to generate INSERT or UPDATE commands.
transactionDbTransactionAn optional DbTransaction. If null, the loader creates and manages its own transaction.
loggerILogger<DbLoader<TRecord, TProgress>>An optional logger for diagnostic output.
Exceptions
- ArgumentNullException
connectionis null.- InvalidOperationException
TRecorddoes not have a[Table]attribute, orwriteModeis Update and no[Key]properties exist.
Properties
CommandText
The SQL command text being executed per record.
public string CommandText { get; }
Property Value
Methods
CreateProgressReport()
Creates a progress report of type TProgress. This gives the derived class the opportunity to implement a custom progress report that is specific to the loading process.
protected override TProgress CreateProgressReport()
Returns
- TProgress
Progress of type TProgress
CreateProgressTimer(IProgress<TProgress>)
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<TProgress> progress)
Parameters
progressIProgress<TProgress>The progress sink that will receive callbacks.
Returns
- IProgressTimer
A started Wolfgang.Etl.Abstractions.IProgressTimer instance.
LoadWorkerAsync(IAsyncEnumerable<TRecord>, CancellationToken)
This method is the core implementation of the loading logic and should be overridden by derived classes.
protected override Task LoadWorkerAsync(IAsyncEnumerable<TRecord> items, CancellationToken token)
Parameters
itemsIAsyncEnumerable<TRecord>The items to be loaded to the destination.
tokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task
A task representing the asynchronous operation.
Remarks
Items may be an empty sequence if no data is available or if the loading fails.
Exceptions
- ArgumentNullException
Argument items is null