我希望DI用于存储库接口和服务接口(如Spring ),使用typedi。
下面的代码(用于存储库的DI示例代码)在调用api时正确工作。
存储库
import { Service } from "typedi";
import { EntityRepository, Repository } from "typeorm";
import { User } from "../entity/User";
export interface IUserRepository {
findAllUsers();
findUserByUserId(id: number);
addUser(user: any);
removeUserByUserId(user: any);
}
@Service()
@EntityRepository(User)
export class UserRepository
extends Repository<User>
implements IUserRepository {
findAllUsers() {
return this.find();
}
findUserByUserId(id: number) {
return this.findOne({ id });
}
addUser(user: any) {
return this.save(user);
}
removeUserByUserId(user: any) {
return this.remove(user);
}
}服务
import { Service } from "typedi";
import { InjectRepository } from "typeorm-typedi-extensions";
import { User } from "../entity/User";
import { UserRepository } from "../repository/userRepository";
export interface IUserService {
all();
one(id: any);
save(user: any);
remove(id: any);
}
@Service()
export class UserService implements IUserService {
@InjectRepository(User)
private userRepository: UserRepository;
async all() {
return this.userRepository.findAllUsers();
}
async one(id: any) {
let user = await this.userRepository.findUserByUserId(id);
if (typeof user === "undefined") {
throw new Error(`userId ${id} is not found.`);
}
return user;
}
async save(user: any) {
return this.userRepository.addUser(user);
}
async remove(id: any) {
let userToRemove = await this.userRepository.findUserByUserId(id);
if (typeof userToRemove === "undefined") {
throw new Error(`userId ${id} is not found.`);
}
return this.userRepository.removeUserByUserId(userToRemove);
}
}但是,当我想使用接口注入存储库时,它不能正常工作,并出现错误消息。
建筑是成功的。调用api时会出现错误消息。
另外,当调用api时,错误消息第一次和第二次不同。
像这样
存储库
import { Service } from "typedi";
import { InjectRepository } from "typeorm-typedi-extensions";
import { User } from "../entity/User";
import { UserRepository } from "../repository/userRepository";
...
@Service()
export class UserService implements IUserService {
@InjectRepository(User)
private userRepository: UserRepository;
async all() {
return this.userRepository.findAllUsers();
}
...
}第一次错误消息。
{
"name": "CustomRepositoryNotFoundError",
"message": "Custom repository Object was not found. Did you forgot to put @EntityRepository decorator on it?",
"stack": "CustomRepositoryNotFoundError: Custom repository Object was not found. Did you forgot to put @EntityRepository decorator on it? (The following is omitted)"
}第二次错误消息。
{
"name": "TypeError",
"message": "Cannot read property 'all' of undefined",
"stack": "TypeError: Cannot read property 'all' of undefined(The following is omitted)"
}服务也不能正常工作。
下面的代码是成功代码。
控制器
import {
Get,
JsonController,
OnUndefined,
Param,
Post,
Body,
Delete,
} from "routing-controllers";
import { Inject, Service } from "typedi";
import { UserService } from "../service/userService";
@Service()
@JsonController("/users")
export class UserRestController {
@Inject()
private userService: UserService;
@Get("/")
getAll() {
return this.userService.all();
}
@Get("/:id")
@OnUndefined(404)
getOne(@Param("id") id: number) {
return this.userService.one(id);
}
@Post("/")
add(@Body() user: any) {
return this.userService.save(user);
}
@Delete("/:id")
delete(@Param("id") id: number) {
return this.userService.remove(id);
}
}但下面的情况不太好。在这种情况下,即使构建也不能工作。
控制器
import {
Get,
JsonController,
OnUndefined,
Param,
Post,
Body,
Delete,
} from "routing-controllers";
import { Inject, Service } from "typedi";
import { IUserService } from "../service/userService";
@Service()
@JsonController("/users")
export class UserRestController {
@Inject()
private userService: IUserService;
@Get("/")
getAll() {
return this.userService.all();
}
@Get("/:id")
@OnUndefined(404)
getOne(@Param("id") id: number) {
return this.userService.one(id);
}
@Post("/")
add(@Body() user: any) {
return this.userService.save(user);
}
@Delete("/:id")
delete(@Param("id") id: number) {
return this.userService.remove(id);
}
}错误消息
CannotInjectValueError: Cannot inject value into "UserRestController.userService". Please make sure you setup reflect-metadata properly and you don't use interfaces without service tokens as injection value.正如在开始时所描述的,我希望DI用于存储库接口和服务接口(如Spring ),使用the DI。
TypeDI不能这样用吗?还是我的代码错了?请帮帮我。
谢谢。
发布于 2021-07-03 22:31:27
接口是短暂的,它们实际上并不存在于您的代码运行时,它们只存在于您编写代码时。另一方面,类几乎是有形的,它们总是存在的。这就是为什么当您使用UserService类时,它可以工作,但是当您使用IUserService接口时,它不能工作。
您所得到的错误告诉您一些有用的信息:
,请确保…不使用没有服务令牌的接口作为注入值.
https://stackoverflow.com/questions/67879609
复制相似问题