Advanced Bindings
Custom Factories
When the instance creation process is complex due to the factory, there are cases where it cannot be handled by coding bindings alone. For example, if you have a factory that involves API communication, you may need to communicate and use the results to generate instances.
In such cases, you can use a custom factory to code the factory's creation process.
Assume that ISomeApi is already bound to the DI container.
A factory that uses the results of API communication to create IPlayer
can be written by inheriting IFactory<T>
as follows.
Inject the necessary instances with [Inject] Construct(...)
and describe the process of creating instances in CreateAsync()
.
To bind a custom factory, specify the type of the custom factory for AsCustomFactory<T>
as follows.
This binds AsyncPlayerFactory
to the DI container.
Custom Resolvers
You can also customize the way the DI container creates instances. For example, a resolver that retrieves IPlayer
from a repository can be written by inheriting IResolver<T>
as follows.
Like a custom factory, you can inject the necessary instances and generate instances in the way you want. The args
argument to ResolveAsync
is passed as an array of arguments specified in Args(...)
in the binding coding.
To bind using a custom resolver, specify FromResolver<T>
as follows.
Passing Arguments through Factories
If a constructor or injection point method requires arguments, specify the type of the argument and call AsFactory<TArg1, ...>()
.
Consider a type with a constructor argument as follows.
In the case of a class with arguments in the constructor like this, specify the int type as follows.
To pass an argument, pass it as an argument to CreateAsync()
.
Passing Arguments through Custom Factories
Depending on the number of arguments, define a custom factory by inheriting IFactory<TArg1, ... T>.