typedi icon indicating copy to clipboard operation
typedi copied to clipboard

question: what is the point of Inject if we can use Container.get

Open fake364 opened this issue 6 months ago • 0 comments

I was trying to find an answer and there were questions that sounded similar but they didn't answer question. So the question is: What is the point to use @Inject decorator if we can use Container.get ? Besides getting an instance of the service Container.get initializes all dependencies on target class (why don't we have separate method for that). But I noticed that if I don't use Container.get, Inject would leave all dependencies undefined. So if I don't want to use Container.get anywhere why can't I use just @Service and @Inject as I intended to use Inject as if it would have been Container.get?

@Service
class Test {
private field=Container.get(Service2)
}

So how to use Inject and Service and make all services initialized with dependencies without writing Container.get for all services.

Also I noticed that it won't inject recursively all dependencies e.g. if I have following structure

@Service
class CommentsService {
@Inject
otherService...
...
}

@Service
class CommentsController {
@Inject
commentsService:CommentsService;
....
}

@Service
class CommentsRoute {
@Inject
commentsController:CommentsController;
.....
}
app.use([Container.get(CommentsRoute)])

So the only way to use that I see

@Service
class CommentsService {
@Inject
otherService...
...
}

@Service
class CommentsController {
commentsService=Container.get(CommentsService);
....

printSomething(){
this.commentsService.print()
}
}

fake364 avatar Jul 30 '24 18:07 fake364