-
Hi 👋 When you want to import and register a provider (a service, for example) you can do it in the following way, as explained in the documentation and as you commented in this issue. // MyService.ts
import { Injectable } from "@tsed/di";
@Injectable()
export class MyService {
constructor() {}
} // Server.ts
import { MyService } from "./MyService"; // if the symbole is used
import "./MyService"; // if the symbole isn't used but you have use it because you use hooks But reviewing the documentation I saw that using the
// MyService.ts
import { Injectable } from "@tsed/di";
@Injectable()
export class MyService {
constructor() {}
} // Server.ts
import { Configuration } from "@tsed/di";
import { MyService } from "./MyService";
@Configuration({
imports: [MyService]
})
export class Server {} So, looking at both alternatives it seems that they serve the same purpose, right? The question is: What should Thanks in advance 🙏 Regards! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hello @borjapazr imports was introduced later to cover a specific usage. In most case, ES6 import is enough. In your given example adding MyService to imports field special effect because your class is already decorated by But in this example // Server.ts
import { Configuration } from "@tsed/di";
class MyService {
}
@Configuration({
imports: [MyService] // add MyService to the registry because MyService isn't decorated.
})
export class Server {} But isn't the initial reason of the imports presences. imports field is here for the following reason:
Example: imports: [
{
token: TimeslotsRepository, // baseClass
useClass: DynamoDBTimeslotsRepository // DynamoDB version of timeslotRepository in Serverless context
}
] So, imports it's here to fine tuning the injected service by the DI where the decorator doesn't solve. See you |
Beta Was this translation helpful? Give feedback.
Hello @borjapazr
imports was introduced later to cover a specific usage.
In most case, ES6 import is enough. In your given example adding MyService to imports field special effect because your class is already decorated by
@Injectable
.But in this example
imports
is required:But isn't the initial reason of the imports presences. imports field is here for the following reason:
@Module
or@Configuration