public class Foo {
IBar _bar;
public Foo(IBar bar) {
_bar = bar;
}
}
public class Foo {
[Inject]
IBar _bar;
}
it occurs after ctor is called.
fields can be either private or public
public class Foo {
[Inject]
public IBar Bar {
get;
private set;
}
}
public class Foo {
IBar _bar;
Qux _qux;
[Inject]
public void Init(IBar bar, Qux qux) {
_bar = bar;
_qux = qux;
}
}
Inject methods are the recommended approach for MonoBehaviours, since MonoBehaviours cannot have constructors.
Do not use inject methods for initialization logic. prefer IInitializable.Initialize or Start() methods instead, since this will allow the entire initial object graph to be created first.
Best practice is to prefer ctor/method injection compared to field/property injection.
Prefer Constructor to initialize as always as it is always guaranteed to be called once before all.
Ctor injection guarantees no circular dependency, Zenject allows the circular dependency in any usage cases.
Ctor/Method Injection is safer than Field/Property Injection.
Ctor/Method Injection makes the DI code clear to which dependencies will be used for the other developers.
DI Container is a hashtable where holds all the dependency registrations.