-
Notifications
You must be signed in to change notification settings - Fork 24
Scopes
All registrations have a particular Scope. This tells StrongInject how long an instance resolved using that registration should live for, and how widely it should be shared.
There are currently 3 possible scopes. The default scope is InstancePerResolution
but the scope can be modified per registration via the Scope
enum.
The default scope.
A single instance is shared between all dependencies created for a single resolution.
For example if A
depends on B
and C
, and B
and C
both depend on an instance of D
, then when A
is resolved B
and C
will share the same instance of D
.
Note every SingleInstance dependency defines a separate resolution, so if B
and/or C
are SingleInstance
they would not share an instance of D
. Similarly every lambda defines a separate resolution, so if A
depends on Func<B>
, then each time Func<B>
is invoked a fresh instance of both B
and D
will be created.
A new instance is created for every usage.
For example even if type B
appears twice in the constructor of A
, two different instances will be passed into the constructor.
A single instance will be shared across all dependencies, from any resolution.
A SingleInstance
instance will be disposed when the container is disposed (except Instance
fields and properties which are never disposed).
An InstancePerResolution
or InstancePerDependency
instance will be disposed when StrongInject knows it can no longer be used. This means:
-
If it is a dependency of a
SingleInstance
it will be disposed when the container is disposed. -
Else, if it was created as part of a call to
Run
/RunAsync
, it will be disposed, once the delegate paramater toRun
/RunAsync
completes. -
Else, if it was created as part of a call to
Resolve
/ResolveAsync
, it will be disposed once the returnedIOwned
/IOwnedAsync
is disposed.