Skip to content

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

Open
@fake364

Description

@fake364

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()
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questionQuestions about the usage of the library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions