modernice/goes

Aggregate Command Handler

Closed this issue · 0 comments

Goal

package example

type FooEvent struct { ... }

type BarEvent struct { ... }

type Foo struct {
  *aggregate.Base
  *command.AggregateHandler
}

type FooRepository = aggregate.TypedRepository[*Foo]

func NewFoo(uuid.UUID) *Foo {
  f := &Foo{
    Base: aggregate.New(...),
    AggregateHandler: command.NewAggregateHandler(),
  }

  command.HandleWith(f, func(ctx command.Ctx[FooData]) error {
    return f.Foo(...)
  }, "foo")

  command.HandleWith(f, func(ctx command.Ctx[BarData]) error {
    return f.Foo(...)
  }, "bar")
}

func (f *Foo) Foo() error {
  aggregate.Next(f, ...)
  return nil
}

func (f *Foo) Bar() error {
  aggregate.Next(f, ...)
  return nil
}

func example(bus command.Bus, repo FooRepository) {
  h := command.NewHandlerOf(repo, bus)

  // command names are extracted from a method on the aggregate
  errs, err := h.Handle(context.TODO())
  errs := h.MustHandle(context.TODO())
}