VContainer automatically collects and calls registered class constructors.
- At this time, all the parameters of the constructor must be registered.
- If the dependency cannot be resolved, throws exception when validating LifetimeScope or building Container.
Here is basic idiom with DI.
Constructors are often stripped in the IL2CPP environment.
To prevent this problem, add the
[Inject] Attribute explicitly.
If class has multiple constructors, the one with
[Inject] has priority.
Use Constructor Injection whenever possible. The constructor & readonly field idiom is:
- The instantiated object has a compiler-level guarantee that the dependencies have been resolved.
- No magic in the class code. Instantiate easily without DI container. (e.g. Unit testing)
- If you look at the constructor, the dependency is clear.
- If too many constructor arguments, it can be considered overly responsible.
MonoBehaviour cannot use a constructor. Use method injection instead.