Skip to content

Cratis Chronicle reducer tags for organization

Cratis Chronicle reducer tags describe a reducer’s purpose or domain so related definitions share recognizable labels. Use them to organize reducers without changing which events update their read models.

In .NET, [Tag] and [Tags] label the reducer class. For a self-reducing read model, put the labels on the model instead. Neither form filters incoming events.

Kotlin and Java use repeatable @Tag annotations. TypeScript uses @tag(...) or @tags(...) on the reducer class. These labels are sent with the reducer’s observer registration, the definition that tells Chronicle what the reducer observes.

If your goal is to limit delivery, use Filter reducers and reactors by tag, Filter reducers and reactors by event source type, or Filter reducers and reactors by event stream type.

Choose the form that makes the reducer’s labels easiest to read. In .NET, [Tag] and [Tags] can each carry several names, and repeated attributes contribute to the same set of labels.

Apply a single tag to your reducer:

using Cratis.Chronicle;
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Reducers;
[EventType]
public record TaggingReducersOrderPlaced(decimal TotalAmount);
public record TaggingReducersOrderAnalytics(int OrderCount, decimal TotalAmount);
[Tag("Analytics")]
public class TaggingReducersOrderAnalyticsReducer : IReducerFor<TaggingReducersOrderAnalytics>
{
public TaggingReducersOrderAnalytics Placed(TaggingReducersOrderPlaced @event, TaggingReducersOrderAnalytics? current, EventContext context) =>
new((current?.OrderCount ?? 0) + 1, (current?.TotalAmount ?? 0m) + @event.TotalAmount);
}

View C# snippet source on GitHub

Pass several tag names to one attribute when they belong together:

using Cratis.Chronicle;
using Cratis.Chronicle.Reducers;
public record TaggingReducersSalesReport(decimal TotalSales);
[Tag("Analytics", "Reporting", "Dashboard")]
public class TaggingReducersSalesReportReducer : IReducerFor<TaggingReducersSalesReport>;

View C# snippet source on GitHub

Apply multiple [Tag] attributes:

using Cratis.Chronicle;
using Cratis.Chronicle.Reducers;
public record TaggingReducersComplianceReport(string Status);
[Tag("Analytics")]
[Tag("Compliance")]
[Tag("Auditing")]
public class TaggingReducersComplianceReportReducer : IReducerFor<TaggingReducersComplianceReport>;

View C# snippet source on GitHub

Combine both approaches:

using Cratis.Chronicle;
using Cratis.Chronicle.Reducers;
public record TaggingReducersExecutiveDashboard(int MetricCount);
[Tag("Analytics", "Reporting")]
[Tag("Executive")]
public class TaggingReducersExecutiveDashboardReducer : IReducerFor<TaggingReducersExecutiveDashboard>;

View C# snippet source on GitHub

Choose names that a teammate would use to find related reducers, such as a business domain or a reporting purpose. Reuse the same spelling across those definitions; near-duplicate names split a group that readers expect to be together.

Keep a tag only if it adds a useful distinction. Adding every possible category makes the labels harder to scan without making the reducer’s role clearer.

The following examples label reducers by their role, such as analytics or compliance. Use them as naming examples, not as categories Chronicle requires:

using Cratis.Chronicle;
using Cratis.Chronicle.Reducers;
public record TaggingReducersCategoryExamples(Guid Id);
// By domain
[Tag("Sales", "Inventory", "Customer")]
// By purpose
[Tag("Analytics", "Reporting", "Dashboard", "Auditing")]
// By stakeholder
[Tag("Executive", "Operations", "Finance")]
// By data type
[Tag("Aggregates", "Summaries", "Metrics")]
public class TaggingReducersCategoryExamplesReducer : IReducerFor<TaggingReducersCategoryExamples>;

View C# snippet source on GitHub

Reducer tags are stored with the reducer definition. They do not activate a reducer, select it for execution, or change its read model’s contents.

To make a read model include only tagged events, use an event filter instead. Continue with Filtering by tag for the delivery rule and append-time metadata it matches.