我想使用 typedi 为存储库接口和服务接口进行 DI,例如 Spring
I'd like to DI for repository interface and service interface like Spring using typedi
我想使用 typedi 为存储库接口和服务接口进行 DI,例如 Spring。
调用 api 时,以下代码(存储库的 DI 示例代码)工作正常。
存储库
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.
如开头所述,我想使用 typedi 为存储库接口和服务接口进行 DI,例如 Spring。
TypeDI 不能这样用?
或者我的代码是错误的?
请帮助我。
谢谢。
接口是短暂的,当您的代码 运行 执行其工作时它们实际上并不存在,它们仅在您编写代码时存在。另一方面,类 几乎是有形的,它们始终存在。这就是为什么当你使用 UserService
class 时,它可以工作,但是当你使用 IUserService
接口时,它不起作用。
您收到的错误告诉您一些有用的信息:
Please make sure […] you don't use interfaces without service tokens as injection value.
我想使用 typedi 为存储库接口和服务接口进行 DI,例如 Spring。
调用 api 时,以下代码(存储库的 DI 示例代码)工作正常。
存储库
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.
如开头所述,我想使用 typedi 为存储库接口和服务接口进行 DI,例如 Spring。
TypeDI 不能这样用?
或者我的代码是错误的?
请帮助我。
谢谢。
接口是短暂的,当您的代码 运行 执行其工作时它们实际上并不存在,它们仅在您编写代码时存在。另一方面,类 几乎是有形的,它们始终存在。这就是为什么当你使用 UserService
class 时,它可以工作,但是当你使用 IUserService
接口时,它不起作用。
您收到的错误告诉您一些有用的信息:
Please make sure […] you don't use interfaces without service tokens as injection value.