Documentation ¶
Index ¶
- Constants
- Variables
- func RegisterEventBus(eventBus IEventBus)
- func RegisterEventHandler(t EventType, handler EventHandler)
- func RegisterEventTXChecker(t EventType, checker EventTXChecker)
- type ActFunc
- type Action
- type ActionResult
- type BaseEntity
- func (e *BaseEntity) AddEvent(evt IEvent, opts ...EventOpt)
- func (e *BaseEntity) Dirty()
- func (e *BaseEntity) GetChildren() map[string][]IEntity
- func (e *BaseEntity) GetEvents() []*DomainEvent
- func (e *BaseEntity) GetID() string
- func (e *BaseEntity) IsDirty() bool
- func (e *BaseEntity) SetID(id string)
- func (e *BaseEntity) UnDirty()
- type BuildFunc
- type Command
- func (c *Command) Act(ctx context.Context, container RootContainer, roots ...IEntity) (err error)
- func (c *Command) Build(ctx context.Context, builder DomainBuilder) (roots []IEntity, err error)
- func (c *Command) Commit(ctx context.Context) error
- func (c *Command) Init(ctx context.Context) (lockIDs []string, err error)
- func (c *Command) Output(data interface{})
- func (c *Command) PostSave(ctx context.Context, res *Result)
- func (c *Command) SetStage(s StageAgent)
- type DTimerOption
- type DomainBuilder
- type DomainEvent
- type DomainEventHandler
- type DomainEventTXChecker
- type Engine
- func (e *Engine) Create(ctx context.Context, roots ...IEntity) *Result
- func (e *Engine) Delete(ctx context.Context, roots ...IEntity) *Result
- func (e *Engine) NewStage() *Stage
- func (e *Engine) RegisterCronTask(key EventType, cron string, f func(key, cron string))
- func (e *Engine) RegisterEventHandler(eventType EventType, construct EventHandlerConstruct)
- func (e *Engine) RunCommand(ctx context.Context, c ICommand, opts ...Option) *Result
- type EntityContainer
- func (w *EntityContainer) Add(root IEntity) error
- func (w *EntityContainer) GetChildren() map[string][]IEntity
- func (w *EntityContainer) GetDeleted() []IEntity
- func (w *EntityContainer) Recycle(e IEntity)
- func (w *EntityContainer) Remove(root IEntity) error
- func (w *EntityContainer) SetChildren(roots []IEntity)
- type EntitySlice
- type ErrList
- type EventBusOption
- type EventHandler
- type EventHandlerConstruct
- type EventOpt
- type EventOption
- type EventPersist
- type EventSaveOption
- type EventTXChecker
- type EventType
- type IAfterCreate
- type IAfterDelete
- type IAfterUpdate
- type IBeforeCreate
- type IBeforeDelete
- type IBeforeUpdate
- type ICommand
- type IConverter
- type IDGeneratorOption
- type IDirty
- type IEntity
- type IEvent
- type IEventBus
- type IExecutor
- type IIDGenerator
- type ILock
- type IModel
- type IStageSetter
- type ITimer
- type ITransaction
- type ITransactionEventBus
- type LoggerOption
- type OpType
- type Option
- type Options
- type PostSaveFunc
- type PostSaveOption
- type RecursiveDeleteOption
- type Result
- type RootContainer
- type SendType
- type Stage
- func (e *Stage) Act(f ActFunc) *Stage
- func (e *Stage) Build(f BuildFunc) *Stage
- func (e *Stage) BuildEntity(ctx context.Context, parent IEntity, children ...interface{}) error
- func (e *Stage) Lock(keys ...string) *Stage
- func (e *Stage) RunCommand(ctx context.Context, c ICommand) *Result
- func (e *Stage) Save(ctx context.Context) *Result
- func (e *Stage) WithOption(opts ...Option) *Stage
- type StageAgent
- type TXStatus
- type TimerEvent
- type TimerHandler
- type TransactionOption
Constants ¶
const WithRecursiveDelete = RecursiveDeleteOption(true)
const WithTransaction = TransactionOption(true)
const WithoutTransaction = TransactionOption(false)
Variables ¶
var ErrBreak = fmt.Errorf("break process") // 中断流程,不返回错误
var ErrEntityNotFound = fmt.Errorf("entity not found")
var ErrEntityNotRegister = fmt.Errorf("entity not registered")
var ErrEntityRepeated = fmt.Errorf("entity already added")
var ErrNoEventBusFound = fmt.Errorf("no eventbus found")
var ErrNoEventTimerFound = fmt.Errorf("no event_timer found")
Functions ¶
func RegisterEventHandler ¶
func RegisterEventHandler(t EventType, handler EventHandler)
RegisterEventHandler 注册事件处理器
func RegisterEventTXChecker ¶
func RegisterEventTXChecker(t EventType, checker EventTXChecker)
RegisterEventTXChecker 注册事务反查接口
Types ¶
type ActFunc ¶
type ActFunc func(ctx context.Context, container RootContainer, roots ...IEntity) error
type ActionResult ¶
type ActionResult struct {
Data interface{}
}
type BaseEntity ¶
type BaseEntity struct {
// contains filtered or unexported fields
}
func NewBase ¶
func NewBase(id string) BaseEntity
func (*BaseEntity) AddEvent ¶
func (e *BaseEntity) AddEvent(evt IEvent, opts ...EventOpt)
AddEvent 实体发送事件,调用方需要保证事件是可序列化的,否则会导致 panic
func (*BaseEntity) Dirty ¶
func (e *BaseEntity) Dirty()
func (*BaseEntity) GetChildren ¶
func (e *BaseEntity) GetChildren() map[string][]IEntity
func (*BaseEntity) GetEvents ¶
func (e *BaseEntity) GetEvents() []*DomainEvent
func (*BaseEntity) GetID ¶
func (e *BaseEntity) GetID() string
func (*BaseEntity) IsDirty ¶
func (e *BaseEntity) IsDirty() bool
func (*BaseEntity) SetID ¶
func (e *BaseEntity) SetID(id string)
func (*BaseEntity) UnDirty ¶
func (e *BaseEntity) UnDirty()
type BuildFunc ¶
type BuildFunc func(ctx context.Context, h DomainBuilder) (roots []IEntity, err error)
type Command ¶
type Command struct {
// contains filtered or unexported fields
}
func (*Command) Output ¶
func (c *Command) Output(data interface{})
Output 设定命令的返回值,data 会被赋值到 Result.Output 该方法可以在 Init - Build - Act 中调用,调用后,后续的方法将不会被执行 例如在 Init 中调用 Return,Build、Act 方法不会执行,但是 PostSave 会被执行
func (*Command) SetStage ¶
func (c *Command) SetStage(s StageAgent)
type DTimerOption ¶ added in v1.1.0
type DTimerOption struct {
// contains filtered or unexported fields
}
func WithTimer ¶ added in v1.1.0
func WithTimer(timer ITimer) DTimerOption
func (DTimerOption) ApplyToOptions ¶ added in v1.1.0
func (t DTimerOption) ApplyToOptions(opts *Options)
type DomainBuilder ¶
type DomainBuilder struct {
// contains filtered or unexported fields
}
type DomainEvent ¶
type DomainEvent struct { ID string Type EventType SendType SendType Sender string // 事件发出实体 ID Payload []byte CreatedAt time.Time }
func NewDomainEvent ¶
func NewDomainEvent(event IEvent, opts ...EventOpt) *DomainEvent
func (*DomainEvent) GetSender ¶
func (d *DomainEvent) GetSender() string
func (*DomainEvent) GetType ¶
func (d *DomainEvent) GetType() EventType
type DomainEventHandler ¶
type DomainEventHandler func(ctx context.Context, evt *DomainEvent) error
DomainEventHandler 通用 DomainEvent 的事件处理器
type DomainEventTXChecker ¶
type DomainEventTXChecker func(evt *DomainEvent) TXStatus
DomainEventTXChecker 通用 DomainEvent 的事务回查
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
func (*Engine) RegisterCronTask ¶ added in v1.1.0
RegisterCronTask 注册定时任务
func (*Engine) RegisterEventHandler ¶
func (e *Engine) RegisterEventHandler(eventType EventType, construct EventHandlerConstruct)
type EntityContainer ¶
type EntityContainer struct { BaseEntity // contains filtered or unexported fields }
EntityContainer 负责维护领域内所有聚合根实体的实体
func (*EntityContainer) Add ¶
func (w *EntityContainer) Add(root IEntity) error
func (*EntityContainer) GetChildren ¶
func (w *EntityContainer) GetChildren() map[string][]IEntity
func (*EntityContainer) GetDeleted ¶
func (w *EntityContainer) GetDeleted() []IEntity
func (*EntityContainer) Remove ¶
func (w *EntityContainer) Remove(root IEntity) error
func (*EntityContainer) SetChildren ¶
func (w *EntityContainer) SetChildren(roots []IEntity)
type EntitySlice ¶
type EntitySlice []IEntity
type EventBusOption ¶
type EventBusOption struct {
// contains filtered or unexported fields
}
func WithEventBus ¶
func WithEventBus(eventBus IEventBus) EventBusOption
func (EventBusOption) ApplyToOptions ¶
func (t EventBusOption) ApplyToOptions(opts *Options)
type EventHandler ¶
type EventHandler interface{}
EventHandler 指定特定领域事件的事件处理器,必须是带有 2 个入参的函数类型,第一个参数为 context.Context 类型 第二个为与 EventType 匹配的事件数据指针类型, 示例 func(ctx context.Context, evt *OrderCreatedEvent) error 当第二个参数声明为 *DomainEvent,EventHandler 回调时会传入原始的事件类型,用户定义事件以序列化形式存在 DomainEvent.Payload 中
type EventHandlerConstruct ¶
type EventHandlerConstruct interface{}
EventHandlerConstruct EventHandler 的构造函数,带一个入参和一个返回值,入参是与事件类型匹配的事件数据指针类型,返回值是 ICommand 示例 func(evt *OrderCreatedEvent) *OnEventCreateCommand
type EventOption ¶
type EventPersist ¶
type EventPersist func(event *DomainEvent) (IModel, error)
type EventSaveOption ¶
type EventSaveOption EventPersist
func WithEventPersist ¶
func WithEventPersist(f EventPersist) EventSaveOption
func (EventSaveOption) ApplyToOptions ¶
func (t EventSaveOption) ApplyToOptions(opts *Options)
type EventTXChecker ¶
type EventTXChecker interface{}
EventTXChecker 事务事件的回查接口,必须带 1 个事件入参,1 个 TXStatus 返回值的函数 示例:func(evt *OrderCreatedEvent) TXStatus
type IAfterCreate ¶
type IAfterDelete ¶
type IAfterUpdate ¶
type IBeforeCreate ¶
IBeforeCreate hook
type IBeforeDelete ¶
type IBeforeUpdate ¶
type ICommand ¶
type ICommand interface { // Init 会在锁和事务之前执行,可进行数据校验,前置准备工作,可选返回锁ID Init(ctx context.Context) (lockKeys []string, err error) // Build 构建并返回聚合根实体,框架会为返回值自动生成快照,作为持久化比对的依据 // 注意,Create 的情况不需要在 Build 方法返回 Build(ctx context.Context, builder DomainBuilder) (roots []IEntity, err error) // Act 调用实体行为 Act(ctx context.Context, container RootContainer, roots ...IEntity) (err error) // PostSave Save 事务完成后回调,可以执行组装返回数据等操作 PostSave(ctx context.Context, res *Result) }
type IConverter ¶
type IConverter interface { // Entity2Model 实体转化为数据模型,entity: 当前实体 parent: 父实体 op: 调用场景,实体新建、实体更新、实体删除、实体查询等 // 注意,对于Model上entity没有对应的字段,除新建场景外,其他需要保持为数据类型的零值,新建场景可以额外指定默认值 // 实体未注册返回 ErrEntityNotRegister 错误 Entity2Model(entity, parent IEntity, op OpType) (IModel, error) // Model2Entity 模型转换为实体,在创建实体的时候调用 Model2Entity(model IModel, entity IEntity) error }
type IDGeneratorOption ¶
type IDGeneratorOption struct {
// contains filtered or unexported fields
}
func WithIDGenerator ¶
func WithIDGenerator(idGen IIDGenerator) IDGeneratorOption
func (IDGeneratorOption) ApplyToOptions ¶
func (t IDGeneratorOption) ApplyToOptions(opts *Options)
type IDirty ¶
type IDirty interface { // Dirty 标记实体对象是否需要更新 Dirty() // UnDirty 取消实体的更新标记 UnDirty() // IsDirty 判断实体是否需要更新 IsDirty() bool }
type IEventBus ¶
type IEventBus interface { // Dispatch 发送领域事件到 EventBus,该方法会在事务内被同步调用 // 对于每个事件,EventBus 必须要至少保证 at least once 送达 Dispatch(ctx context.Context, evt ...*DomainEvent) error // RegisterEventHandler 注册事件回调,IEventBus 的实现必须保证收到事件同步调用该回调 RegisterEventHandler(cb DomainEventHandler) }
type IExecutor ¶
type IExecutor interface { ITransaction IConverter Exec(ctx context.Context, actions *Action) error }
type IIDGenerator ¶
type IStageSetter ¶
type IStageSetter interface {
SetStage(s StageAgent)
}
type ITimer ¶ added in v1.1.0
type ITimer interface { // RegisterTimerHandler 注册定时任务,定时到来时候调用该回调函数 RegisterTimerHandler(cb TimerHandler) // RunCron 按照 cron 语法设置定时,并在定时到达后作为参数调用定时任务回调 // key: 定时任务唯一标识,重复调用时不覆盖已有计时; cron: 定时配置; data: 透传数据,回调函数传入 RunCron(key, cron string, data []byte) error // RunOnce 指定时间单次运行 // key: 定时任务唯一标识,重复调用时不覆盖已有计时; t: 执行时间; data: 透传数据,回调函数传入 RunOnce(key string, t time.Time, data []byte) error // Cancel 删除某个定时 Cancel(key string) error }
ITimer 分布式定时器协议
type ITransaction ¶
type ITransactionEventBus ¶
type ITransactionEventBus interface { IEventBus // RegisterEventTXChecker 注册事件回查 RegisterEventTXChecker(cb DomainEventTXChecker) // DispatchBegin 开始发送事务,eventbus 需要保证同步成功,但是暂不发送到消费端 // 返回值 context.Context 会传递给 Commie 和 Rollback 接口,实现方可以利用机制传递上下文信息 DispatchBegin(ctx context.Context, evt ...*DomainEvent) (context.Context, error) // Commit 发送事件到消费端 Commit(ctx context.Context) error // Rollback 回滚事件 Rollback(ctx context.Context) error }
ITransactionEventBus 支持事务性消息的实现 如果 eventbus 实现了这个接口,当发送事件的 DomainEvent.SendType == SendTypeTransaction 会优先选择事务的方式来发送
type LoggerOption ¶
type LoggerOption struct {
// contains filtered or unexported fields
}
func WithLogger ¶
func WithLogger(logger logr.Logger) LoggerOption
func (LoggerOption) ApplyToOptions ¶
func (t LoggerOption) ApplyToOptions(opts *Options)
type Options ¶
type Options struct { WithTransaction bool RecursiveDelete bool // 删除根实体是否递归删除所有子实体 EventPersist EventPersist // 是否保存领域事件到 DB Logger logr.Logger EventBus IEventBus Timer ITimer IDGenerator IIDGenerator PostSaveHooks []PostSaveFunc }
type PostSaveFunc ¶
type PostSaveOption ¶
type PostSaveOption PostSaveFunc
func WithPostSave ¶
func WithPostSave(f PostSaveFunc) PostSaveOption
func (PostSaveOption) ApplyToOptions ¶
func (t PostSaveOption) ApplyToOptions(opts *Options)
type RecursiveDeleteOption ¶
type RecursiveDeleteOption bool
func (RecursiveDeleteOption) ApplyToOptions ¶
func (t RecursiveDeleteOption) ApplyToOptions(opts *Options)
type RootContainer ¶
type RootContainer struct {
// contains filtered or unexported fields
}
RootContainer 聚合根实体容器
type Stage ¶
type Stage struct {
// contains filtered or unexported fields
}
Stage 取舞台的意思,表示单次运行
func (*Stage) BuildEntity ¶
BuildEntity 查询并构建 parent 以及 children 实体 parent 必须指定 id,children 为可选,需要是 *IEntity 或者 *[]IEntity 类型
func (*Stage) WithOption ¶
type StageAgent ¶
type StageAgent struct {
// contains filtered or unexported fields
}
func (*StageAgent) Output ¶
func (s *StageAgent) Output(data interface{})
type TimerEvent ¶ added in v1.1.0
TimerEvent 定时器专用的事件
func (*TimerEvent) GetSender ¶ added in v1.1.0
func (e *TimerEvent) GetSender() string
func (*TimerEvent) GetType ¶ added in v1.1.0
func (e *TimerEvent) GetType() EventType
type TimerHandler ¶ added in v1.1.0
type TransactionOption ¶
type TransactionOption bool
func (TransactionOption) ApplyToOptions ¶
func (t TransactionOption) ApplyToOptions(opts *Options)