Skip to content

Chronicle camel case naming configuration

Configure camel case naming in the Chronicle .NET client when your stored event and read model properties need names such as firstName instead of FirstName. The same policy controls projection property mappings and read model container names.

By default, the .NET client preserves C# property names. CamelCaseNamingPolicy converts those names to camelCase during serialization and projection definition building, so the definitions refer to the names written to storage. It also camel-cases read model container names; both the default policy and the camel case policy pluralize those names by default.

Choose the policy before the client initializes. This is a client dependency, not an ASP.NET Core JSON setting.

When constructing ChronicleClient yourself, pass CamelCaseNamingPolicy through the named namingPolicy argument:

using Cratis.Chronicle;
using Cratis.Serialization;
public static class CamelCasingDirectClient
{
public static ChronicleClient Create() =>
new(options: new ChronicleOptions(), namingPolicy: new CamelCaseNamingPolicy());
}

View C# snippet source on GitHub

For an ASP.NET Core host, set the policy during Chronicle registration so the client created by dependency injection uses it.

In Program.cs, call WithCamelCaseNamingPolicy() in the configure callback. That callback receives the IChronicleBuilder for client dependencies:

using Microsoft.AspNetCore.Builder;
public static class CamelCasingAspNetCoreBasicRegistration
{
public static void Configure(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.AddCratisChronicle(
configure: chronicleBuilder => chronicleBuilder.WithCamelCaseNamingPolicy());
var app = builder.Build();
}
}

View C# snippet source on GitHub

Keep runtime settings in configureOptions and the naming policy in configure. Here, EventStore selects the store while the builder selects how its client names properties:

using Microsoft.AspNetCore.Builder;
public static class CamelCasingAspNetCoreWithOptionsRegistration
{
public static void Configure(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.AddCratisChronicle(
configureOptions: options => options.EventStore = "MyEventStore",
configure: chronicleBuilder => chronicleBuilder.WithCamelCaseNamingPolicy());
var app = builder.Build();
}
}

View C# snippet source on GitHub

Worker services and other non-web hosts use the same configure callback on IHostApplicationBuilder:

using Microsoft.Extensions.Hosting;
public static class CamelCasingWorkerHostRegistration
{
public static void Configure(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
builder.AddCratisChronicle(
configure: chronicleBuilder => chronicleBuilder.WithCamelCaseNamingPolicy());
var host = builder.Build();
}
}

View C# snippet source on GitHub

The policy connects the C# properties in a projection to their serialized names. Your C# types keep their original property names.

When the client builds a projection definition, it applies the policy to property mappings. A mapping for FirstName therefore refers to firstName in the stored data.

The read model container name identifies where a sink stores the model. With CamelCaseNamingPolicy configured, the CamelCasingUserReadModel type below produces the container name camelCasingUserReadModels.

The following event and read model use C# property names. The projection then maps between their serialized forms.

using Cratis.Chronicle.Events;
[EventType]
public record CamelCasingUserRegistered(
string FirstName,
string LastName,
string EmailAddress,
DateTime RegistrationDate);

View C# snippet source on GitHub

public class CamelCasingUserReadModel
{
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string EmailAddress { get; set; } = string.Empty;
public DateTime RegistrationDate { get; set; }
}

View C# snippet source on GitHub

The projection’s .From<CamelCasingUserRegistered>() uses automatic property mapping. With the policy configured, those mappings use camelCase names:

using Cratis.Chronicle.Projections;
public class CamelCasingUserProjection : IProjectionFor<CamelCasingUserReadModel>
{
public void Define(IProjectionBuilderFor<CamelCasingUserReadModel> builder) => builder
.From<CamelCasingUserRegistered>();
}

View C# snippet source on GitHub

The resulting read model properties are firstName, lastName, emailAddress, and registrationDate. These are the names to look for in the stored model.

The policy applies to the projections built by that Chronicle client. It does not change general ASP.NET Core JSON serialization; configure that separately if your HTTP responses also need camelCase.

Changing the policy changes property paths and can change container names. Treat a change on an existing store as a storage-contract change, not just a display preference.

Check the client that builds the projection. It must receive the policy before initialization, either through WithCamelCaseNamingPolicy() on its builder or through the constructor’s namingPolicy argument. Configuring one client does not change another client instance.

Compare the registered projection’s property paths with the stored model, including any custom mappings. After changing the policy, rebuild the projection definitions with the configured client. See Projections for the projection workflow, or Structural Dependencies to configure a custom naming policy.