属性 基于抽象的注入 Class
Property-based injection with an Abstract Class
我正在尝试添加一个抽象方法来序列化我的响应对象,以便散列 id。这将需要有一个基于 属性 的注入,以便不通过构造函数传递大量变量。
将用于序列化的class:
import { AbstractHash } from 'src/global/abstract.hash';
import { IItems } from '../items.interface';
import { Injectable } from '@nestjs/common';
@Injectable()
export class ResponseItem extends AbstractHash implements IItems {
name: string;
price: number;
type: string;
constructor(partial: Partial<IItems>) {
super();
Object.assign(this, partial);
}
}
摘要class:
import { HashIdHelper } from 'src/hash-id.helper';
import { Exclude, Expose } from 'class-transformer';
import { Inject, Injectable } from '@nestjs/common';
@Injectable()
export abstract class AbstractHash {
@Exclude()
id: number;
//@Inject('HASHID_HELPER') // Neither works
@Inject(HashIdHelper)
public readonly hashIdHelper: HashIdHelper;
@Expose({ name: 'id' })
encodedId() {
console.log(this.hashIdHelper); // -> undefined
console.log(HashIdHelper);
return this.hashIdHelper.encode(this.id);
}
}
控制器:
// imports
@UseInterceptors(ClassSerializerInterceptor)
@Controller('items')
export class ItemsController {
constructor(
private readonly itemsService: ItemsService,
@Inject('HASHID_HELPER') private readonly hash: HashIdHelper, // <-- Ok
) {}
@Get(':id')
async findOne(@Param('id') id: string) {
console.log(this.hash.encode(+id)); // <-- Ok
const item = await this.itemsService.findOne(parseInt(id));
console.log(new ResponseItem(item));
return new ResponseItem(item); // <-- undefined attribute, see above
}
}
存储库:https://github.com/befabry/caddie/tree/hash_id
感谢您的帮助
您正在调用 new ClassInstance()
,因此您可以 100% 控制 class 及其属性。 Nest 不会在手动实例化的 class 上注入任何东西,因为它首先不受 Nest 的生命周期管理。您可以使用 moduleRef.create
让 Nest 为您创建一个实例,但是一旦您使用关键字 new
您就负责该实例。
我正在尝试添加一个抽象方法来序列化我的响应对象,以便散列 id。这将需要有一个基于 属性 的注入,以便不通过构造函数传递大量变量。
将用于序列化的class:
import { AbstractHash } from 'src/global/abstract.hash';
import { IItems } from '../items.interface';
import { Injectable } from '@nestjs/common';
@Injectable()
export class ResponseItem extends AbstractHash implements IItems {
name: string;
price: number;
type: string;
constructor(partial: Partial<IItems>) {
super();
Object.assign(this, partial);
}
}
摘要class:
import { HashIdHelper } from 'src/hash-id.helper';
import { Exclude, Expose } from 'class-transformer';
import { Inject, Injectable } from '@nestjs/common';
@Injectable()
export abstract class AbstractHash {
@Exclude()
id: number;
//@Inject('HASHID_HELPER') // Neither works
@Inject(HashIdHelper)
public readonly hashIdHelper: HashIdHelper;
@Expose({ name: 'id' })
encodedId() {
console.log(this.hashIdHelper); // -> undefined
console.log(HashIdHelper);
return this.hashIdHelper.encode(this.id);
}
}
控制器:
// imports
@UseInterceptors(ClassSerializerInterceptor)
@Controller('items')
export class ItemsController {
constructor(
private readonly itemsService: ItemsService,
@Inject('HASHID_HELPER') private readonly hash: HashIdHelper, // <-- Ok
) {}
@Get(':id')
async findOne(@Param('id') id: string) {
console.log(this.hash.encode(+id)); // <-- Ok
const item = await this.itemsService.findOne(parseInt(id));
console.log(new ResponseItem(item));
return new ResponseItem(item); // <-- undefined attribute, see above
}
}
存储库:https://github.com/befabry/caddie/tree/hash_id
感谢您的帮助
您正在调用 new ClassInstance()
,因此您可以 100% 控制 class 及其属性。 Nest 不会在手动实例化的 class 上注入任何东西,因为它首先不受 Nest 的生命周期管理。您可以使用 moduleRef.create
让 Nest 为您创建一个实例,但是一旦您使用关键字 new
您就负责该实例。