Appearance
Service 业务层
Service 层负责业务逻辑处理,是 Command 和 Database 之间的桥梁。
结构
每个业务模块对应一个 Service 文件:
src-tauri/src/services/
├── mod.rs # 模块导出
└── config.rs # 配置业务逻辑示例
rust
pub struct ConfigService<'a> {
db: &'a Database,
}
impl<'a> ConfigService<'a> {
pub fn get_all(&self) -> Result<Vec<AppConfig>, AppError> {
self.db.get_all_configs()
}
pub fn set(&self, key: &str, value: &str) -> Result<(), AppError> {
self.db.upsert_config(key, value)
}
}规范
- Service 通过构造函数接收 Database 引用
- 使用
AppError作为错误类型(非CommandError) - 复杂逻辑拆分为多个小方法
- 一个 Service 对应一个业务模块