Skip to content

PulseSharp.Abstractions ​

Contracts for handlers and libraries. No dependencies.

Generated from the XML documentation of PulseSharp.Abstractions.

DailyStreamName ​

csharp
public abstract record DailyStreamName

A stream that starts again every day: {Category}-{yyyy-MM-dd}.

ParameterDescription
DateThe day of the stream.

Use it for app-wide events that are not about one entity, such as settings or navigation changes. Without a daily split, such a stream would grow for the whole life of the app.

Example

csharp
public sealed record SettingsStream(DateOnly Date) : DailyStreamName(Date)
{
    protected override string Category
    {
        get
        {
            return "Settings";
        }
    }
}

DailyStreamName(DateOnly) ​

A stream that starts again every day: {Category}-{yyyy-MM-dd}.

ParameterDescription
DateThe day of the stream.

Use it for app-wide events that are not about one entity, such as settings or navigation changes. Without a daily split, such a stream would grow for the whole life of the app.

Example

csharp
public sealed record SettingsStream(DateOnly Date) : DailyStreamName(Date)
{
    protected override string Category
    {
        get
        {
            return "Settings";
        }
    }
}

Date ​

The day of the stream.

Category ​

The part of the name before the date, for example Settings.

Value() ​

Returns {Category}-{yyyy-MM-dd}.

Returns: The stream name.

EmitOptions ​

csharp
public sealed class EmitOptions

Options for IPulse.Emit.

ExpectedVersion ​

What the stream must look like for the emit to succeed. The default is ExpectedVersion.Any.

Transaction ​

A database transaction to save the event in. The event is then saved together with your own data, and handlers receive it only after the transaction commits. Stores that do not use a database ignore it.

EmitResult ​

csharp
public readonly record struct EmitResult

The result of emitting an event.

ParameterDescription
EventIdThe globally unique id of the event.
StreamPositionThe position of the event in its stream, starting at 1.

EmitResult(Guid, long) ​

The result of emitting an event.

ParameterDescription
EventIdThe globally unique id of the event.
StreamPositionThe position of the event in its stream, starting at 1.

EventId ​

The globally unique id of the event.

StreamPosition ​

The position of the event in its stream, starting at 1.

EnqueueOptions ​

csharp
public sealed class EnqueueOptions

Options for IPulse.Enqueue.

RunAt ​

The earliest time the job may run. When it is not set, the job runs as soon as a worker is free.

Transaction ​

A database transaction to save the job in. The job is then saved together with your own data, and runs only after the transaction commits. Stores that do not use a database ignore it.

EventContext ​

csharp
public sealed class EventContext

Information about the event a handler is receiving.

EventId ​

The globally unique id of the event. It stays the same when the event is synced to another node.

EventName ​

The stored name of the event type.

Stream ​

The name of the stream the event belongs to.

StreamPosition ​

The position of the event in its stream, starting at 1.

Origin ​

The node that emitted the event, for example till-1 or cloud.

EmittedAt ​

When the event was emitted.

HandlerName ​

The stable name of the handler receiving the event.

Attempt ​

The attempt number, starting at 1. Higher numbers mean earlier attempts failed.

EventNameAttribute ​

csharp
public sealed class EventNameAttribute

Sets the stored name of an event type. Without it, the class name is used.

Stored events are matched to their C# type by this name. Pin it before you rename or move the class, and stored events still load.

EventNameAttribute(string) ​

Sets the stored name of the event type.

ParameterDescription
nameThe name, for example sale-completed.

Throws ArgumentException when name is empty or only whitespace.

Name ​

The stored name of the event type.

ExpectedVersion ​

csharp
public readonly record struct ExpectedVersion

What the stream must look like for an emit to succeed. Use it to make sure nobody else changed the stream since you read it (optimistic concurrency).

The default value is ExpectedVersion.Any: no check.

Any ​

No check. The event is always added.

NoStream ​

The stream must not have any events yet.

StreamExists ​

The stream must already have at least one event.

Kind ​

The kind of check.

Position ​

For ExpectedVersionKind.Exactly, the position the last event must have. Otherwise 0.

Allows(long) ​

Checks this expectation against the stream as it is now. Stores call it while appending.

ParameterDescription
currentPositionThe position of the last event in the stream, or 0 when the stream has no events.

Returns: true when an event may be appended.

Exactly(long) ​

The last event of the stream must be at exactly position.

ParameterDescription
positionThe expected position of the last event, starting at 1.

Returns: The expected version.

Throws ArgumentOutOfRangeException when position is less than 1.

ExpectedVersionKind ​

csharp
public enum ExpectedVersionKind

The kind of check an ExpectedVersion makes.

Any ​

No check. The event is always added.

