Binding
Registering types or instances with the DI container is called binding.
- Instance provision methods
Singleton
Factory
Fixed instance
Temporary generation (Transient)
- Types of objects
Type
Game object
Prefab
Addressable Asset
- Other
Factory
Arguments
By combining these, you can perform various bindings flexibly with simple coding.
Specifying the type to bind
The simplest type binding
Register the type SomeClass
in the DI container. An instance is created only once in the context space when there is an injection target and is cached (same as AsCached()
).
Interface
SomeClass
needs to implement ISomeInterface
. An instance of SomeClass
is injected into ISomeInterface
.
You can also write it as follows.
Type inherited from MonoBehaviour
The coding for regular binding is the same.
You can specify the generation position as follows.
Generate at the top level of the scene to which the context belongs
container.Bind<SomeComponent>() .UnderSceneRoot();Generate as a child object of the specified
Transfrom
container.Bind<SomeComponent>() .Under(someTransform);AddComponent
to the specifiedGameObject
container.Bind<SomeComponent>() .On(someGameObject);
Instance
someClassInstance is injected as is.
You can also write it as follows.
Generate an instance from a prefab
SomeComponent needs to be attached to the root object of the prefab.
You can specify the generation position as follows.
Generate at the top level of the scene to which the context belongs
container.Bind<SomeComponent>() .UnderSceneRoot();Generate as a child object of the specified
Transfrom
container.BindPrefab<SomeComponent>(somePrefab) .Under(someTransform);
Load from Addressable Asset
AsserReference
is a type provided by the Addressable Asset System and represents a reference to an asset.
You can also specify RuntimeKey
directly using BindAssetRuntimeKey
.
Load a prefab from Addressables Asset and generate an instance
You can load a prefab from the Addressable Asset System and generate an instance using BindPrefabAssetReference
. PrefabAssetReference
is a type provided by Doinject for referencing prefabs. SomeComponent needs to be attached to the root object of the prefab.
You can also specify RuntimeKey
directly using BindPrefabAssetRuntimeKey
.
You can specify the generation position as follows.
Generate at the top level of the scene to which the context belongs
container.BindPrefabAssetReference<SomeComponent>(prefabAssetReference) .UnderSceneRoot();Generate as a child object of the specified
Transfrom
container.BindPrefabAssetReference<SomeComponent>(prefabAssetReference) .Under(someTransform);
Specifying the instance generation method
Cache
By calling AsCached()
, you explicitly indicate that it will be cached. Even if there is no coding, it is cached by default. You can also write it as follows.
Singleton
It generates only one instance in the context. Unlike AsCached()
, it generates an instance even if there is no injection target.
Temporary (Transient)
Instances generated by AsTransient() are generated separately for each injection target. Since the generated instance may not be automatically destroyed when the DI container is released, care is needed.
Arguments
Assume that SomeClass has the following constructor.
For such types, you need to pass arguments when generating instances, You need to define arguments with Args().
Injecting another bound type
Assume that SomeClass has the following constructor.
And, ISomeInterface is bound.
In this case, when SomeClass
is generated, an instance of SomeOtherClass
is automatically injected into ISomeInterface
.
Next, consider a case with a more complex constructor like the following.
By specifying the arguments that the DI container cannot resolve with Args()
, it is possible to generate instances.
Injection is performed for arguments that the DI container cannot resolve according to the order of arguments specified in Args. Therefore, even for the following constructor, injection is possible without any problem with the above binding description.
Factory
The way to generate a factory is to just call AsFactory()
on the binding description. IFactory<T>
corresponding to the binding description is automatically bound.
By describing this way, IFactory<SomeClass>
is bound.
By defining a factory, a factory that generates the specified instance can be injected. You can generate an instance by calling the CreateAsync()
method on the factory instance.
To use a factory, inject IFactory<SomeClass>
as follows.
You can use the binding syntax shown so far as is, so you can combine them freely to perform various bindings flexibly with simple coding.
Here are some examples.