-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get/inject an array of service instances #24
Comments
Can you provide an example of functionality you need? |
no there is no such way, because each token holds single unique instance of the object |
Any plans to support such a feature in the future? Most if not all DI frameworks support this kind of injection. |
probably this can be implemented in the future. Need to start with a design proposal, simply |
For starters what about import { Container } from "typedi";
@service('handler')
class HandlerX implements Handler {...}
@service('handler')
class HandlerY implements Handler {...}
...
let allHandlera = Container.getAll(Handler) And afterwards we can find a suitable way to use decorators for this functionality. |
In my opinion, you should be able to define the scope of the beans/tokens. For example, Spring has a couple of different scopes for beans, where the following 3 are the most relevant:
Here are examples how prototype and request scopes could be implemented with typeid (btw. I also likes @alexproca idea): Prototype scope
Request scope
Initialize a new request scope
|
I think it could be easier adding a |
what @steven166 says does make sense, we'll need to implement this functionality |
I'm also looking for something like this. Maybe it could be something like "tagged services"?
|
I've implemented this feature and released it in Using service groupsYou can group multiple services into single group tagged with service id or token. // Factory.ts
export interface Factory {
create(): any;
}
// FactoryToken.ts
export const FactoryToken = new Token<Factory>("factories");
// BeanFactory.ts
@Service({ id: FactoryToken, multiple: true })
export class BeanFactory implements Factory {
create() {
console.log("bean created");
}
}
// SugarFactory.ts
@Service({ id: FactoryToken, multiple: true })
export class SugarFactory implements Factory {
create() {
console.log("sugar created");
}
}
// WaterFactory.ts
@Service({ id: FactoryToken, multiple: true })
export class WaterFactory implements Factory {
create() {
console.log("water created");
}
}
// app.ts
// now you can get all factories in a single array
const factories = Container.getMany(FactoryToken); // factories is Factory[]
factories.forEach(factory => factory.create()); Using multiple containers and scoped containersBy default all services are stored in the global service container, If you want your services to behave and store data inside differently, // QuestionController.ts
@Service()
export class QuestionController {
constructor(protected questionRepository: QuestionRepository) {
}
save() {
this.questionRepository.save();
}
}
// QuestionRepository.ts
@Service()
export class QuestionRepository {
save() {
}
}
// app.ts
const request1 = { param: "question1" };
const controller1 = Container.of(request1).get(QuestionController);
controller1.save("Timber");
Container.reset(request1);
const request2 = { param: "question2" };
const controller2 = Container.of(request2).get(QuestionController);
controller2.save("");
Container.reset(request2); In this example
@Service({ global: true })
export class QuestionUtils {
} And this global service will be the same instance across all containers. |
IMHO service groups has a problem. When you want to have several implementations of the same interface, most of the cases what you want at the end is to inject all them into an array in another class without having to know concrete implementations of that interface. In this approach, you have to import all possible implemtantions what breaks the concept of DI and IOC. Any idea about how handling this scenario? Thanks! |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Is there a way to inject all service instances (e.g. named services) into an array?
This is a "must have" feature, but I could not find anything in the docs.
The text was updated successfully, but these errors were encountered: