You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This works well as long as the Create method does not depend on any external dependencies, like it does in the following example (context.Context and RatingService):
package example
typeMoviestruct {
*aggregate.Base
}
funcNewMovie(id uuid.UUID) *Movie {
m:=&Movie{Base: aggregate.New(...))
command.ApplyWith(m, func(namestring) error {
// Here, we would need access to a context.Context and RatingServicereturn m.Create(???, name, ???)
}, "movie.create")
returnm
}
typeCreateParamsstruct {
NamestringRatingint8
}
typeRatingServiceinterface {
Rating(ctx context.Context, namestring) (int8, error)
}
func (m*Movie) Create(ctxcontext.Context, namestring, svcRatingService) error {
rating, err:=svc.Rating(ctx, name)
iferr!=nil {
returnerr
}
aggregate.Next(m, "movie.created", CreateParams{
Name: name,
Rating: rating,
})
returnnil
}
Ideas
a.command.ApplyContextWith helper
Idea: Add a new command.ApplyContextWith helper function to register command handlers that accept a generic command.Context type.
package example
typeMoviestruct { ... }
funcNewMovie() *Movie {
m:=&Movie{...}
command.ApplyContextWith(m, func(ctx command.Ctx[string]) error {
name:=ctx.Payload()
returnm.Create(ctx, name, ???) // still no RatingService available
}, "movie.create")
returnm
}
b. Dependency Injection Container
Idea: Provide a DI Container to command.Context to provide dependencies to aggregates when setting up commands.
// commands.gopackage example
import"github.com/modernice/goes/command/handler"funcHandleCommands(ctx context.Context, svcRatingService, bus command.Bus, repo aggregate.Repository) <-chanerror {
returnhandler.New(NewMovie, repo, bus).MustHandle(ctx, command.Provide(svc))
}
Context
The
*aggregate.Base
type embeds acommand.Handler
to allow registration of commands in the aggregate constructor, like this:This works well as long as the
Create
method does not depend on any external dependencies, like it does in the following example (context.Context
andRatingService
):Ideas
a.
command.ApplyContextWith
helperIdea: Add a new
command.ApplyContextWith
helper function to register command handlers that accept a genericcommand.Context
type.b. Dependency Injection Container
Idea: Provide a DI Container to
command.Context
to provide dependencies to aggregates when setting up commands.The text was updated successfully, but these errors were encountered: