Add Use Parent Context option to ZenjectBinding #56
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
Add a "Use Project Context" flag to
ZenjectBinding
to allow components to be bound in a parent container.Approach
Context ProjectContext
property andbool UseProjectContext
property toZenjectBinding
SceneContext.InstallBindings()
but beforeSceneContext.InstallSceneBindings()
, ifZenjectBinding.UseParentContext
is true, set eachZenjectBinding.ParentContext
to the nearestContext
.Context.InstallSceneBindings()
for each context, bindZenjectBinding
s to the current context not only if it is equal toZenjectBinding.Context
but alsoZenjectBinding.ParentContext
.Motivation
Sub-containers are a useful abstraction. The documentation states:
The documentation appreciates how sub-containers are essential for keeping large projects organized. It emphasizes how sub-containers work especially well with the Facade pattern:
Finally, the documentation acknowledges that many of the benefits of sub-containers are hard to enjoy when dealing with MonoBehaviours and scene-related matters:
The documentation goes through an example of building a Ship prefab, that contains a
GameObjectContext
, a facade, and aZenjectBinding
. The documentation explains that without the use of the "Use Scene Context" flag on theZenjectBinding
the Ship's facade would get bound to its own localGameObjectContext
hiding it from dependants in the wider scene. By using "Use Scene Context" the facade will instead be bound in the Scene Context.This is a great serendipitous way in which the natures of sub-containers and the scene-hierarchy align to bring additional power of Zenject to Unity. However, if one tries to copy this exact pattern, for all the same reasons, and for all the same benefits even one single level deeper, then you run into troubles. If the ship had an internal prefab which warranted its own encapsulation via it's own
GameObjectContext
, facade andZenjectBinding
, there is currently no way to get the facade to bind into the parent Ship'sGameObjectContext
even though the GOCs will already work correctly in this nested fashion. There is simply no way to useZenjectBinding
to do the binding.This PR addresses that. I understand if this specific approach is not the best one and is just meant to start a conversation. It is simple and works though.