Container API
You can also access the DI container directly via IObjectResolver
. VContainer automatically registers IObjectResolver
and injects it wherever it's needed, so you can obtain it just as you would for any other dependency.
For example:
If you regularly use IObjectResolver.Inject
in a certain pattern, consider writing an extension method for it. In fact, almost every IObjectResolver
API is an extension method (including in the preceding code sample).
tip
You should only need to use IObjectResolver.Resolve
explicitly if the other supported injection techniques don't fit your needs, or if using a VContainer callback that exposes IObjectResolver
(e.g. build callbacks or certain factory functions). All resolution techniques have similar performance if you use the IL generator, but directly using Resolve
requires more code and obscures your intent.
LifetimeScope
has an IObjectResolver
reference through its Container
property. VContainer also registers it automatically, but you won't need it too often after the container is built.