Moving from CRUD and EF Core to Cratis Chronicle
Cratis Chronicle records events and derives current state from them, rather than replacing a row whenever something changes. If you use EF Core, the main adoption decision is which changes need that history and which can stay as ordinary CRUD: create, read, update, and delete operations. You can use both approaches in the same application.
The mental shift in one sentence
Section titled “The mental shift in one sentence”In CRUD, you store current state and update it; in Chronicle, you record what happened and derive current state from those facts. That derived state is a read model: data shaped for a query or screen, built from events instead of edited directly.
How the pieces map
Section titled “How the pieces map”| You know (CRUD / EF Core) | In Chronicle |
|---|---|
| An entity with an identity | An event source: the identity whose changes the events describe |
INSERT a row |
Append a “created” event |
UPDATE a column |
Append an event describing what changed (e.g. AddressChanged) |
DELETE a row |
Append a domain event such as CustomerRemoved; erasure is a separate compliance operation |
DbContext.SaveChanges() |
EventLog.Append(...) |
SELECT / LINQ query |
A query over a read model built by a projection |
| A computed/denormalized view | A purpose-built read model — make as many as you need |
ALTER TABLE / EF migration |
Use an event type migration when an event’s shape changes; rebuild read models when their projection needs it |
| Optimistic concurrency token | A concurrency scope or decision read that guards the append against intervening events |
What stays the same
Section titled “What stays the same”In a .NET application, you still use C# records and dependency injection. Your screens still query data and render results; they query read models rather than the event log itself. Keep EF Core and your existing database for data that only needs ordinary updates. Choosing Chronicle for one workflow does not require moving every table.
What changes (and why)
Section titled “What changes (and why)”A customer row tells you the address now. An AddressChanged event also records that a move happened, so you can later derive a relocation count or rebuild a view of those changes. Choose events that describe business facts, such as CustomerRegistered and AccountClosed, rather than recording every column assignment.
A projection defines how those events become a read model. By default, Chronicle materializes that model, meaning it stores the derived result for queries. Processing happens after the append, so a successful write does not mean the stored view already includes it. For read-after-write, you can wait for processing or use a suitable passive read model, computed from events on demand. Read model consistency explains those choices. A fresh read alone does not guard a later write against another request.
Removing a customer from a screen need not remove the customer’s history. Your projection can react to a removal event and remove its read-model entry. A closed account also needs an explicit closing rule if later appends must be refused. For obligations that require erasing personal data, use Compliance rather than treating a removal event as erasure.
Side by side
Section titled “Side by side”CRUD: update a row, then read it back. This is illustrative EF Core pseudocode, not a compiled example:
customer.Address = newAddress;await db.SaveChangesAsync();var current = await db.Customers.FindAsync(id);With Chronicle, the address change becomes an event to append. The examples below illustrate that write and its read model; client APIs differ, so use your chosen client’s setup guide before applying them. First, define the event types:
using Cratis.Chronicle.Events;
[EventType]public record CrudComparisonCustomerRegistered(string Name, string Address);
[EventType]public record CrudComparisonAddressChanged(string Address);import io.cratis.chronicle.events.EventType
@EventTypedata class CrudComparisonCustomerRegistered(val name: String, val address: String)
@EventTypedata class CrudComparisonAddressChanged(val address: String)import io.cratis.chronicle.events.EventType;
@EventTyperecord CrudComparisonCustomerRegistered(String name, String address) {}
@EventTyperecord CrudComparisonAddressChanged(String address) {}defmodule MyApp.Events.CrudComparisonCustomerRegistered do use Cratis.Chronicle.Events.EventType, id: "crud-comparison-customer-registered"
defstruct [:name, :address]end
defmodule MyApp.Events.CrudComparisonAddressChanged do use Cratis.Chronicle.Events.EventType, id: "crud-comparison-address-changed"
defstruct [:address]endimport { eventType } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class CrudComparisonCustomerRegistered { @field(String) readonly name: string; @field(String) readonly address: string;
constructor(name: string, address: string) { this.name = name; this.address = address; }}
@eventType()class CrudComparisonAddressChanged { @field(String) readonly address: string;
constructor(address: string) { this.address = address; }}Then define a customer card for the screen. The projection maps events onto that card and counts address changes. The result need not mirror the old Customer entity:
using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[FromEvent<CrudComparisonCustomerRegistered>][FromEvent<CrudComparisonAddressChanged>]public record CrudComparisonCustomerCard( [Key] Guid Id, string Name, string Address, [Count<CrudComparisonAddressChanged>] int TimesRelocated);import io.cratis.chronicle.projections.Countimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.FromEveryimport io.cratis.chronicle.projections.SetFromimport io.cratis.chronicle.readModels.ReadModel
@ReadModel@FromEvent(CrudComparisonCustomerRegistered::class)@FromEvent(CrudComparisonAddressChanged::class)data class CrudComparisonCustomerCard( @FromEvery(contextProperty = "eventSourceId") val id: String = "", val name: String = "", // AutoMap covers the registration; a relocation also needs its address mapped explicitly, // because an event with only a count mapping is not auto-mapped. @SetFrom("address", CrudComparisonAddressChanged::class) val address: String = "", @Count(CrudComparisonAddressChanged::class) val timesRelocated: Int = 0)import io.cratis.chronicle.projections.Count;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.FromEvery;import io.cratis.chronicle.projections.SetFrom;import io.cratis.chronicle.readModels.ReadModel;
@ReadModel@FromEvent(eventType = CrudComparisonCustomerRegistered.class)@FromEvent(eventType = CrudComparisonAddressChanged.class)class CrudComparisonCustomerCard { @FromEvery(contextProperty = "eventSourceId") public String id = ""; public String name = "";
// AutoMap covers the registration; a relocation also needs its address mapped explicitly, // because an event with only a count mapping is not auto-mapped. @SetFrom(propertyPath = "address", eventType = CrudComparisonAddressChanged.class) public String address = "";
@Count(eventType = CrudComparisonAddressChanged.class) public int timesRelocated = 0;}defmodule MyApp.ReadModels.CrudComparisonCustomerCard do use Cratis.Chronicle.ReadModels.ReadModel
alias MyApp.Events.{CrudComparisonCustomerRegistered, CrudComparisonAddressChanged}
defstruct id: nil, name: nil, address: nil, times_relocated: 0
from CrudComparisonCustomerRegistered, set: [id: :event_source_id, name: :name, address: :address]
from CrudComparisonAddressChanged, set: [address: :address], count: :times_relocatedendimport { count, fromEvent, Guid } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@fromEvent(CrudComparisonCustomerRegistered)@fromEvent(CrudComparisonAddressChanged)export class CrudComparisonCustomerCard { id: Guid = Guid.empty; @field(String) name = ''; @field(String) address = '';
@count(CrudComparisonAddressChanged) timesRelocated = 0;}In the .NET example, [FromEvent<T>] maps matching event properties onto the read model; TypeScript uses @fromEvent. The registration event supplies the name, and address-change events update the address. The count declaration retains something a row holding only the latest address would not tell you: how many changes occurred.
Now the write and the read-back:
using Cratis.Chronicle.Events;
public class CrudComparisonCustomerAddressUpdater(IEventStore eventStore){ public async Task<CrudComparisonCustomerCard> ChangeAddress(EventSourceId customerId, string newAddress) { await eventStore.EventLog.Append(customerId, new CrudComparisonAddressChanged(newAddress)); return await eventStore.ReadModels.GetInstanceById<CrudComparisonCustomerCard>(customerId); }}Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.Events.ComingFromCrudWriteAndReadAddressChanged do use Cratis.Chronicle.Events.EventType, id: "crud-comparison-write-and-read-address-changed"
defstruct [:address]end
defmodule MyApp.ReadModels.ComingFromCrudWriteAndReadCustomerCard do use Cratis.Chronicle.ReadModels.ReadModel
alias MyApp.Events.ComingFromCrudWriteAndReadAddressChanged
defstruct id: nil, address: nil
from ComingFromCrudWriteAndReadAddressChanged, set: [id: :event_source_id, address: :address]end
defmodule MyApp.ComingFromCrudCustomerAddressUpdater do alias MyApp.Events.ComingFromCrudWriteAndReadAddressChanged alias MyApp.ReadModels.ComingFromCrudWriteAndReadCustomerCard
def change_address(customer_id, new_address) do :ok = Cratis.Chronicle.append(customer_id, %ComingFromCrudWriteAndReadAddressChanged{ address: new_address })
Cratis.Chronicle.read_model(ComingFromCrudWriteAndReadCustomerCard, customer_id) endendimport { IEventStore } from '@cratis/chronicle';
class CrudComparisonCustomerAddressUpdater { constructor(private readonly store: IEventStore) {}
async changeAddress(customerId: string, newAddress: string): Promise<CrudComparisonCustomerCard | null> { await this.store.eventLog.append(customerId, new CrudComparisonAddressChanged(newAddress)); return this.store.readModels.findInstanceById(CrudComparisonCustomerCard, customerId); }}GetInstanceById retrieves the derived card, including TimesRelocated. For a materialized read model, that is the stored result, not a promise to replay the history on this call. Before relying on an immediate read-back, choose the consistency behavior that includes your append.
Not sure it’s worth it?
Section titled “Not sure it’s worth it?”Start with a workflow where the sequence of changes answers a question the current row cannot. If you only need the latest value and have no use for replay or history, CRUD may remain the simpler choice. When to use event sourcing helps you make that decision before changing the storage model.
- Get started to run the kernel and connect your chosen client.
- Follow the tutorial to build an event-driven workflow and its read side.