Repository navigation
1. Faster binding of setups and faster builds
Every call that bound a setup without explicit type arguments, such as .Transient(() => ...) or .To(ctx => ...), made the C# compiler run type inference over all 378 overloads for 2 to 64 implementation types. Those overloads are now extension methods, so they are only looked up when no regular method applies.
- The same code keeps working unchanged; only the binding speed changes.
- Binding of a setup became several times faster, both in the generator and in the compilation of your project.
- Type attribute bindings are looked up without walking member bodies, which shortens generation further.
2. Smaller generated code for shared dependencies
A graph whose instances share dependencies (a diamond) used to be written out once per dependency path, so the generated code could grow exponentially with the depth of the graph.
- A transient instance used in several places is now created by a local function, so the code is written once; the function is marked for aggressive inlining, so the JIT can still keep those instances on the stack. Fixes #167: generated code grows exponentially with the depth of shared transient dependencies.
- A shared instance is now created in one place even when its own declaration is small but the dependencies written together with it are not, which covers diamonds of per-resolve instances and of singletons without a lock.
- Shared instances are also created once under accumulators such as
Owned<T>of a disposable, including when the accumulator is declared in an enclosing scope, and inside the lambdas of consumer delegates that override values. Fixes #171: generation time and output explode when a graph with shared dependencies is reached through aFunc<T1, T>delegate. - The new
LocalFunctionshint (Auto,On,Off) controls when the code of an instance is moved to a local function:Auto(default) does it when repeating the code would be large,Onwhenever possible, andOffkeeps the code in place except where a local function is required for correctness.
3. Collection bindings and tags
- A composite that injects a collection of its own contract no longer receives itself in that collection, so a lazy
IEnumerable<T>no longer recurses at run time; other consumers keep the shared collection. Fixes #170: anIEnumerable<IHandler<int>>injected into the defaultIHandler<int>binding contained the consumer itself. - A collection element is resolved by the first tag that still belongs to its binding, so a binding with several tags is no longer dropped when another binding overrides only one of them.
- Unnamed tuple literals are accepted for a contract with named tuple elements: a factory returning
("Main St", "Springfield")now satisfies a(string Street, string City)contract, as the C# language itself classifies both tuple types as identical. Fixes #166: a factory returning a tuple literal without element names was rejected for a named tuple contract (DIE011).
4. Setups, generated code and default values
- A setup now overrides the setups it depends on, so a chain
Composition → Mid → BaseappliesBasefirst. A composition built on aDependsOnchain finally behaves the same as the same setups listed directly. Fixes #169: binding precedence is reversed through a chain ofDependsOnsetups. - Using aliases are deduplicated in the generated composition, so the same alias declared in two partial setup files no longer breaks the build. Fixes #165: the same
usingalias in two setup files was copied twice into the generated composition (CS1537). - An alias target is rendered without a special type keyword, so the generated code compiles with older compilers as well.
- Boolean default values of optional parameters are rendered as
true/falseliterals, so a constructor with a boolean default value compiles again; boolean tags are fixed the same way. Fixes #164: the default value of an optionalboolconstructor parameter was emitted asTrue(CS0103).
5. New diagnostic: a stored lifetime built from a delegate argument
An argument of a generated Func<..., T> delegate rewrites the whole dependency subgraph of T. A singleton or scoped dependency inside that subgraph is created once, so it silently keeps the value of the first call, and when the same dependency is also reachable outside the delegate its value is ignored entirely. Fixes #168: a singleton reached through a Func<T1, T> delegate is built with the delegate argument of the first call.
- The new
DIW015diagnostic reports this case without changing the generated code, so explicitctx.Overridecalls stay silent. - Fix it by using transient or per-resolve dependencies for values that vary per call, by passing the value to the code that consumes it, or by resolving it from a root argument when it is constant for the whole root.
Closed issues
All eight issues were reported by EmilienDup with a minimal reproduction and a precise description of the observed and the expected behavior; thank you for the detailed analysis.
- #164 - default value of an optional
boolconstructor parameter is emitted asTrue(CS0103) - #165 - the same
usingalias in two setup files is copied twice into the generated composition (CS1537) - #166 - a factory returning a tuple literal without element names is rejected for a named tuple contract (DIE011)
- #167 - generated code grows exponentially with the depth of shared transient dependencies
- #168 - a singleton reached through a
Func<T1, T>delegate is built with the delegate argument of the first call - #169 - binding precedence is reversed through a chain of
DependsOnsetups - #170 -
IEnumerable<IHandler<int>>injected into the defaultIHandler<int>binding contains the consumer itself - #171 - generation time and output explode when a graph with shared dependencies is reached through a
Func<T1, T>delegate
Full Changelog: 2.5.6...2.5.7