with one click
domain
// Create a new domain bounded context with aggregates, commands, events, and handlers
// Create a new domain bounded context with aggregates, commands, events, and handlers
Plan a new feature end-to-end ā impact analysis across all layers before delegating to /domain, /read-model, /controller skills
Create atomic git commits following project conventions
Build a new read model following project conventions (event handlers, tests, migration, configuration)
Upgrade Rails framework to a newer version following the smooth upgrade methodology
Upgrade RailsEventStore (RES) gems to a newer version
Scaffold a new Rails app with event sourcing, following project conventions (todomvc as reference)
| name | domain |
| description | Create a new domain bounded context with aggregates, commands, events, and handlers |
Use this skill when asked to create a new domain module (bounded context) in the domains/ directory, or to add new aggregates/commands/events to an existing domain.
Two patterns exist in the codebase:
lib/{domain}/todo.rbUse the structured pattern for any non-trivial domain. The simple pattern is only for demos.
Before writing any code, clarify:
project_management, ticketing)Customer, Deal, Contact)Create the directory structure:
domains/{domain_name}/
āāā Gemfile
āāā Makefile
āāā .mutant.yml
āāā lib/
ā āāā {domain_name}.rb # Module entry point + Configuration
ā āāā {domain_name}/
ā āāā commands/
ā ā āāā {command_name}.rb # One file per command
ā āāā events/
ā ā āāā {event_name}.rb # One file per event
ā āāā {aggregate_name}.rb # Aggregate root
ā āāā {aggregate_name}_service.rb # Command handlers
āāā test/
āāā test_helper.rb
āāā {test_name}_test.rb # One file per test concern
Gemfile:
source "https://rubygems.org"
eval_gemfile "../../infra/Gemfile.test"
gem "infra", path: "../../infra"
Makefile:
install:
@bundle install
test:
@bundle exec ruby -e "require \"rake/rake_test_loader\"" test/*_test.rb
mutate:
@RAILS_ENV=test bundle exec mutant run
.PHONY: install test mutate
.mutant.yml:
requires:
- ./test/test_helper
integration: minitest
usage: opensource
coverage_criteria:
process_abort: true
matcher:
subjects:
- {DomainModule}*
ignore:
- {DomainModule}::Configuration#call
Create test/test_helper.rb:
require "minitest/autorun"
require "mutant/minitest/coverage"
require_relative "../lib/{domain_name}"
module DomainModule
class Test < Infra::InMemoryTest
def before_setup
super
Configuration.new.call(event_store, command_bus)
end
private
def some_helper(id, attribute)
run_command(SomeCommand.new(entity_id: id, attribute: attribute))
end
end
end
Create test files ā one per logical concern (e.g. test/registration_test.rb, test/assignment_test.rb):
require_relative "test_helper"
module DomainModule
class SomeTest < Test
cover "DomainModule*"
def test_entity_can_be_created
id = SecureRandom.uuid
create_entity(id, "some value")
expected_event = EntityCreated.new(data: { entity_id: id, attribute: "some value" })
assert_events("DomainModule::Entity$#{id}", expected_event) do
create_entity(id, "some value")
end
end
def test_cannot_create_same_entity_twice
id = SecureRandom.uuid
create_entity(id, "value")
assert_raises(Entity::AlreadyExists) do
create_entity(id, "value")
end
end
private
def create_entity(id, value)
run_command(CreateEntity.new(entity_id: id, attribute: value))
end
end
end
Test conventions:
cover "DomainModule*" for mutation testingrun_command(...) to dispatch commands (provided by Infra::InMemoryTest)assert_events(stream, expected_event) { block } to verify eventsassert_raises(ErrorClass) { block } for invariant violationsOne file per command in lib/{domain_name}/commands/:
module DomainModule
class CreateEntity < Infra::Command
attribute :entity_id, Infra::Types::UUID
attribute :name, Infra::Types::String
alias aggregate_id entity_id
end
end
Command conventions:
Infra::CommandInfra::Types::UUID for UUIDs, Infra::Types::String for stringsalias aggregate_id {entity_id_field} so the handler can use command.aggregate_idOne file per event in lib/{domain_name}/events/:
module DomainModule
class EntityCreated < Infra::Event
attribute :entity_id, Infra::Types::UUID
attribute :name, Infra::Types::String
end
end
Event conventions:
Infra::EventCreated, Updated, Assigned, Promoted)module DomainModule
class Entity
include AggregateRoot
AlreadyExists = Class.new(StandardError)
NotFound = Class.new(StandardError)
def initialize(id)
@id = id
end
def create(name)
raise AlreadyExists if @created
apply EntityCreated.new(data: { entity_id: @id, name: name })
end
def update_name(name)
raise NotFound unless @created
apply EntityNameUpdated.new(data: { entity_id: @id, name: name })
end
private
on EntityCreated do |event|
@created = true
end
on EntityNameUpdated do |event|
end
end
end
Aggregate conventions:
include AggregateRoot@created, @completed, etc.)raise before applyon EventClass do |event| blocks update internal stateon blocksClass.new(StandardError)One handler class per command, grouped in a service file:
module DomainModule
class OnCreateEntity
def initialize(event_store)
@repository = Infra::AggregateRootRepository.new(event_store)
end
def call(command)
@repository.with_aggregate(Entity, command.aggregate_id) do |entity|
entity.create(command.name)
end
end
end
class OnUpdateEntityName
def initialize(event_store)
@repository = Infra::AggregateRootRepository.new(event_store)
end
def call(command)
@repository.with_aggregate(Entity, command.aggregate_id) do |entity|
entity.update_name(command.name)
end
end
end
end
Handler conventions:
@repository.with_aggregate(AggregateClass, id) { |agg| ... }OnRegistration, OnSetCustomer, OnPromoteCustomerToVip_service.rb file per aggregate, or separate files for clarityCreate lib/{domain_name}.rb:
require "infra"
require_relative "{domain_name}/commands/create_entity"
require_relative "{domain_name}/commands/update_entity_name"
require_relative "{domain_name}/events/entity_created"
require_relative "{domain_name}/events/entity_name_updated"
require_relative "{domain_name}/entity_service"
require_relative "{domain_name}/entity"
module DomainModule
class Configuration
def call(event_store, command_bus)
command_bus.register(CreateEntity, OnCreateEntity.new(event_store))
command_bus.register(UpdateEntityName, OnUpdateEntityName.new(event_store))
end
end
end
Configuration conventions:
require "infra" first, then all domain filesevent_store in constructorcd domains/{domain_name}
bundle install
make test # All tests pass
make mutate # 100% mutation score
Then from the project root:
make test # Ensure nothing is broken globally
Keep aggregates as small as possible. An aggregate is often a small state machine with two or three states. Adding new methods to an existing aggregate is a smell ā it signals a cohesion problem. Before adding a method, ask: "Is this really the same concept, or is it a new aggregate?"
Associations between entities are often aggregates on their own. For example, "Contact assigned to Company" is not a method on Contact ā it's a separate ContactCompanyAssignment aggregate with its own lifecycle. The Contact aggregate handles contact registration and attributes. The assignment is a different concern.
Passing strings or "data" into aggregate methods is a smell. Aggregate methods should ideally receive only IDs (UUIDs). If you're passing names, descriptions, or other data, consider whether the aggregate is doing too much. Small smells are acceptable ā the goal is to avoid ActiveRecord-like bloat where aggregates accumulate dozens of setter methods.
Don't validate other aggregates from command handlers. Avoid patterns like @event_store.read.stream("Crm::Entity$#{id}").last or raise NotFound in command handlers. This couples the handler to stream naming conventions and makes it responsible for validating external state. Instead, trust the caller (controllers provide valid IDs from read model dropdowns) or model the relationship as its own aggregate that can enforce its own invariants.
Signs an aggregate is too big:
@name, @email, @company_id)on blocks are growing in numberRegisterCustomer, AddTodo, AssignDealCustomerRegistered, TodoAdded, DealAssignedInfra::Types::UUID and Infra::Types::String for typed attributes