modernice / goes

goes is an event-sourcing framework for Go.
https://goes.modernice.dev
Apache License 2.0
134 stars 12 forks source link

Aggregate Command Handler #28

Closed bounoable closed 2 years ago

bounoable commented 2 years ago

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())
}