Audiences
An audience is the named tag audience. It says who an event is addressed to, and it is what a subscription tag filter matches on. The name is available as WellKnownTags.Audience.
Stamping the audience from a property
Section titled “Stamping the audience from a property”Mark a property of the event with [Audience]. Its value, or the underlying value of a concept, becomes the audience tag.
using Cratis.Chronicle;using Cratis.Chronicle.Events;
[EventType][Public]public record AudiencesInvitationIssued([property: Audience] string Lobby, string Email);Stamping the audience from the surroundings
Section titled “Stamping the audience from the surroundings”EventTags.Audience(value) opens an ambient scope; events appended inside it are addressed to that audience. Open it twice (nested) to address two audiences. The ambient audience is only stamped on events appended to the outbox, or on events whose type is marked [Audience], so it never leaks onto ordinary event log events.
Requiring an audience
Section titled “Requiring an audience”Mark the event type with [Audience] to make an audience mandatory. Appending it without an audience tag, from whatever source, fails with MissingAudience. In a unit of work the failure happens when the event is added, not at commit.
using Cratis.Chronicle;using Cratis.Chronicle.Events;
[EventType][Public][Audience]public record AudiencesRegistrationCompleted(string Organization);
public static class AudiencesRequired{ public static async Task Complete(IEventStore eventStore) { var outbox = eventStore.GetEventSequence(Cratis.Chronicle.EventSequences.EventSequenceId.Outbox);
// Throws MissingAudience: the event type requires an audience and none was provided. // await outbox.Append("org-1", new AudiencesRegistrationCompleted("Acme"));
using var scope = EventTags.Audience("lobby-a"); await outbox.Append("org-1", new AudiencesRegistrationCompleted("Acme")); }}Without the attribute on the type, an audience is optional; nothing in Chronicle forces events to have one.