-
Notifications
You must be signed in to change notification settings - Fork 0
/
generics.go
38 lines (28 loc) · 869 Bytes
/
generics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package cqrs
import "context"
func RegisterCommandHandler[Command any](commandBus CommandBus, handler CommandHandlerFunc[Command]) error {
if err := commandBus.Register(handler); err != nil {
return err
}
return nil
}
func RegisterEventHandler[Event any](eventBus EventBus, handler EventHandlerFunc[Event]) error {
if err := eventBus.Register(handler); err != nil {
return err
}
return nil
}
func RegisterQueryHandler[Query any, Result any](queryBus QueryBus, handler QueryHandlerFunc[Query, Result]) error {
if err := queryBus.Register(handler); err != nil {
return err
}
return nil
}
func ExecuteQuery[Query any, Result any](queryBus QueryBus, ctx context.Context, query Query) (Result, error) {
var emptyResult Result
result, err := queryBus.Execute(ctx, query)
if err != nil {
return emptyResult, err
}
return result.(Result), nil
}