Skip to content

CQRS with Anvil

Anvil fits CQRS because controllers only call commands and queries. The command and query implementation stays in your application packages.

type CreateProject struct {
Store project.WriteStore
}
func (h CreateProject) Run(ctx context.Context, input CreateProjectInput) (project.Project, error) {
return h.Store.Create(ctx, input)
}
type GetProject struct {
ReadModel project.ReadModel
}
func (h GetProject) Run(ctx context.Context, id project.ID) (project.View, error) {
return h.ReadModel.Find(ctx, id)
}
type Projects struct {
sdk.Controller `path:"/projects"`
CreateProject commands.CreateProject
GetProject queries.GetProject
}

The generated DI container wires the command and query handlers like any other dependency. Anvil does not require CQRS, but it does not fight it.