Register Factory
VContainer allows to register a Func<>
delegate for the creation of an instance. This is especially useful in scenarios where instance is created at any time during execution, or multiple instances are created at any time.
By registering Factory instead of instance, you can keep the dependency static and simple.
Func<>
Factory that requires only runtime parameters#
Register We can resolve like below:
Func<>
Factory that requires container dependencies and runtime parameters#
Register This method required 2 params
Func<>
: Receives Container and returns Factory.Lifetime
: Determines how often the Factory is generated. (that is, how often the outer Func is executed.)
We can resolve like below:
In this case, container.Instantiate etc. are also useful.
See Use Container directory more information.
note
Func <>
factory is like a short hand.
In complex cases, consider defining and registering your own factory class。