这是一个针对频繁出差飞行会员是否升级为模型的Go语言ES实现:
jen20/go-event-sourcing-sample · GitHub
使用聚合体实体模型定义一个struct,其中主要是可变的状态,比如哪些客户当飞行里程达到后能够升级到黄金会员,需要一个可变状态来跟踪其等级积分(tierPoint):
type FrequentFlierAccount struct {
id string
miles int
tierPoints int
status Status
}
<p class="indent">
|
其中Status是使用不可变的ENUM用于只读:
type Status int
const (
StatusRed Status = iota
StatusSilver Status = iota
StatusGold Status = iota
)
<p class="indent">
|
这个聚合体实体模型的状态是通过下面三个事件来改变的:
type FrequentFlierAccountCreated struct {
AccountId string
OpeningMiles int
OpeningTierPoints int
}
type StatusMatched struct {
NewStatus Status
}
type FlightTaken struct {
MilesAdded int
TierPointsAdded int
}
<p class="indent">
|
以上基本模型设计完毕后,可以实现拓展服务功能,比如:从历史资料加载信息Loading from History;改变跟踪Change Tracking;具体可参考:英文原文
[该贴被banq于2015-02-10 11:05修改过]