Calling Inject multiple times when class is already injected #833
|
Hi Team I have a situation where i cant control the Create method of a pool, so when i get the item from pool i call inject, and it works Please advise |
Replies: 2 comments
|
This sounds more like an architecture problem, why you can't control injection and must call it multiple times in the first place? That is odd in the first place. But if you must do that, the workaround you mentioned is kind of fine, although, you can make [Inject] methods instead of read-only fields and check inside them if a local field already has that dependency injected or not (at least, in your case, the responsibility to handle multiple calls of injection is fairly belongs to the class that gets injected with dependencies). Btw the process of injection itself takes minimal amount of hardware resources, with what you have it won't be noticeable on any platform. But still, the way it is implemented is not a good thing |
This sounds more like an architecture problem, why you can't control injection and must call it multiple times in the first place? That is odd in the first place. But if you must do that, the workaround you mentioned is kind of fine, although, you can make [Inject] methods instead of read-only fields and check inside them if a local field already has that dependency injected or not (at least, in your case, the responsibility to handle multiple calls of injection is fairly belongs to the class that gets injected with dependencies).
Btw the process of injection itself takes minimal amount of hardware resources, with what you have it won't be noticeable on any platform. But still, the way it…