问题描述
我有一个User
实体,该实体具有generateToken
方法。此方法具有一个称为cipher
的依赖项,我想使用Inverisfy注入该依赖项。这可能吗?如果没有,那有什么选择呢?
研究
我见过this issue,但没有给出答案。我也不同意将我的功能移入服务层。我想要丰富的域模型,而生成令牌肯定是丰富的域模型的工作。
我尝试过的
我可以通过向public cipher: ICipher
添加一个UserEntity
属性,然后注入一个包装器类UserRepository
来共同破坏事物,就像这样:
@injectable()
export class UserRepository {
private repository: Repository<UserEntity>;
constructor(@inject(TYPES.Cipher) private cipher: ICipher) {
this.repository = getRepository(UserEntity);
}
create(data: UserEntity): UserEntity {
const user = this.repository.create(data);
user.cipher = this.cipher;
return user;
}
}
但是包装每一个Repository
方法都不是一个好的解决方案。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)