Class Change<T>
Represents a single data change captured from a Change Data Capture (CDC) source.
public sealed record Change<T> : IEquatable<Change<T>>
Type Parameters
TThe type of the data entity that changed.
- Inheritance
-
Change<T>
- Implements
-
IEquatable<Change<T>>
- Inherited Members
- Extension Methods
Examples
// Processing changes from an IChangeDataCapture source
await foreach (var change in cdc.GetChangesAsync(lastPosition, cancellationToken))
{
switch (change.Type)
{
case ChangeType.Insert:
await HandleInsertAsync(change.After!);
break;
case ChangeType.Update:
await HandleUpdateAsync(change.Before, change.After!);
break;
case ChangeType.Delete:
await HandleDeleteAsync(change.Before!);
break;
}
// Checkpoint after processing
await stateStore.SavePositionAsync(change.Position);
}
Remarks
This record provides a provider-agnostic representation of a data change event. The generic type parameter allows strongly-typed access to the before/after data.
State Availability by Change Type:
| ChangeType | Before/After Availability |
|---|---|
| Insert | Before is null; After contains inserted data |
| Update | Before contains previous state (if supported); After contains new state |
| Delete | Before contains deleted data (if supported); After is null |
Before-Image Support: Not all providers support before-images for updates and deletes:
- SQL Server: Requires CDC "all columns" tracking mode
- Postgres: Requires REPLICA IDENTITY FULL or appropriate index
- MongoDB: Requires explicit pre-image recording (changeStreamPreAndPostImages)
- CosmosDB: Does not support before-images
- DynamoDB: Requires NEW_AND_OLD_IMAGES stream view type
Constructors
Change(ChangeType, T?, T?, ChangePosition, DateTimeOffset)
Represents a single data change captured from a Change Data Capture (CDC) source.
public Change(ChangeType Type, T? Before, T? After, ChangePosition Position, DateTimeOffset Timestamp)
Parameters
TypeChangeTypeThe type of change that occurred.
BeforeTThe state of the entity before the change, or null for inserts or when before-images are not available.
AfterTThe state of the entity after the change, or null for deletes.
PositionChangePositionThe position in the CDC stream where this change occurred. Used for checkpointing and resumption.
TimestampDateTimeOffsetThe timestamp when this change was committed to the source database.
Examples
// Processing changes from an IChangeDataCapture source
await foreach (var change in cdc.GetChangesAsync(lastPosition, cancellationToken))
{
switch (change.Type)
{
case ChangeType.Insert:
await HandleInsertAsync(change.After!);
break;
case ChangeType.Update:
await HandleUpdateAsync(change.Before, change.After!);
break;
case ChangeType.Delete:
await HandleDeleteAsync(change.Before!);
break;
}
// Checkpoint after processing
await stateStore.SavePositionAsync(change.Position);
}
Remarks
This record provides a provider-agnostic representation of a data change event. The generic type parameter allows strongly-typed access to the before/after data.
State Availability by Change Type:
| ChangeType | Before/After Availability |
|---|---|
| Insert | Before is null; After contains inserted data |
| Update | Before contains previous state (if supported); After contains new state |
| Delete | Before contains deleted data (if supported); After is null |
Before-Image Support: Not all providers support before-images for updates and deletes:
- SQL Server: Requires CDC "all columns" tracking mode
- Postgres: Requires REPLICA IDENTITY FULL or appropriate index
- MongoDB: Requires explicit pre-image recording (changeStreamPreAndPostImages)
- CosmosDB: Does not support before-images
- DynamoDB: Requires NEW_AND_OLD_IMAGES stream view type
Properties
After
The state of the entity after the change, or null for deletes.
public T? After { get; init; }
Property Value
- T
Before
The state of the entity before the change, or null for inserts or when before-images are not available.
public T? Before { get; init; }
Property Value
- T
HasAfterImage
Gets a value indicating whether this change includes after-image data.
public bool HasAfterImage { get; }
Property Value
HasBeforeImage
Gets a value indicating whether this change includes before-image data.
public bool HasBeforeImage { get; }
Property Value
Position
The position in the CDC stream where this change occurred. Used for checkpointing and resumption.
public ChangePosition Position { get; init; }
Property Value
Timestamp
The timestamp when this change was committed to the source database.
public DateTimeOffset Timestamp { get; init; }
Property Value
Type
The type of change that occurred.
public ChangeType Type { get; init; }
Property Value
Methods
GetKey<TKey>(Func<T, TKey>)
Gets the entity key or identifier from the available before/after data.
public TKey GetKey<TKey>(Func<T, TKey> keySelector)
Parameters
keySelectorFunc<T, TKey>A function that extracts the key from an entity.
Returns
Type Parameters
TKeyThe type of the entity key.