NoStream ​

The stream must not have any events yet.

StreamExists ​

The stream must already have at least one event.

Exactly ​

The last event of the stream must be at exactly ExpectedVersion.Position.

ExpectedVersionMismatchException ​

csharp
public sealed class ExpectedVersionMismatchException

Thrown by IPulse.Emit when the stream does not match EmitOptions.ExpectedVersion. Someone else changed the stream: read it again and retry.

ExpectedVersionMismatchException(string, ExpectedVersion, long) ​

Creates the exception.

ParameterDescription
streamThe stream name.
expectedThe expected version that was not met.
actualPositionThe position of the last event in the stream, or 0 when the stream has no events.

Stream ​

The stream name.

Expected ​

The expected version that was not met.

ActualPosition ​

The position of the last event in the stream, or 0 when the stream has no events.

HandlerNameAttribute ​

csharp
public sealed class HandlerNameAttribute

Sets the stable name of an event handler class. Without it, the class name is used.

PulseSharp stores how far each handler has got under this name. Pin it before you rename or move the class, and the stored progress is kept.

HandlerNameAttribute(string) ​

Sets the stable name of the handler.

ParameterDescription
nameThe name, for example update-stock.

Throws ArgumentException when name is empty or only whitespace.

Name ​

The stable name of the handler.

IEventHandler ​

csharp
public interface IEventHandler

Reacts to events of type TEvent.

Type parameterDescription
TEventThe event type.

Each handler has a stable name: the class name, or the name in HandlerNameAttribute. PulseSharp stores how far each handler has got under that name, so pin the name before renaming the class. Delivery is at-least-once: after a crash at the wrong moment a handler can see the same event again, so make handlers safe to repeat.

Handle(, EventContext, CancellationToken) ​

Handles one event. Events of one stream arrive in order, one at a time. Throwing an exception makes PulseSharp retry the event later.

ParameterDescription
eventThe event.
contextWhere the event came from and which attempt this is.
cancellationTokenSignals that the app is stopping.

Returns: A task that completes when the event is handled.

IJobHandler ​

csharp
public interface IJobHandler

Runs jobs of type TJob. Each job type has exactly one handler.

Type parameterDescription
TJobThe job type.

If the app stops while a job is running, the job runs again later, so make handlers safe to repeat.

Handle(, JobContext, CancellationToken) ​

Runs one job. Throwing an exception makes PulseSharp retry the job later.

ParameterDescription
jobThe job.
contextReports progress and tells which attempt this is.
cancellationTokenSignals that the job was cancelled or that the app is stopping.

Returns: A task that completes when the job is done.

IPulse ​

csharp
public interface IPulse

The entry point for emitting events and enqueuing jobs.

Emit<TEvent>(StreamName, TEvent, CancellationToken) ​

Saves an event to a stream. Once this method returns, the event is stored and every handler of TEvent will receive it.

Type parameterDescription
TEventThe event type. Its stored name is the class name, or the name in EventNameAttribute.
ParameterDescription
streamThe stream the event belongs to.
eventThe event.
cancellationTokenCancels the save.

Returns: The id of the event and its position in the stream.

Emit<TEvent>(StreamName, TEvent, EmitOptions, CancellationToken) ​

Saves an event to a stream, with an expected-version check or inside a database transaction.

Type parameterDescription
TEventThe event type. Its stored name is the class name, or the name in EventNameAttribute.
ParameterDescription
streamThe stream the event belongs to.
eventThe event.
optionsThe expected version and the optional transaction.
cancellationTokenCancels the save.

Returns: The id of the event and its position in the stream.

EmitAll(StreamName, IReadOnlyList<object>, CancellationToken) ​

Saves several events to one stream in one commit: all of them are saved, in the order given, or none. This is also much faster than emitting them one by one. Every handler receives each event as usual.

ParameterDescription
streamThe stream the events belong to.
eventsThe events. They may be of different types. There must be at least one.
cancellationTokenCancels the save.

Returns: The id and stream position of the last event.

Throws ArgumentException when the list is empty or has a null event.

EmitAll(StreamName, IReadOnlyList<object>, EmitOptions, CancellationToken) ​

Saves several events to one stream in one commit, with an expected-version check or inside a database transaction. The check and the transaction apply to the whole group.

ParameterDescription
streamThe stream the events belong to.
eventsThe events. They may be of different types. There must be at least one.
optionsThe expected version (of the stream before the group) and the optional transaction.
cancellationTokenCancels the save.

Returns: The id and stream position of the last event.

Throws ArgumentException when the list is empty or has a null event.

Throws ExpectedVersionMismatchException when the stream is not at the expected version. Nothing is saved.

