-
Notifications
You must be signed in to change notification settings - Fork 24
Resolution
Resolution is how the container create/provides an instance of a type. This can be when you ask for the instance directly, or it may be needed as a dependency for another resolution.
To find out more about using containers see the documentation.
Resolution is recursive. When you request an instance of a type, StrongInject will find the best way to resolve it. If doing so requires some dependencies, StrongInject will resolve them too, and so on.
For example:
using StrongInject;
[Register(typeof(A))]
[Register(typeof(B))]
[Register(typeof(C))]
public class MyContainer : IContainer<A>() {}
public class A { public A(B b){} }
public class B { public B(C c){} }
public class C {}
- StrongInject will try to resolve
A
, and finds that we've registered the typeA
, so will try to useA
's constructor to create an instance ofA
. -
A
's constructor requires an instance ofB
as a parameter. - StrongInject will try to resolve
B
, and finds that we've registered the typeB
, so will try to useB
's constructor to create an instance ofB
. -
B
's constructor requires an instance ofB
as a parameter. - StrongInject will try to resolve
C
, and finds that we've registered the typeC
, so will try to useC
's constructor to create an instance ofC
. -
C
's constructor has no parameters, and so we are done.
When StrongInject needs to resolve an instance of a type, it will check the following potential providers one by one, and stop once it finds any that are capable of providing the instance.
StrongInject can automatically resolve delegates. When resolving the return type of the delegate, StrongInject will make use of any of the delegate parameters for use as dependencies of the return type. For a delegate resolved inside a delegate, inner delegate parameters override outer delegate parameters of the same type. Each delegate parameter can only be used to resolve an instance of the exact same type as the parameter. It cannot be used to resolve as the parameters base classes, or interface implementations.
To find out more about using registration see the documentation. Most registrations are non generic, and StrongInject checks to see if there are any such registrations for the type. If there is a best registration, this is used. If there are multiple best registrations, then StrongInject produces an error message and stops.
To find out more about using registration see the documentation. Some registrations can be generic, and if there are no non generic registrations for the type, StrongInject checks to see if any generic registrations can be used to create the type by substituting in the correct type parameters. If there is a best such registration, this is used. If there are multiple best registrations, then StrongInject produces an error message and stops.
If the type is a delegate type, StrongInject will automatically create a delegate which resolves an instance of the delegate return type, using the delegate parameters as dependencies if necessary.
If the return type is a TaskT>
or ValueTask<T>
, then StrongInject will create an async delegate which asynchronously resolves an instance of T
, using the delegate parameters as dependencies if necessary.
If the type is an array, StrongInject will find all non generic and generic registrations which can be used to resolve the element type of the array. It will then resolve all of them and create an array containing all of these instances.