Skip to content
Giau Tran Minh edited this page Dec 12, 2018 · 2 revisions

Define Commands

Command Name

Begin by defining the commands you want in your aggregate as a const CommandType. You can define your own naming convention for commands. Two naming conventions are listed below:

const (
	CreateInviteCommand eh.CommandType = "CreateInvite"
	AcceptInviteCommand eh.CommandType = "guestlist:accept_invite"
)

Command Struct

Every command needs to implement the command interface. Define the command properties similar to below (note that every command would need an ID field to identify the aggregate it is intended for)

type CreateInvite struct {
	ID   eh.UUID
	Name string
	Age  int `eh:"optional"`
}

And then implement the interface by defining the AggregateID(), AggregateType() and CommandType() methods

func (c CreateInvite) AggregateID() eh.UUID            { return c.ID }
func (c CreateInvite) AggregateType() eh.AggregateType { return InvitationAggregateType }
func (c CreateInvite) CommandType() eh.CommandType     { return CreateInviteCommand }

Register Commands

All commands for an aggregate need to be registered in the init()

func init() {
	eh.RegisterCommand(func() eh.Command { return &CreateInvite{} })
	eh.RegisterCommand(func() eh.Command { return &AcceptInvite{} })
	...
}

Static Type Checks

It would be a good idea to also define some static type checks on each command. This ensures that each command fully implements the command interface.

var _ = eh.Command(&CreateInvite{})
Clone this wiki locally