diff --git a/engine.go b/engine.go index 66cb708d2008b3ab3c90fe2fc05b12327d680798..8e7fea56a4f6cfa875da174ec434b093be5d66e2 100644 --- a/engine.go +++ b/engine.go @@ -277,7 +277,11 @@ type ActFunc func(ctx context.Context, container RootContainer, roots ...IEntity type MainFunc func(ctx context.Context, repo *Repository) error type PostSaveFunc func(ctx context.Context, res *Result) -// EventHandlerConstruct EventHandler çš„æž„é€ å‡½æ•°ï¼Œå¸¦ä¸€ä¸ªå…¥å‚和一个返回值,入å‚是与事件类型匹é…的事件数æ®æŒ‡é’ˆç±»åž‹ï¼Œè¿”回值是 ICommand +// EventHandlerConstruct EventHandler çš„æž„é€ å‡½æ•°ï¼Œå¸¦ä¸€ä¸ªå…¥å‚和一个返回值,入å‚是与事件类型匹é…的事件数æ®æŒ‡é’ˆç±»åž‹ï¼Œ +// 返回值支æŒä¸‰ç§: +// - ICommand interface +// - ICommandMain interface +// - MainFunc type // 示例 func(evt *OrderCreatedEvent) *OnEventCreateCommand type EventHandlerConstruct interface{} @@ -478,10 +482,6 @@ func (e *Engine) RegisterEventHandler(eventType EventType, construct EventHandle panic("event type must be pointer") } evtType = evtType.Elem() // event type 引用实际类型 - outType := handlerType.Out(0) - if !outType.Implements(cmdType) { - panic("construct output must be type of ICommand") - } constructFunc := reflect.ValueOf(construct) RegisterEventHandler(eventType, func(ctx context.Context, evt *DomainEvent) error { @@ -497,7 +497,8 @@ func (e *Engine) RegisterEventHandler(eventType EventType, construct EventHandle } outputs := constructFunc.Call([]reflect.Value{bizEvt}) - if res := e.RunCommand(ctx, outputs[0].Interface().(ICommand)); res.Error != nil { + + if res := e.Run(ctx, outputs[0].Interface()); res.Error != nil { e.logger.Error(res.Error, "event handler exec failed") return res.Error } @@ -767,8 +768,11 @@ func (e *Stage) Run(ctx context.Context, cmd interface{}) *Result { return e.WithOption(options...).Lock(keys...).Main(c.Main).Save(ctx) case func(ctx context.Context, repo *Repository) error: return e.Main(c).Save(ctx) + case MainFunc: + return e.Main(c).Save(ctx) + default: + panic(fmt.Sprintf("cmd type %T is invalid", c)) } - panic("cmd is invalid") } func childrenSnapshot(children map[string][]IEntity) map[string][]IEntity {