业务模型
介绍
业务模型模块(biz_model)定义业务实体与请求对象。这些模型与具体业务相关,因此从 core/model 中分离出来,放在 foundation 层。
与 core/model 的区别
| 模块 | 内容 | 示例 |
|---|---|---|
| core/model | 通用数据模型,与业务无关 | NetworkResponse、NetworkPageData、Id |
| foundation/biz_model | 业务实体与请求对象 | Auth、Goods、User、PasswordLoginRequest |
业务实体示例
文件位置:foundation/biz_model/src/main/ets/entity/Goods.ets
ts
/**
* @file 商品实体
*/
export class Goods {
/**
* 商品 ID
*/
id?: string;
/**
* 商品名称
*/
name?: string;
/**
* 商品价格
*/
price?: number;
/**
* 商品图片
*/
image?: string;
/**
* 商品描述
*/
description?: string;
}请求对象示例
文件位置:foundation/biz_model/src/main/ets/request/GoodsSearchRequest.ets
ts
/**
* @file 商品搜索请求参数
*/
export class GoodsSearchRequest {
/**
* 页码
*/
page: number = 1;
/**
* 每页数量
*/
size: number = 20;
/**
* 搜索关键词
*/
keyword?: string;
/**
* 分类 ID
*/
categoryId?: string;
}如何新增业务模型
- 根据类型在
entity/或request/下新建文件。 - 定义类并添加必要的字段注释。
- 在
Index.ets中补充导出。
ts
// Index.ets
export { NewEntity } from './src/main/ets/entity/NewEntity';
export { NewRequest } from './src/main/ets/request/NewRequest';使用示例
ts
import { Goods, GoodsSearchRequest } from "@foundation/biz-model";
// 创建请求对象
const request = new GoodsSearchRequest();
request.page = 1;
request.size = 20;
request.keyword = "手机";
// 使用实体
const goods: Goods = response.data;
console.log(goods.name);注意事项
- 业务实体字段建议使用可选类型(
?),便于处理接口返回的不完整数据。 - 请求对象可以设置默认值,减少调用时的参数配置。
- 复杂的嵌套结构建议拆分为多个实体类。