Enqueue<TJob>(TJob, CancellationToken) ​

Saves a job. One worker runs it with the IJobHandler for TJob.

Type parameterDescription
TJobThe job type. Its stored name is the class name, or the name in JobNameAttribute.
ParameterDescription
jobThe job.
cancellationTokenCancels the save.

Returns: The id of the job.

Enqueue<TJob>(TJob, EnqueueOptions, CancellationToken) ​

Saves a job that runs later, or saves it inside a database transaction.

Type parameterDescription
TJobThe job type. Its stored name is the class name, or the name in JobNameAttribute.
ParameterDescription
jobThe job.
optionsWhen to run the job, and the optional transaction.
cancellationTokenCancels the save.

Returns: The id of the job.

JobContext ​

csharp
public abstract class JobContext

Information about the running job, and a way to report its progress.

JobId ​

The id returned by IPulse.Enqueue.

JobName ​

The stored name of the job type.

Attempt ​

The attempt number, starting at 1. Higher numbers mean earlier attempts failed.

Report(int, int, CancellationToken) ​

Saves the job's progress so that any app instance can show it. Call it every few items, not for every item.

ParameterDescription
doneHow many items are finished.
totalHow many items there are in total.
cancellationTokenCancels the save.

Returns: A task that completes when the progress is saved.

JobNameAttribute ​

csharp
public sealed class JobNameAttribute

Sets the stored name of a job type. Without it, the class name is used.

Stored jobs are matched to their C# type by this name. Pin it before you rename or move the class, and waiting jobs still run.

JobNameAttribute(string) ​

Sets the stored name of the job type.

ParameterDescription
nameThe name, for example import-products.

Throws ArgumentException when name is empty or only whitespace.

Name ​

The stored name of the job type.

PulseExtensions ​

csharp
public static class PulseExtensions

Short ways to emit events and enqueue jobs.

Emit<TEvent>(IPulse, TEvent, CancellationToken) ​

Saves an event to the stream named after its type (for example SaleCompleted), so events of one type reach each handler in the order they were emitted.

Type parameterDescription
TEventThe event type. The stream name is the class name, or the name in EventNameAttribute.
ParameterDescription
pulseThe pulse.
eventThe event.
cancellationTokenCancels the save.

Returns: The id of the event and its position in the stream.

While one event keeps failing in a handler, later events of the same type wait for it. When events are independent (one sale has nothing to do with another), give each item its own stream instead: pulse.Emit("Sale-" + sale.Id, sale).

Example

csharp
await pulse.Emit(new SaleCompleted(saleId, 25m));

EnqueueIn<TJob>(IPulse, TJob, TimeSpan, CancellationToken) ​

Saves a job that runs after a delay.

Type parameterDescription
TJobThe job type.
ParameterDescription
pulseThe pulse.
jobThe job.
delayHow long to wait before the job may run.
cancellationTokenCancels the save.

Returns: The id of the job.

Example

csharp
await pulse.EnqueueIn(new SendReminder(customerId), TimeSpan.FromHours(1));

EnqueueAt<TJob>(IPulse, TJob, DateTimeOffset, CancellationToken) ​

Saves a job that runs at a given time.

Type parameterDescription
TJobThe job type.
ParameterDescription
pulseThe pulse.
jobThe job.
runAtThe earliest time the job may run.
cancellationTokenCancels the save.

Returns: The id of the job.

Example

csharp
await pulse.EnqueueAt(new SendDailyReport(), tomorrowAtNine);

EmitAll(IPulse, StreamName, object[]) ​

Saves several events to one stream in one commit: all of them, in the order given, or none.

ParameterDescription
pulseThe pulse.
streamThe stream the events belong to.
eventsThe events. They may be of different types.

Returns: The id and stream position of the last event.

Example

csharp
await pulse.EmitAll("Menu", new ProductAdded("tea", 2.50m), new ProductAdded("coffee", 3.20m));

StreamName ​

csharp
public abstract record StreamName

The name of a stream: an ordered list of events. Events of one stream reach each handler in order.

By convention a stream name is {Category}-{Id}, for example Sale-7f3a. Derive one record per kind of stream, or use StreamName.From for a plain name.

Example

csharp
public sealed record SaleStream(Guid SaleId) : StreamName
{
    public override string Value()
    {
        return $"Sale-{SaleId}";
    }
}

Value() ​

Returns the stream name as it is stored.

Returns: The stream name.

From(string) ​

Creates a stream name from a plain string.

ParameterDescription
valueThe stream name, for example Sale-7f3a.

Returns: The stream name.

Throws ArgumentException when value is empty or only whitespace.

ToString() ​

Returns the stream name as it is stored.

Returns: The stream name.

Released under the MIT License.