domain-fintech
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFinTech Domain
金融科技(FinTech)领域
Layer 3: Domain Constraints
第3层:领域约束
Domain Constraints → Design Implications
领域约束 → 设计影响
| Domain Rule | Design Constraint | Rust Implication |
|---|---|---|
| Audit trail | Immutable records | Arc<T>, no mutation |
| Precision | No floating point | rust_decimal |
| Consistency | Transaction boundaries | Clear ownership |
| Compliance | Complete logging | Structured tracing |
| Reproducibility | Deterministic execution | No race conditions |
| 领域规则 | 设计约束 | Rust 实现建议 |
|---|---|---|
| 审计追踪 | 不可变记录 | Arc<T>,禁止修改 |
| 精度要求 | 禁止使用浮点数 | rust_decimal |
| 一致性 | 事务边界 | 清晰的所有权模型 |
| 合规性 | 完整日志记录 | 结构化追踪(Structured tracing) |
| 可复现性 | 确定性执行 | 无竞态条件 |
Critical Constraints
关键约束
Financial Precision
金融精度
RULE: Never use f64 for money
WHY: Floating point loses precision
RUST: Use rust_decimal::DecimalRULE: 切勿使用f64处理资金
WHY: 浮点数会丢失精度
RUST: 使用rust_decimal::DecimalAudit Requirements
审计要求
RULE: All transactions must be immutable and traceable
WHY: Regulatory compliance, dispute resolution
RUST: Arc<T> for sharing, event sourcing patternRULE: 所有交易必须是不可变且可追踪的
WHY: 符合监管要求、解决纠纷
RUST: 使用Arc<T>实现共享,采用事件溯源模式Consistency
一致性
RULE: Money can't disappear or appear
WHY: Double-entry accounting principles
RUST: Transaction types with validated totalsRULE: 资金不能凭空消失或出现
WHY: 遵循复式记账原则
RUST: 采用带总额验证的事务类型Trace Down ↓
向下追溯 ↓
From constraints to design (Layer 2):
"Need immutable transaction records"
↓ m09-domain: Model as Value Objects
↓ m01-ownership: Use Arc for shared immutable data
"Need precise decimal math"
↓ m05-type-driven: Newtype for Currency/Amount
↓ rust_decimal: Use Decimal type
"Need transaction boundaries"
↓ m12-lifecycle: RAII for transaction scope
↓ m09-domain: Aggregate boundaries从约束到设计(第2层):
"需要不可变的交易记录"
↓ m09-domain: 建模为值对象
↓ m01-ownership: 使用Arc处理共享不可变数据
"需要精确的小数运算"
↓ m05-type-driven: 为货币/金额创建Newtype
↓ rust_decimal: 使用Decimal类型
"需要事务边界"
↓ m12-lifecycle: 用RAII实现事务范围
↓ m09-domain: 聚合边界Key Crates
关键依赖库(Crates)
| Purpose | Crate |
|---|---|
| Decimal math | rust_decimal |
| Date/time | chrono, time |
| UUID | uuid |
| Serialization | serde |
| Validation | validator |
| 用途 | 依赖库 |
|---|---|
| 小数运算 | rust_decimal |
| 日期/时间 | chrono, time |
| UUID | uuid |
| 序列化 | serde |
| 验证 | validator |
Design Patterns
设计模式
| Pattern | Purpose | Implementation |
|---|---|---|
| Currency newtype | Type safety | |
| Transaction | Atomic operations | Event sourcing |
| Audit log | Traceability | Structured logging with trace IDs |
| Ledger | Double-entry | Debit/credit balance |
| 模式 | 用途 | 实现方式 |
|---|---|---|
| 货币Newtype | 类型安全 | |
| 事务 | 原子操作 | 事件溯源 |
| 审计日志 | 可追踪性 | 带追踪ID的结构化日志 |
| 分类账 | 复式记账 | 借贷平衡 |
Code Pattern: Currency Type
代码模式:货币类型
rust
use rust_decimal::Decimal;
#[derive(Clone, Debug, PartialEq)]
pub struct Amount {
value: Decimal,
currency: Currency,
}
impl Amount {
pub fn new(value: Decimal, currency: Currency) -> Self {
Self { value, currency }
}
pub fn add(&self, other: &Amount) -> Result<Amount, CurrencyMismatch> {
if self.currency != other.currency {
return Err(CurrencyMismatch);
}
Ok(Amount::new(self.value + other.value, self.currency))
}
}rust
use rust_decimal::Decimal;
#[derive(Clone, Debug, PartialEq)]
pub struct Amount {
value: Decimal,
currency: Currency,
}
impl Amount {
pub fn new(value: Decimal, currency: Currency) -> Self {
Self { value, currency }
}
pub fn add(&self, other: &Amount) -> Result<Amount, CurrencyMismatch> {
if self.currency != other.currency {
return Err(CurrencyMismatch);
}
Ok(Amount::new(self.value + other.value, self.currency))
}
}Common Mistakes
常见错误
| Mistake | Domain Violation | Fix |
|---|---|---|
| Using f64 | Precision loss | rust_decimal |
| Mutable transaction | Audit trail broken | Immutable + events |
| String for amount | No validation | Validated newtype |
| Silent overflow | Money disappears | Checked arithmetic |
| 错误 | 违反的领域规则 | 修复方案 |
|---|---|---|
| 使用f64 | 丢失精度 | 改用rust_decimal |
| 可变交易记录 | 破坏审计追踪 | 不可变设计 + 事件溯源 |
| 用字符串存储金额 | 无验证机制 | 带验证的Newtype |
| 静默溢出 | 资金丢失 | 使用检查型算术运算 |
Trace to Layer 1
追溯到第1层
| Constraint | Layer 2 Pattern | Layer 1 Implementation |
|---|---|---|
| Immutable records | Event sourcing | Arc<T>, Clone |
| Transaction scope | Aggregate | Owned children |
| Precision | Value Object | rust_decimal newtype |
| Thread-safe sharing | Shared immutable | Arc (not Rc) |
| 约束 | 第2层模式 | 第1层实现 |
|---|---|---|
| 不可变记录 | 事件溯源 | Arc<T>, Clone |
| 事务范围 | 聚合 | 拥有所有权的子对象 |
| 精度要求 | 值对象 | rust_decimal Newtype |
| 线程安全共享 | 共享不可变数据 | Arc(而非Rc) |
Related Skills
相关技能
| When | See |
|---|---|
| Value Object design | m09-domain |
| Ownership for immutable | m01-ownership |
| Arc for sharing | m02-resource |
| Error handling | m13-domain-error |
| 场景 | 参考内容 |
|---|---|
| 值对象设计 | m09-domain |
| 不可变数据的所有权管理 | m01-ownership |
| 使用Arc实现共享 | m02-resource |
| 错误处理 | m13-domain-error |