我有一些服务可以通过 id 返回用户的数据。 用户已存储在请求( session )中 - 无需每次都获取它。
问题是参数的最佳实践是什么。用户的 ID (SOLID) 或用户对象(更多验证/输入)。
示例:
//...
// this
public async getByUser(user: User) {
return await Transaction.findAll({
where: { userId: user.id }
});
}
// or this
public async getByUser(userId: number) {
return await Transaction.findAll({
where: { userId }
});
}
最佳答案
要么。真的。 只要保持命名正确即可。
函数名称越具有声明性,困惑就越少
//...
// this
public async getByUser(user: User) {
return await Transaction.findAll({
where: { userId: user.id }
});
}
// or this
public async getByUserId(userId: number) {
return await Transaction.findAll({
where: { userId }
});
}
关于node.js - 函数参数 - id 与对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55810121/