domain-fintech

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

FinTech Domain

金融科技(FinTech)领域

Layer 3: Domain Constraints
第3层:领域约束

Domain Constraints → Design Implications

领域约束 → 设计影响

Domain RuleDesign ConstraintRust Implication
Audit trailImmutable recordsArc<T>, no mutation
PrecisionNo floating pointrust_decimal
ConsistencyTransaction boundariesClear ownership
ComplianceComplete loggingStructured tracing
ReproducibilityDeterministic executionNo 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::Decimal
RULE: 切勿使用f64处理资金
WHY: 浮点数会丢失精度
RUST: 使用rust_decimal::Decimal

Audit Requirements

审计要求

RULE: All transactions must be immutable and traceable
WHY: Regulatory compliance, dispute resolution
RUST: Arc<T> for sharing, event sourcing pattern
RULE: 所有交易必须是不可变且可追踪的
WHY: 符合监管要求、解决纠纷
RUST: 使用Arc<T>实现共享,采用事件溯源模式

Consistency

一致性

RULE: Money can't disappear or appear
WHY: Double-entry accounting principles
RUST: Transaction types with validated totals

RULE: 资金不能凭空消失或出现
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)

PurposeCrate
Decimal mathrust_decimal
Date/timechrono, time
UUIDuuid
Serializationserde
Validationvalidator
用途依赖库
小数运算rust_decimal
日期/时间chrono, time
UUIDuuid
序列化serde
验证validator

Design Patterns

设计模式

PatternPurposeImplementation
Currency newtypeType safety
struct Amount(Decimal);
TransactionAtomic operationsEvent sourcing
Audit logTraceabilityStructured logging with trace IDs
LedgerDouble-entryDebit/credit balance
模式用途实现方式
货币Newtype类型安全
struct Amount(Decimal);
事务原子操作事件溯源
审计日志可追踪性带追踪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

常见错误

MistakeDomain ViolationFix
Using f64Precision lossrust_decimal
Mutable transactionAudit trail brokenImmutable + events
String for amountNo validationValidated newtype
Silent overflowMoney disappearsChecked arithmetic

错误违反的领域规则修复方案
使用f64丢失精度改用rust_decimal
可变交易记录破坏审计追踪不可变设计 + 事件溯源
用字符串存储金额无验证机制带验证的Newtype
静默溢出资金丢失使用检查型算术运算

Trace to Layer 1

追溯到第1层

ConstraintLayer 2 PatternLayer 1 Implementation
Immutable recordsEvent sourcingArc<T>, Clone
Transaction scopeAggregateOwned children
PrecisionValue Objectrust_decimal newtype
Thread-safe sharingShared immutableArc (not Rc)

约束第2层模式第1层实现
不可变记录事件溯源Arc<T>, Clone
事务范围聚合拥有所有权的子对象
精度要求值对象rust_decimal Newtype
线程安全共享共享不可变数据Arc(而非Rc)

Related Skills

相关技能

WhenSee
Value Object designm09-domain
Ownership for immutablem01-ownership
Arc for sharingm02-resource
Error handlingm13-domain-error
场景参考内容
值对象设计m09-domain
不可变数据的所有权管理m01-ownership
使用Arc实现共享m02-resource
错误处理m13-domain-error