VContainer icon indicating copy to clipboard operation
VContainer copied to clipboard

Constructor Injection vs Method injection vs Property Injection

Open shaked-okami opened this issue 1 year ago • 4 comments

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

shaked-okami avatar Jul 31 '23 10:07 shaked-okami

constructor > method > field link: https://vcontainer.hadashikick.jp/resolving/constructor-injection

image

YegorStepanov avatar Sep 09 '23 18:09 YegorStepanov

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 :)

shaked-okami avatar Sep 10 '23 09:09 shaked-okami

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.

YegorStepanov avatar Sep 10 '23 23:09 YegorStepanov

Ah ok thank you very much

shaked-okami avatar Sep 11 '23 17:09 shaked-okami