Resolution
A consumer declares what it needs, and the container fills it. The dependency can be the service itself, a way to get it later or more than once, every implementation at once, or a dictionary of them. This section explains each shape and how you resolve services from the container at the edges of your app.
Dependency shapes
Each shape below works as a constructor parameter, a factory parameter or an injected property. T is the service, TArg… the runtime arguments of a service built from them.
| Shape | What you get | Works over async-tainted T | Under [FromKey] | Resolvable by type | Explained on |
|---|---|---|---|---|---|
T | The service. | Yes, the consumer becomes async-tainted too. | Yes | Yes, also with a key | Resolving services |
Func<T> | A factory that resolves T on each call. | No, AWT119 or AWT120 | Yes | Yes | Relationships |
Lazy<T> | T, resolved once on first access. | No, AWT119 or AWT120 | Yes | Yes | Relationships |
Owned<T> | T built in a scope of its own, disposed with the handle. | No, AWT119 or AWT120 | Yes | Yes | Owned |
Func<Owned<T>> | A new Owned<T> handle on each call. | No, AWT119 or AWT120 | Yes | Yes | Owned |
Task<T> | A task that completes once T is initialized. | Yes, the consumer stays synchronous. | Yes | For an async-tainted T | Relationships |
Func<Task<T>> | A factory that resolves and initializes T on each call. | Yes, the consumer stays synchronous. | Yes | For an async-tainted T | Relationships |
Lazy<Task<T>> | T, resolved and initialized once on first access. | Yes, the consumer stays synchronous. | Yes | For an async-tainted T | Relationships |
Task<Owned<T>> | An Owned<T> handle once T is initialized. | Yes, the consumer stays synchronous. | Yes | For an async-tainted T | Owned |
Func<Task<Owned<T>>> | A new initialized Owned<T> handle on each call. | Yes, the consumer stays synchronous. | Yes | For an async-tainted T | Owned |
Func<TArg…, T> | A factory that builds T from your arguments. | No, AWT119 or AWT120 | Yes | Yes | Runtime arguments |
Func<TArg…, Owned<T>> | A factory that builds T from your arguments into a handle. | No, AWT119 or AWT120 | Yes | Yes | Runtime arguments |
Func<TArg…, Task<T>> | A factory that builds and initializes T from your arguments. | Yes, the consumer stays synchronous. | Yes | Yes | Runtime arguments |
Func<TArg…, Task<Owned<T>>> | The same, handing back an Owned<T> handle. | Yes, the consumer stays synchronous. | Yes | Yes | Runtime arguments |
IEnumerable<T>, T[], IReadOnlyList<T>, IReadOnlyCollection<T>, IList<T>, ICollection<T> | Every registration of T. | No, AWT122 | Yes, the registrations under the key | Yes | Collections |
IAsyncEnumerable<T> | Every registration of T, each awaited as you enumerate. | Yes, the consumer becomes async-tainted too. | Yes, the registrations under the key | Yes | Collections |
Task<IReadOnlyList<T>> and the Task of the other collection shapes | Every registration of T once all are initialized. | Yes, the consumer stays synchronous. | Yes, the registrations under the key | Yes | Collections |
IReadOnlyDictionary<TKey, T> | Every keyed registration of T, by its string or enum key. | No, AWT122 | No, AWT160 | Yes | Keyed dictionaries |
Task<IReadOnlyDictionary<TKey, T>> | The same dictionary once every member is initialized. | Yes, the consumer stays synchronous. | No, AWT160 | Yes | Keyed dictionaries |
IReadOnlyDictionary<Type, Func<TBase>> | A factory per closing of a generic service, by its type argument. | No, AWT119 or AWT120 | No, AWT213 | No | Keyed dictionaries |
| An injected property of any shape above | The shape, set on the property after construction. | As for its shape. | Yes, through Key or [FromKey] | Not applicable | Property injection |
How to read the columns:
- Works over async-tainted
T.Tis async-tainted when it is async-initialized or depends on an async-initialized service. A synchronous relationship reports AWT119 whenTitself is async-initialized and AWT120 when it only depends on one. A synchronous collection or dictionary reports AWT122 for any async-tainted member. "No" holds in the strict default. WithAsyncSafety.SyncAfterIniton the container, a synchronous shape may reach an async-initialized service after warm-up, see Async initialization. - Under
[FromKey]. The shape wraps the registration, or holds the registrations, under that key. See Keyed services. - Resolvable by type.
Resolve<…>(),TryResolve<…>()orResolveAsync<…>()hands out the shape from the root or a scope, with these limits:- A keyed resolve serves only the service itself, so every other shape under a key reaches a consumer only through
[FromKey]. - The synchronous relationships come with a service that is not async-tainted and the
Taskrelationships with an async-tainted one. Asking for the other throws and names the shape to use. A service with runtime arguments always comes withFunc<TArg…, Task<T>>andFunc<TArg…, Task<Owned<T>>>, async-tainted or not. - A collection shape is resolvable only when its element type has a registration, so a collection that is injected empty throws when resolved. A synchronous one also needs that no member is async-tainted.
IReadOnlyDictionary<TKey, T>and itsTaskform are resolvable only when every key ofTis of one type, and the synchronous dictionary only when no member is async-tainted.- In the strict default, the root withholds a shape that builds a new disposable on each call, such as a
Func<T>over a disposable transient, see Relationships.
- A keyed resolve serves only the service itself, so every other shape under a key reaches a consumer only through
Some dependencies are errors:
- A service built from runtime arguments comes only through its
Func<TArg…, …>shapes. A dependency on the service itself,Lazy<T>,Owned<T>orTask<T>is AWT115. - A
Funcwhose arguments do not match is AWT113. Lazy<Owned<T>>andLazy<Task<Owned<T>>>are AWT121, becauseLazywould memoize a single disposal handle. TakeOwned<T>,Func<Owned<T>>orTask<Owned<T>>instead.- Relationships do not nest: a relationship over a collection or a collection of relationships is an error unless that type is registered as a service, see Relationships.
- A relationship over another relationship, such as
Func<Lazy<T>>,Lazy<Func<T>>,Func<TArg…, Lazy<T>>orTask<Lazy<T>>, is not built. Only the combinations in the table above are, so such a dependency is a missing dependency (AWT101).
Where to go next
- Resolving services for
Resolve,TryResolveandResolveAsync. - Relationships for
Func,LazyandTask. - Collections for every implementation at once.