PulseSharp.Abstractions
Contracts for handlers and libraries. No dependencies.
Generated from the XML documentation of
PulseSharp.Abstractions.
DailyStreamName
public abstract record DailyStreamNameA stream that starts again every day: {Category}-{yyyy-MM-dd}.
| Parameter | Description |
|---|---|
Date | The 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
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}.
| Parameter | Description |
|---|---|
Date | The 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
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
public sealed class EmitOptionsOptions 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
public readonly record struct EmitResultThe result of emitting an event.
| Parameter | Description |
|---|---|
EventId | The globally unique id of the event. |
StreamPosition | The position of the event in its stream, starting at 1. |
EmitResult(Guid, long)
The result of emitting an event.
| Parameter | Description |
|---|---|
EventId | The globally unique id of the event. |
StreamPosition | The 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
public sealed class EnqueueOptionsOptions 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
public sealed class EventContextInformation 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
public sealed class EventNameAttributeSets 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.
| Parameter | Description |
|---|---|
name | The name, for example sale-completed. |
Throws ArgumentException when name is empty or only whitespace.
Name
The stored name of the event type.
ExpectedVersion
public readonly record struct ExpectedVersionWhat 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.
| Parameter | Description |
|---|---|
currentPosition | The 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.
| Parameter | Description |
|---|---|
position | The expected position of the last event, starting at 1. |
Returns: The expected version.
Throws ArgumentOutOfRangeException when position is less than 1.
ExpectedVersionKind
public enum ExpectedVersionKindThe 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
public sealed class ExpectedVersionMismatchExceptionThrown 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.
| Parameter | Description |
|---|---|
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. |
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
public sealed class HandlerNameAttributeSets 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.
| Parameter | Description |
|---|---|
name | The name, for example update-stock. |
Throws ArgumentException when name is empty or only whitespace.
Name
The stable name of the handler.
IEventHandler
public interface IEventHandlerReacts to events of type TEvent.
| Type parameter | Description |
|---|---|
TEvent | The 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.
| Parameter | Description |
|---|---|
event | The event. |
context | Where the event came from and which attempt this is. |
cancellationToken | Signals that the app is stopping. |
Returns: A task that completes when the event is handled.
IJobHandler
public interface IJobHandlerRuns jobs of type TJob. Each job type has exactly one handler.
| Type parameter | Description |
|---|---|
TJob | The 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.
| Parameter | Description |
|---|---|
job | The job. |
context | Reports progress and tells which attempt this is. |
cancellationToken | Signals that the job was cancelled or that the app is stopping. |
Returns: A task that completes when the job is done.
IPulse
public interface IPulseThe 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 parameter | Description |
|---|---|
TEvent | The event type. Its stored name is the class name, or the name in EventNameAttribute. |
| Parameter | Description |
|---|---|
stream | The stream the event belongs to. |
event | The event. |
cancellationToken | Cancels 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 parameter | Description |
|---|---|
TEvent | The event type. Its stored name is the class name, or the name in EventNameAttribute. |
| Parameter | Description |
|---|---|
stream | The stream the event belongs to. |
event | The event. |
options | The expected version and the optional transaction. |
cancellationToken | Cancels 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.
| Parameter | Description |
|---|---|
stream | The stream the events belong to. |
events | The events. They may be of different types. There must be at least one. |
cancellationToken | Cancels 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.
| Parameter | Description |
|---|---|
stream | The stream the events belong to. |
events | The events. They may be of different types. There must be at least one. |
options | The expected version (of the stream before the group) and the optional transaction. |
cancellationToken | Cancels 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 parameter | Description |
|---|---|
TJob | The job type. Its stored name is the class name, or the name in JobNameAttribute. |
| Parameter | Description |
|---|---|
job | The job. |
cancellationToken | Cancels 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 parameter | Description |
|---|---|
TJob | The job type. Its stored name is the class name, or the name in JobNameAttribute. |
| Parameter | Description |
|---|---|
job | The job. |
options | When to run the job, and the optional transaction. |
cancellationToken | Cancels the save. |
Returns: The id of the job.
JobContext
public abstract class JobContextInformation 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.
| Parameter | Description |
|---|---|
done | How many items are finished. |
total | How many items there are in total. |
cancellationToken | Cancels the save. |
Returns: A task that completes when the progress is saved.
JobNameAttribute
public sealed class JobNameAttributeSets 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.
| Parameter | Description |
|---|---|
name | The name, for example import-products. |
Throws ArgumentException when name is empty or only whitespace.
Name
The stored name of the job type.
PulseExtensions
public static class PulseExtensionsShort 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 parameter | Description |
|---|---|
TEvent | The event type. The stream name is the class name, or the name in EventNameAttribute. |
| Parameter | Description |
|---|---|
pulse | The pulse. |
event | The event. |
cancellationToken | Cancels 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
await pulse.Emit(new SaleCompleted(saleId, 25m));EnqueueIn<TJob>(IPulse, TJob, TimeSpan, CancellationToken)
Saves a job that runs after a delay.
| Type parameter | Description |
|---|---|
TJob | The job type. |
| Parameter | Description |
|---|---|
pulse | The pulse. |
job | The job. |
delay | How long to wait before the job may run. |
cancellationToken | Cancels the save. |
Returns: The id of the job.
Example
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 parameter | Description |
|---|---|
TJob | The job type. |
| Parameter | Description |
|---|---|
pulse | The pulse. |
job | The job. |
runAt | The earliest time the job may run. |
cancellationToken | Cancels the save. |
Returns: The id of the job.
Example
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.
| Parameter | Description |
|---|---|
pulse | The pulse. |
stream | The stream the events belong to. |
events | The events. They may be of different types. |
Returns: The id and stream position of the last event.
Example
await pulse.EmitAll("Menu", new ProductAdded("tea", 2.50m), new ProductAdded("coffee", 3.20m));StreamName
public abstract record StreamNameThe 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
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.
| Parameter | Description |
|---|---|
value | The 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.