Sign in

    thy1016/mooncmd/aggregate does not have a README file

    AggregateId

    pub trait AggregateId {
    fn to_string(Self) -> String
    fn from_string(s : String) -> Result[Self, AggregateError]
    fn eq(self : Self, other : Self) -> Bool
    }

    聚合根标识
    impl AggregateId for Int

    Event

    pub trait Event {
    fn version() -> Int
    fn type_name() -> String
    }

    事件:已发生的事实,不可变

    AggregateError

    pub(all) suberror AggregateError {
    IdParseError(String)
    }

    AggUUID

    pub struct AggUUID {
    value : String
    }

    AggUUID::eq

    fn AggUUID::eq(self : AggUUID, other : AggUUID) -> Bool

    R-05 收口(2026-10-08):显式提升声明,消 implicit_impl_as_method([0079])

    AggUUID::from_string

    fn AggUUID::from_string(s : String) -> Result[AggUUID, AggregateError]

    R-05 收口(2026-10-08):显式提升声明,消 implicit_impl_as_method([0079])

    AggUUID::new

    fn AggUUID::new() -> AggUUID

    AggUUID::to_string

    fn AggUUID::to_string(self : AggUUID) -> String

    R-05 收口(2026-10-08):显式提升声明,消 implicit_impl_as_method([0079])

    AggregateRoot

    pub struct AggregateRoot[AggregateId, S, C, E] {
    handler : (S, C) -> Result[Array[E],
    CommandError
    ]
    applier :
    EventApplier
    [S, E]
    empty_state : S
    state : AggregateState[AggregateId, S, E]
    id : AggregateId
    }

    聚合根

    AggregateRoot::commit

    fn[AggregateId, S, C, E] AggregateRoot::commit(self : AggregateRoot[AggregateId, S, C, E]) -> AggregateRoot[AggregateId, S, C, E]

    提交事件(清空未提交事件)

    AggregateRoot::current_state

    fn[AggregateId, S, C, E] AggregateRoot::current_state(self : AggregateRoot[AggregateId, S, C, E]) -> S

    获取状态

    AggregateRoot::exec

    fn[AggregateId, S, C, E] AggregateRoot::exec(self : AggregateRoot[AggregateId, S, C, E], cmd : C) -> AggregateRoot[AggregateId, S, C, E] raise
    CommandError

    形态 A 流式执行:命令自动包装 CommandMessage, 错误 raise 上抛,返回 Self 链式。 用法:root.exec(CreateAccount("main", 100)).exec(DepositMoney(50))

    AggregateRoot::exec_with_effect

    fn[AggregateId, S, C, E] AggregateRoot::exec_with_effect(self : AggregateRoot[AggregateId, S, C, E], cmd : C, machine : RetryMachine, hooks : RetryHooks, wait : (Int64) -> Unit) -> AggregateRoot[AggregateId, S, C, E] raise
    CommandError

    Effect 形态执行(spec §2.1 分层):exec 上抛先过状态机—— 钩子覆写 > 词表默认 → decide → WaitRetry(ms) 则 wait(ms)(注入时钟,红线6 同源) 后重执行;ActionExhausted / GiveUpRaise(含熔断)→ 原错上抛。 幂等前提:execute Err 不改态 ⇒ 同命令重放同结果。

    AggregateRoot::execute

    fn[AggregateId, S, C, E] AggregateRoot::execute(self : AggregateRoot[AggregateId, S, C, E], cmd :
    CommandMessage
    [C]) -> Result[AggregateRoot[AggregateId, S, C, E],
    CommandError
    ]

    执行命令(借鉴 Commanded 的 execute/apply 模式)

    AggregateRoot::from_events

    fn[AggregateId, S, C, E] AggregateRoot::from_events(id : AggregateId, events : Array[
    EventMessage
    [AggregateId, E]], handler : (S, C) -> Result[Array[E],
    CommandError
    ], applier :
    EventApplier
    [S, E], empty_state : S) -> AggregateRoot[AggregateId, S, C, E]

    从已保存的事件恢复

    AggregateRoot::from_snapshot

    fn[AggregateId, S, C, E] AggregateRoot::from_snapshot(id : AggregateId, empty_state : S, base_state : S, base_version : Int, events : Array[
    EventMessage
    [AggregateId, E]], handler : (S, C) -> Result[Array[E],
    CommandError
    ], applier :
    EventApplier
    [S, E]) -> AggregateRoot[AggregateId, S, C, E]

    从快照基态 + 尾事件恢复(快照读路径):base = 快照态/版本,尾事件 折入;尾空 = 版本取快照版本(非 0)。from_events = 本形特例 (base = 空态/0,委托)。

    AggregateRoot::new

    fn[AggregateId, S, C, E] AggregateRoot::new(id : AggregateId, handler : (S, C) -> Result[Array[E],
    CommandError
    ], applier :
    EventApplier
    [S, E], empty_state : S) -> AggregateRoot[AggregateId, S, C, E]

    创建新的聚合根

    AggregateRoot::to_snapshot

    fn[AggregateId, S, C, E] AggregateRoot::to_snapshot(self : AggregateRoot[AggregateId, S, C, E]) ->
    Snapshot
    [AggregateId, S]

    创建快照

    AggregateRoot::uncommitted_events

    fn[AggregateId, S, C, E] AggregateRoot::uncommitted_events(self : AggregateRoot[AggregateId, S, C, E]) -> Array[
    EventMessage
    [AggregateId, E]]

    获取未提交事件

    AggregateRoot::version

    fn[AggregateId, S, C, E] AggregateRoot::version(self : AggregateRoot[AggregateId, S, C, E]) -> Int

    获取当前版本

    AggregateState

    pub struct AggregateState[AggregateId, S, E] {
    state : S
    version : Int
    uncommitted_events : Array[
    EventMessage
    [AggregateId, E]]
    }

    聚合根状态

    EffectErrorKind

    pub(all) enum EffectErrorKind {
    Duplicate
    Validation
    Storage
    Other
    }

    框架错误分类封闭集(结构知识非裁决——分类固定;动作归词表+钩子)

    RetryAction

    pub(all) enum RetryAction {
    ActionRetry
    ActionExhausted
    }

    重试动作封闭集(词表 on_error 值域)

    RetryHooks

    pub(all) struct RetryHooks {
    override_action : (EffectErrorKind) -> RetryAction?
    }

    策略钩子(红线7:业务裁决归钩子;None = 沿用词表默认分派)

    RetryHooks::none

    fn RetryHooks::none() -> RetryHooks

    纯透钩子(零覆写)

    RetryMachine

    pub(all) struct RetryMachine {
    default_action : (EffectErrorKind) -> RetryAction
    decide : (Int, Int, EffectErrorKind) -> RetryVerdict
    }

    Effect 重试状态机协议(记录面,宪法4;gen 产物实现)

    RetryVerdict

    pub(all) enum RetryVerdict {
    WaitRetry(wait_ms~ : Int64)
    GiveUpRaise
    }

    状态机裁决输出

    effect_kind_of_command_error

    fn effect_kind_of_command_error(err :
    CommandError
    ) -> EffectErrorKind

    命令错误分类(结构知识:Conflict → Duplicate——命令面乐观并发镜像,可重试性质)

    effect_kind_of_store_error

    存储错误分类(dispatch 层 with_config 挂钩用)