-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
115 lines (96 loc) · 3.42 KB
/
Copy pathapp.go
File metadata and controls
115 lines (96 loc) · 3.42 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package framework
import (
"context"
"errors"
"github.com/glopezep/framework/bus"
"github.com/glopezep/framework/cqrs"
"github.com/glopezep/framework/domain"
"github.com/glopezep/framework/inbox"
"github.com/glopezep/framework/integration"
"github.com/glopezep/framework/outbox"
)
type Application struct {
Flusher *cqrs.AggregateFlusher
InboxRepo inbox.Repository
OutboxRepo outbox.Repository
DomainDispatcher *domain.Dispatcher
Bus *integration.JetStreamBus // Optional
// Add other shared dependencies as needed
// ...other optional components
commandHandlers map[string]cqrs.CommandHandler
queryHandlers map[string]cqrs.QueryHandler
}
type ApplicationOption func(*Application)
// NewFramework initializes and wires up all framework components.
func NewApplication(
inboxRepo inbox.Repository,
outboxRepo outbox.Repository,
opts ...ApplicationOption,
) *Application {
eventBus := bus.NewEventBus()
domainDispatcher := domain.NewDispatcher(eventBus)
flusher := cqrs.NewAggregateFlusher(domainDispatcher)
app := &Application{
Flusher: flusher,
InboxRepo: inboxRepo,
OutboxRepo: outboxRepo,
DomainDispatcher: domainDispatcher,
commandHandlers: make(map[string]cqrs.CommandHandler),
queryHandlers: make(map[string]cqrs.QueryHandler),
}
for _, opt := range opts {
opt(app)
}
return app
}
func WithJetStreamBus(bus *integration.JetStreamBus) ApplicationOption {
return func(app *Application) {
app.Bus = bus
}
}
// RegisterCommandHandler registers a command handler by name.
func (a *Application) RegisterCommandHandler(name string, handler cqrs.CommandHandler) {
a.commandHandlers[name] = handler
}
// RegisterQueryHandler registers a query handler by name.
func (a *Application) RegisterQueryHandler(name string, handler cqrs.QueryHandler) {
a.queryHandlers[name] = handler
}
// ExecuteCommand executes a registered command handler by name.
func (a *Application) ExecuteCommand(ctx context.Context, name string, cmd cqrs.Command) error {
handler, ok := a.commandHandlers[name]
if !ok {
return errors.New("command handler not found: " + name)
}
return handler.Handle(ctx, cmd)
}
// ExecuteQuery executes a registered query handler by name and returns the result.
func (a *Application) ExecuteQuery(ctx context.Context, name string, qry cqrs.Query) (any, error) {
handler, ok := a.queryHandlers[name]
if !ok {
return nil, errors.New("query handler not found: " + name)
}
return handler.Handle(ctx, qry)
}
type commandHandlerWrapper struct {
fn cqrs.CommandHandlerFunc
}
func (w *commandHandlerWrapper) Handle(ctx context.Context, cmd cqrs.Command) error {
return w.fn(ctx, cmd)
}
// NewCommandHandler creates a command handler with default middleware.
func (a *Application) NewCommandHandler(fn cqrs.CommandHandlerFunc, middlewares ...cqrs.CommandMiddleware) cqrs.CommandHandler {
all := append(middlewares, cqrs.FlushingCommandMiddleware(a.Flusher))
handlerFunc := cqrs.NewCommandHandler(a.Flusher, fn, all...)
return &commandHandlerWrapper{fn: handlerFunc}
}
type queryHandlerWrapper struct {
fn cqrs.QueryHandlerFunc
}
func (w *queryHandlerWrapper) Handle(ctx context.Context, qry cqrs.Query) (any, error) {
return w.fn(ctx, qry)
}
func (a *Application) NewQueryHandler(fn cqrs.QueryHandlerFunc, middlewares ...cqrs.QueryMiddleware) cqrs.QueryHandler {
handlerFunc := cqrs.NewQueryHandler(fn, middlewares...)
return &queryHandlerWrapper{fn: handlerFunc}
}