Skip to content

CHR0016: Projection Define() must not contain imperative code

The Define() method of a class implementing IProjectionFor<T> must not contain imperative statements such as if/else, loops, variable declarations, or assignments. Projection definitions only declare the shape; they do not execute.

Error

using Cratis.Chronicle.Projections;
public class UserReadModel { }
public class UserProjection : IProjectionFor<UserReadModel>
{
public void Define(IProjectionBuilderFor<UserReadModel> builder)
{
if (someCondition)
{
builder.From<UserRegistered>(_ => _);
}
}
}
using Cratis.Chronicle.Projections;
public class UserReadModel { }
public class UserProjection : IProjectionFor<UserReadModel>
{
public void Define(IProjectionBuilderFor<UserReadModel> builder) =>
builder.From<UserRegistered>(_ => _);
}

The Define() method is called once at registration time to declare the projection shape. Chronicle serializes the resulting definition and sends it to the server for use during both live processing and replay. Any imperative code such as conditionals or loops creates non-determinism: different runs could produce different projection definitions, breaking replay consistency.

  • CHR0015: Projection must not have side effects
  • CHR0018: Constraint Define() must not contain imperative code