VContainer
VContainer copied to clipboard
Constructor Injection vs Method injection vs Property Injection
I am curious in terms of runtime, what is the best way to inject?
If i am not mistaken through the constructor is the best way. However i did not see any indications within the documentations of what is the best. On top of that we cannot inject through a constructor to a mono behaviour and must use method or property injection.
I was wondering if you could shed more details about what if there are any implications on runtime. Thank you very much
constructor > method > field link: https://vcontainer.hadashikick.jp/resolving/constructor-injection
Thank you for replying :) I have seen that in the documentation, however my main question about mono behaviors was in terms of usage whether on runtime, is there any difference on what to use between property or field injection? I know in zenject for example that you should use: Constructor, then method (if constructor is not possible) and only then property.
So do you know anything about that in vcontainer?
Thank you very much :)
There is another page (https://vcontainer.hadashikick.jp/resolving/method-injection) that says:
If you need to inject dependencies into a type but can't do so with constructors, consider using method injection instead.
In my experience, it is also better to use method injection rather than field injection. Field injection doesn't work in some places (it's a bug) and is difficult to debug.
Ah ok thank you very much