Repository navigation
2.5.8
1. Generated code grows with the graph, not with the number of paths through it
Graphs with delegates and cycles could still make the generated code and the build time grow with every path that leads to a dependency. This release removes most of the remaining sources of that growth.
- The graph under a
Func<T1, T>delegate is rewritten once per override scope instead of once per dependency path, including nested delegates, back-references and inner delegates that pass the same argument again. Fixes #172, #175, #176, #177 and #178. - An object made by the lambdas of a delegate is created by a local function instead of being repeated in every lambda, and a singleton reached inside delegates with arguments is created in one place. Fixes #184 and #186.
- Screens that open each other through delegates, a typical navigation graph, are created once by local functions instead of once per navigation path, also when they use an instance that a singleton uses too and when a
PerBlockinstance is under their factories. Fixes #192, #195 and #201. - Arrays and spans, transient diamonds that share a
PerBlockdependency andIAsyncEnumerable<T>dependencies no longer multiply the code either. Fixes #187, #188 and #189. - A transient instance made by a factory is created by a local function when that keeps the code smaller, unless the factory reads
ctx.ConsumerTypeorctx.ConsumerTypes, since then each consumer needs its own copy.
2. New diagnostic: the code of a root is much larger than its graph
The new DIW016 warning is reported when the code of a root has at least 2,000 lines and more than 50 lines per dependency. It points at the cases that are still expensive by design, such as a transient dependency reachable from itself through Lazy<T>, or the arguments of nested delegates that are overridden for every object created under them.
- Give such a dependency a shared lifetime (
PerResolve,PerBlockorSingleton) when it does not need to be a new instance each time, or setHint.LocalFunctionstoOn. - When the argument of a delegate is needed only by the object the delegate creates, bind the delegate yourself with
ctx.Letinstead ofctx.Override. - See
DIW016for details.
3. Scopes and singletons
- The singletons of a composition are now stored in one object that the root composition shares with its scopes, so creating a scope no longer allocates and clears a field for every singleton of the composition.
- A scope creates its array of disposables only when it creates its first scoped disposable instance, and disposing a scope no longer allocates a new array.
- With Microsoft DI, every root except a singleton is now resolved from the composition of the current scope. A transient, per-resolve or per-block root with a scoped dependency used to take that dependency from the parent composition, so it lived for the whole application. Closes #43.
- A shared instance is checked once more under
ctx.Lockwhen thread safety is disabled. Fixes #174.
4. Finding implementations
RootsandBuilderstake a new optionalsearchInCompilationparameter. When it istrue, the implementations are looked up in every type of the current assembly, so a setup works without importing the namespaces of the implementations. Fixes #153.- When a wildcard filter matches nothing,
DIE016now names up to five implementations that exist in the assembly but are not visible from the setup, and suggests ausingdirective or the new parameter. - An interface re-implemented on top of a base class is kept as a contract of a simplified binding. Fixes #173.
- The consumer type scenario now explains that the
TTmarker substitutes the consumer type at generation time, so a call such asCreateLogger<TT>()needs no runtime value. Closes #77.
5. Fixes in factories, overrides and generated code
- A singleton that reads a member of another singleton no longer gets
nullbecause the other singleton was not created yet. This was a regression in 2.5.7. Fixes #202. - A singleton that reads a delegate argument on a cycle through lambdas compiles again. This was a regression in an earlier fix of this release. Fixes #193.
- A nested delegate creates its result instead of returning the argument of an outer delegate. Fixes #185.
- A
Func<T1, T>injected by a factory that overridesT1gets its own argument, and the override of a factory replaces the inherited one. Fixes #183. - The argument of
ctx.Letreaches the instance behind a decorator; this is now covered by tests. Closes #191. - When locals of a factory are renamed:
- type names in an override value are no longer renamed (CS0103), fixes #179;
- two factories that override the same type under one root both declare their value (CS0103), fixes #180;
- named arguments and members keep their names (CS1739), fixes #197 and #198;
- a local no longer takes the name of a lambda parameter that
ctx.Letpasses (CS0136, CS0841), fixes #199; - variables and roots get unique names when the name of a type ends with a number.
- A
returnin a local function or an anonymous method of a factory is no longer rewritten as a return of the factory. Fixes #200. - A type nested in a generic type keeps the type arguments of its containing type when a generic binding is closed with it. Fixes #196.
- Variables restored by nested custom accumulators are declared when leaving nested scopes. Fixes #190.
- Binding ids no longer overflow when a bound type has exported members, which caused a bogus cyclic dependency and unresolved dependencies. Unused exported roots and members no longer trigger
DIW003. Fixes #194. - Optional parameters:
6. Platform
- The repository builds with the .NET 11 SDK and the Roslyn 5.12 compiler toolset, and union types work on .NET 11.
- NuGet packages are now published from a version tag by a GitHub workflow that validates the package identities and smoke-tests the templates. CI builds and tests each supported Roslyn version in parallel.
Closed issues
Most issues of this release were reported by EmilienDup, and scott-xu reported three. Each came with a minimal reproduction and a clear description of the observed and expected behavior; thank you.
- #43 - integrate the Scoped lifetime with the Microsoft DI scoped lifetime
- #77 -
TConsumeras a marker of the consumer type in factories - #153 - cannot find the derived classes in different namespaces
- #172 - override rewrite visits the graph once per dependency path under a
Func<T1, T> - #173 - DIE000: unable to resolve
Quartz.ISchedulerFactory - #174 - missing null check inside a manual lock
- #175 - the graph under nested
Func<T1, T>delegates is duplicated for every nesting of unused arguments - #176 - regression after #172: generation time for nested delegates grows 4× per level
- #177 - a back-reference under
Func<T1, T>makes the rewrite visit the graph once per path - #178 - the graph under nested delegates is duplicated when inner delegates pass the same argument
- #179 - a type name inside an override value is renamed like a local variable (CS0103)
- #180 - two factories that override the same type under one root: the second variable is not declared (CS0103)
- #182 - optional parameters reported as unresolved when another dependency is missing
- #183 - a
Func<T1, T>injected by a factory that overridesT1does not get its own argument - #184 - every delegate lambda contains the whole graph of the object it creates
- #185 - a nested delegate returns the argument of an outer delegate
- #186 - a singleton reached inside delegates with arguments is copied for every delegate
- #187 - generated code grows exponentially through arrays and spans
- #188 - transient diamonds sharing a
PerBlockdependency produce exponential code - #189 - duplicate iterator local functions for
IAsyncEnumerabledependencies - #190 - nested custom accumulator boundaries reference undeclared variables
- #191 -
ctx.Letdoes not reach the instance behind a decorator - #192 - screens that open each other through delegates get one construction per navigation path
- #193 - a singleton that reads a delegate argument on a cycle through lambdas: CS0103
- #194 - binding ids overflow when a bound type has exported members
- #195 - a navigation cycle is created per path when the screens use a factory instance that a singleton also uses
- #196 - a type nested in a generic type loses the type arguments of its containing type
- #197 - a named argument in a factory of a setup in another file is renamed (CS1739)
- #198 - duplicate of #197
- #199 - a factory local gets the name of a lambda parameter that
ctx.Letpasses (CS0136, CS0841) - #200 - a
returnin a local function of a factory is rewritten as a return of the factory - #201 - screens on a navigation cycle are created per path when a
PerBlockinstance is under their factories - #202 - a singleton reads a member of another singleton that is not created yet (regression from 2.5.7)
- #203 - the value of an optional parameter depends on whether another class requires the same unbound type
Full changelog: 2.5.7...2.5.8