From 8498248f4adee7d789a3032cdfbbf769c007c7d0 Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov <nikolayp@mail.ru> Date: Thu, 12 Dec 2024 12:24:42 +0300 Subject: [PATCH] Support of a simple factory with lambdas without explicit type definition --- readme/ArrayDetails.md | 2 +- readme/EnumDetails.md | 2 +- readme/FuncDetails.md | 2 +- readme/SingletonDetails.md | 2 +- readme/TransientDetails.md | 2 +- readme/a-few-partial-classes.md | 2 +- readme/accumulators.md | 2 +- readme/advanced-interception.md | 26 +++--- readme/array.md | 2 +- readme/async-disposable-scope.md | 2 +- readme/async-disposable-singleton.md | 2 +- readme/async-enumerable.md | 2 +- readme/auto-bindings.md | 2 +- readme/auto-scoped.md | 11 +-- readme/bind-attribute-for-a-generic-type.md | 2 +- .../bind-attribute-with-lifetime-and-tag.md | 2 +- readme/bind-attribute.md | 2 +- readme/check-for-a-root.md | 2 +- readme/composition-root-kinds.md | 2 +- readme/composition-roots.md | 2 +- readme/custom-generic-argument-attribute.md | 2 +- readme/custom-generic-argument.md | 2 +- readme/decorator.md | 2 +- .../default-lifetime-for-a-type-and-a-tag.md | 2 +- readme/default-lifetime-for-a-type.md | 2 +- readme/default-lifetime.md | 2 +- readme/default-values.md | 2 +- readme/dependent-compositions.md | 4 +- readme/disposable-singleton.md | 2 +- readme/enumerable-generics.md | 2 +- readme/enumerable.md | 2 +- readme/exposed-generic-roots.md | 2 +- readme/exposed-roots-with-tags.md | 2 +- readme/exposed-roots.md | 2 +- readme/field-injection.md | 2 +- readme/func-with-arguments.md | 2 +- readme/func-with-tag.md | 2 +- readme/func.md | 12 +-- ...eric-composition-roots-with-constraints.md | 2 +- readme/generic-composition-roots.md | 2 +- readme/generics.md | 2 +- readme/injections-of-abstractions.md | 2 +- readme/interception.md | 2 +- readme/keyed-service-provider.md | 2 +- readme/lazy.md | 2 +- readme/method-injection.md | 2 +- readme/oncannotresolve-hint.md | 6 +- readme/ondependencyinjection-hint.md | 42 ++++++---- readme/onnewinstance-hint.md | 9 ++- readme/overriding-the-bcl-binding.md | 19 ++--- readme/partial-class.md | 2 +- readme/perblock.md | 2 +- readme/perresolve.md | 2 +- readme/property-injection.md | 2 +- readme/resolve-hint.md | 2 +- readme/resolve-methods.md | 2 +- readme/root-binding.md | 6 +- readme/scope.md | 2 +- readme/service-collection.md | 2 +- readme/service-provider-with-scope.md | 2 +- readme/service-provider.md | 2 +- readme/simplified-binding.md | 6 +- readme/simplified-factory.md | 2 +- readme/singleton.md | 2 +- readme/span-and-readonlyspan.md | 2 +- readme/tag-attribute.md | 2 +- readme/tag-on-a-constructor-argument.md | 6 +- readme/tag-on-a-member.md | 4 +- readme/tag-on-a-method-argument.md | 4 +- .../tag-on-injection-site-with-wildcards.md | 6 +- readme/tag-on-injection-site.md | 4 +- readme/tag-type.md | 2 +- readme/tag-unique.md | 2 +- readme/tags.md | 2 +- readme/threadsafe-hint.md | 2 +- readme/tostring-hint.md | 2 +- ...async-disposable-instances-in-delegates.md | 2 +- ...osable-instances-per-a-composition-root.md | 2 +- ...cking-disposable-instances-in-delegates.md | 2 +- ...osable-instances-per-a-composition-root.md | 2 +- ...sable-instances-using-pre-built-classes.md | 2 +- ...able-instances-with-different-lifetimes.md | 2 +- readme/transient.md | 2 +- readme/tuple.md | 2 +- readme/type-attribute.md | 2 +- readme/valuetask.md | 2 +- readme/weak-reference.md | 2 +- .../Core/ApiInvocationProcessor.cs | 69 ++++++++++------ .../Core/Code/DefaultConstructorBuilder.cs | 2 +- .../Core/Code/FactoryCodeBuilder.cs | 6 +- src/Pure.DI.Core/Core/Models/MdFactory.cs | 1 + src/Pure.DI.Core/Core/SetupsBuilder.cs | 1 + .../SimpleFactoryTests.cs | 79 ++++++++++++++++++- .../Advanced/TagOnConstructorArgScenario.cs | 4 +- .../Advanced/TagOnInjectionSiteScenario.cs | 4 +- ...TagOnInjectionSiteWithWildcardsScenario.cs | 4 +- .../Advanced/TagOnMemberScenario.cs | 2 +- .../Advanced/TagOnMethodArgScenario.cs | 2 +- .../BaseClassLibrary/FuncScenario.cs | 10 +-- .../OverridingBclBindingScenario.cs | 9 +-- .../Basics/ResolveMethodsScenario.cs | 2 + .../Basics/RootBindScenario.cs | 4 +- .../Basics/SimplifiedBindingScenario.cs | 4 +- .../Hints/OnCannotResolveHintScenario.cs | 4 +- .../OnDependencyInjectionHintScenario.cs | 16 ++-- .../Hints/OnNewInstanceHintScenario.cs | 7 +- .../AdvancedInterceptionScenario.cs | 27 +++---- .../Lifetimes/AutoScopedScenario.cs | 7 +- 108 files changed, 339 insertions(+), 234 deletions(-) diff --git a/readme/ArrayDetails.md b/readme/ArrayDetails.md index 142888d1f..d52074a13 100644 --- a/readme/ArrayDetails.md +++ b/readme/ArrayDetails.md @@ -93,7 +93,7 @@ partial class Array { private readonly Array _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Array() { _root = this; diff --git a/readme/EnumDetails.md b/readme/EnumDetails.md index 356e87f9d..f2c253c14 100644 --- a/readme/EnumDetails.md +++ b/readme/EnumDetails.md @@ -95,7 +95,7 @@ partial class Enum { private readonly Enum _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Enum() { _root = this; diff --git a/readme/FuncDetails.md b/readme/FuncDetails.md index 081c10e07..b4d744962 100644 --- a/readme/FuncDetails.md +++ b/readme/FuncDetails.md @@ -77,7 +77,7 @@ partial class Func { private readonly Func _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Func() { _root = this; diff --git a/readme/SingletonDetails.md b/readme/SingletonDetails.md index 3d1bc01a5..2b2b96720 100644 --- a/readme/SingletonDetails.md +++ b/readme/SingletonDetails.md @@ -74,7 +74,7 @@ partial class Singleton private Service1? _scopedService141; private Service4? _scopedService444; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Singleton() { _root = this; diff --git a/readme/TransientDetails.md b/readme/TransientDetails.md index 310f74873..3d5f24f67 100644 --- a/readme/TransientDetails.md +++ b/readme/TransientDetails.md @@ -71,7 +71,7 @@ partial class Transient { private readonly Transient _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Transient() { _root = this; diff --git a/readme/a-few-partial-classes.md b/readme/a-few-partial-classes.md index 07fb494ad..a88199f9c 100644 --- a/readme/a-few-partial-classes.md +++ b/readme/a-few-partial-classes.md @@ -49,7 +49,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/accumulators.md b/readme/accumulators.md index 9125f3d20..7cae7ee9a 100644 --- a/readme/accumulators.md +++ b/readme/accumulators.md @@ -51,7 +51,7 @@ partial class Composition private XyzDependency? _singletonXyzDependency45; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/advanced-interception.md b/readme/advanced-interception.md index bf8140d9e..c53300238 100644 --- a/readme/advanced-interception.md +++ b/readme/advanced-interception.md @@ -8,30 +8,26 @@ This approach of interception maximizes performance by precompiling the proxy ob ```c# public interface IDependency { - void DependencyCall(); + int DependencyRun(); } class Dependency : IDependency { - public void DependencyCall() - { - } + public int DependencyRun() => 33; } public interface IService { IDependency Dependency { get; } - void ServiceCall(); + string ServiceRun(); } class Service(IDependency dependency) : IService { public IDependency Dependency { get; } = dependency; - public void ServiceCall() - { - } + public string ServiceRun() => "Abc"; } internal partial class Composition : IInterceptor @@ -64,8 +60,8 @@ internal partial class Composition : IInterceptor public void Intercept(IInvocation invocation) { - _log.Add(invocation.Method.Name); invocation.Proceed(); + _log.Add($"{invocation.Method.Name} returns {invocation.ReturnValue}"); } private static class ProxyFactory<T> @@ -105,14 +101,14 @@ DI.Setup(nameof(Composition)) var log = new List<string>(); var composition = new Composition(log); var service = composition.Root; -service.ServiceCall(); -service.Dependency.DependencyCall(); +service.ServiceRun(); +service.Dependency.DependencyRun(); log.ShouldBe( ImmutableArray.Create( - "ServiceCall", - "get_Dependency", - "DependencyCall")); + "ServiceRun returns Abc", + "get_Dependency returns Castle.Proxies.IDependencyProxy", + "DependencyRun returns 33")); ``` The following partial class will be generated: @@ -122,7 +118,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/array.md b/readme/array.md index 2cff7ac82..3be9124e0 100644 --- a/readme/array.md +++ b/readme/array.md @@ -70,7 +70,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/async-disposable-scope.md b/readme/async-disposable-scope.md index ef7c4abda..c18b20c2e 100644 --- a/readme/async-disposable-scope.md +++ b/readme/async-disposable-scope.md @@ -95,7 +95,7 @@ partial class Composition: IDisposable, IAsyncDisposable private Dependency? _scopedDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/async-disposable-singleton.md b/readme/async-disposable-singleton.md index afb805c45..5ca012b62 100644 --- a/readme/async-disposable-singleton.md +++ b/readme/async-disposable-singleton.md @@ -61,7 +61,7 @@ partial class Composition: IDisposable, IAsyncDisposable private Dependency? _singletonDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/async-enumerable.md b/readme/async-enumerable.md index b5a313fd0..a6ffe4189 100644 --- a/readme/async-enumerable.md +++ b/readme/async-enumerable.md @@ -53,7 +53,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/auto-bindings.md b/readme/auto-bindings.md index a33f71f63..ad084e294 100644 --- a/readme/auto-bindings.md +++ b/readme/auto-bindings.md @@ -33,7 +33,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/auto-scoped.md b/readme/auto-scoped.md index bebafa75d..a24c9f07b 100644 --- a/readme/auto-scoped.md +++ b/readme/auto-scoped.md @@ -36,14 +36,11 @@ partial class Composition // Session composition root .Root<Service>("SessionRoot", kind: RootKinds.Private) // Auto scoped - .Bind().To<IService>(ctx => + .Bind().To(IService (Composition baseComposition) => { - // Injects a base composition - ctx.Inject(out Composition baseComposition); - // Creates a session var session = new Composition(baseComposition); - + // Provides a root return session.SessionRoot; }) @@ -77,7 +74,7 @@ partial class Composition private Dependency? _scopedDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; @@ -99,10 +96,10 @@ partial class Composition { Composition transientComposition3 = this; IService transientIService2; - // Injects a base composition Composition localBaseComposition81 = transientComposition3; // Creates a session var localSession82= new Composition(localBaseComposition81); + // Provides a root transientIService2 = localSession82.SessionRoot; IService localValue80 = transientIService2; return localValue80; diff --git a/readme/bind-attribute-for-a-generic-type.md b/readme/bind-attribute-for-a-generic-type.md index 9f92b121f..fc8b78b41 100644 --- a/readme/bind-attribute-for-a-generic-type.md +++ b/readme/bind-attribute-for-a-generic-type.md @@ -54,7 +54,7 @@ partial class Composition private Facade? _singletonFacade43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/bind-attribute-with-lifetime-and-tag.md b/readme/bind-attribute-with-lifetime-and-tag.md index fc177f3db..101fb13ab 100644 --- a/readme/bind-attribute-with-lifetime-and-tag.md +++ b/readme/bind-attribute-with-lifetime-and-tag.md @@ -55,7 +55,7 @@ partial class Composition private IDependency? _singletonIDependency0; private Facade? _singletonFacade43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/bind-attribute.md b/readme/bind-attribute.md index 55359d9aa..b9c7cb113 100644 --- a/readme/bind-attribute.md +++ b/readme/bind-attribute.md @@ -58,7 +58,7 @@ partial class Composition private Facade? _singletonFacade43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/check-for-a-root.md b/readme/check-for-a-root.md index 12e178af5..dc66a3009 100644 --- a/readme/check-for-a-root.md +++ b/readme/check-for-a-root.md @@ -67,7 +67,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/composition-root-kinds.md b/readme/composition-root-kinds.md index c333b3c4a..ddf53ce72 100644 --- a/readme/composition-root-kinds.md +++ b/readme/composition-root-kinds.md @@ -54,7 +54,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/composition-roots.md b/readme/composition-roots.md index 745851730..a1cfc8661 100644 --- a/readme/composition-roots.md +++ b/readme/composition-roots.md @@ -80,7 +80,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/custom-generic-argument-attribute.md b/readme/custom-generic-argument-attribute.md index 2b2c6c9eb..48739c103 100644 --- a/readme/custom-generic-argument-attribute.md +++ b/readme/custom-generic-argument-attribute.md @@ -53,7 +53,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/custom-generic-argument.md b/readme/custom-generic-argument.md index adc0e749d..2f8f7738e 100644 --- a/readme/custom-generic-argument.md +++ b/readme/custom-generic-argument.md @@ -49,7 +49,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/decorator.md b/readme/decorator.md index b3b777c1d..3e0f56941 100644 --- a/readme/decorator.md +++ b/readme/decorator.md @@ -42,7 +42,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/default-lifetime-for-a-type-and-a-tag.md b/readme/default-lifetime-for-a-type-and-a-tag.md index 01e02a523..f27bbd722 100644 --- a/readme/default-lifetime-for-a-type-and-a-tag.md +++ b/readme/default-lifetime-for-a-type-and-a-tag.md @@ -56,7 +56,7 @@ partial class Composition private Dependency? _singletonDependency43; private Dependency? _singletonDependency44; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/default-lifetime-for-a-type.md b/readme/default-lifetime-for-a-type.md index e0d0a95e1..d8812a40a 100644 --- a/readme/default-lifetime-for-a-type.md +++ b/readme/default-lifetime-for-a-type.md @@ -54,7 +54,7 @@ partial class Composition private Dependency? _singletonDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/default-lifetime.md b/readme/default-lifetime.md index c09ac514b..09e712316 100644 --- a/readme/default-lifetime.md +++ b/readme/default-lifetime.md @@ -57,7 +57,7 @@ partial class Composition private Service? _singletonService44; private Dependency? _singletonDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/default-values.md b/readme/default-values.md index 8393cafb3..56193b828 100644 --- a/readme/default-values.md +++ b/readme/default-values.md @@ -44,7 +44,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/dependent-compositions.md b/readme/dependent-compositions.md index 6a1b7dda9..09dd4e53d 100644 --- a/readme/dependent-compositions.md +++ b/readme/dependent-compositions.md @@ -54,7 +54,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; @@ -196,7 +196,7 @@ partial class OtherComposition { private readonly OtherComposition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public OtherComposition() { _root = this; diff --git a/readme/disposable-singleton.md b/readme/disposable-singleton.md index ce9b9698e..7a0c7ddea 100644 --- a/readme/disposable-singleton.md +++ b/readme/disposable-singleton.md @@ -59,7 +59,7 @@ partial class Composition: IDisposable private Dependency? _singletonDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/enumerable-generics.md b/readme/enumerable-generics.md index 82304d2c2..57fa401a7 100644 --- a/readme/enumerable-generics.md +++ b/readme/enumerable-generics.md @@ -50,7 +50,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/enumerable.md b/readme/enumerable.md index de3949b74..8a4067dbc 100644 --- a/readme/enumerable.md +++ b/readme/enumerable.md @@ -45,7 +45,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/exposed-generic-roots.md b/readme/exposed-generic-roots.md index 717e9ac01..bf39379e7 100644 --- a/readme/exposed-generic-roots.md +++ b/readme/exposed-generic-roots.md @@ -46,7 +46,7 @@ partial class Composition private Integration.CompositionWithGenericRootsInOtherProject? _singletonCompositionWithGenericRootsInOtherProject43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/exposed-roots-with-tags.md b/readme/exposed-roots-with-tags.md index 1bfae74a1..06b078eaf 100644 --- a/readme/exposed-roots-with-tags.md +++ b/readme/exposed-roots-with-tags.md @@ -41,7 +41,7 @@ partial class Composition private Integration.CompositionWithTagsInOtherProject? _singletonCompositionWithTagsInOtherProject43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/exposed-roots.md b/readme/exposed-roots.md index 61da8bc44..aaf28d3e5 100644 --- a/readme/exposed-roots.md +++ b/readme/exposed-roots.md @@ -44,7 +44,7 @@ partial class Composition private Integration.CompositionInOtherProject? _singletonCompositionInOtherProject43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/field-injection.md b/readme/field-injection.md index 8065562f3..80f18fc7f 100644 --- a/readme/field-injection.md +++ b/readme/field-injection.md @@ -44,7 +44,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/func-with-arguments.md b/readme/func-with-arguments.md index af40613c1..bfe9586bb 100644 --- a/readme/func-with-arguments.md +++ b/readme/func-with-arguments.md @@ -92,7 +92,7 @@ partial class Composition private Clock? _singletonClock43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/func-with-tag.md b/readme/func-with-tag.md index cad2f6a1e..82780ad30 100644 --- a/readme/func-with-tag.md +++ b/readme/func-with-tag.md @@ -43,7 +43,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/func.md b/readme/func.md index 8acdaecf4..4dc7d9b58 100644 --- a/readme/func.md +++ b/readme/func.md @@ -17,11 +17,11 @@ interface IService class Service(Func<IDependency> dependencyFactory) : IService { - public ImmutableArray<IDependency> Dependencies { get; } = + public ImmutableArray<IDependency> Dependencies => [ - ..Enumerable - .Range(0, 10) - .Select(_ => dependencyFactory()) + dependencyFactory(), + dependencyFactory(), + dependencyFactory() ]; } @@ -34,7 +34,7 @@ DI.Setup(nameof(Composition)) var composition = new Composition(); var service = composition.Root; -service.Dependencies.Length.ShouldBe(10); +service.Dependencies.Length.ShouldBe(3); ``` Be careful, replication takes into account the lifetime of the object. @@ -46,7 +46,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/generic-composition-roots-with-constraints.md b/readme/generic-composition-roots-with-constraints.md index 53d643c3d..4a3328950 100644 --- a/readme/generic-composition-roots-with-constraints.md +++ b/readme/generic-composition-roots-with-constraints.md @@ -67,7 +67,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/generic-composition-roots.md b/readme/generic-composition-roots.md index b84020cee..5b2b0fe35 100644 --- a/readme/generic-composition-roots.md +++ b/readme/generic-composition-roots.md @@ -61,7 +61,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/generics.md b/readme/generics.md index c9140300f..2b819db75 100644 --- a/readme/generics.md +++ b/readme/generics.md @@ -96,7 +96,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/injections-of-abstractions.md b/readme/injections-of-abstractions.md index 05b633e82..a8299a6f2 100644 --- a/readme/injections-of-abstractions.md +++ b/readme/injections-of-abstractions.md @@ -56,7 +56,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/interception.md b/readme/interception.md index ccd71ca24..c60931145 100644 --- a/readme/interception.md +++ b/readme/interception.md @@ -81,7 +81,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/keyed-service-provider.md b/readme/keyed-service-provider.md index df80d111f..8115b78e3 100644 --- a/readme/keyed-service-provider.md +++ b/readme/keyed-service-provider.md @@ -55,7 +55,7 @@ partial class Composition private Dependency? _singletonDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/lazy.md b/readme/lazy.md index 5b04e7b81..9b40121c9 100644 --- a/readme/lazy.md +++ b/readme/lazy.md @@ -37,7 +37,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/method-injection.md b/readme/method-injection.md index 28976601a..f09531cf0 100644 --- a/readme/method-injection.md +++ b/readme/method-injection.md @@ -46,7 +46,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/oncannotresolve-hint.md b/readme/oncannotresolve-hint.md index 88b276ea9..747481563 100644 --- a/readme/oncannotresolve-hint.md +++ b/readme/oncannotresolve-hint.md @@ -35,7 +35,7 @@ partial class Composition { if (typeof(T) == typeof(string)) { - return (T)(object)"Dependency with name"; + return (T)(object)"My name"; } throw new InvalidOperationException("Cannot resolve."); @@ -51,7 +51,7 @@ DI.Setup(nameof(Composition)) var composition = new Composition(); var service = composition.Root; -service.Dependency.ToString().ShouldBe("Dependency with name"); +service.Dependency.ToString().ShouldBe("My name"); ``` @@ -65,7 +65,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/ondependencyinjection-hint.md b/readme/ondependencyinjection-hint.md index 70d157f2b..ec5b16e95 100644 --- a/readme/ondependencyinjection-hint.md +++ b/readme/ondependencyinjection-hint.md @@ -11,7 +11,7 @@ using static Hint; interface IDependency; -class Dependency : IDependency; +record Dependency(int Id) : IDependency; interface IService { @@ -42,16 +42,20 @@ partial class Composition // OnDependencyInjection = On DI.Setup(nameof(Composition)) - .Hint(OnDependencyInjectionContractTypeNameRegularExpression, nameof(IDependency)) + .Hint(OnDependencyInjectionContractTypeNameRegularExpression, "(.*IDependency|int)$") + .RootArg<int>("id") .Bind().To<Dependency>() .Bind().To<Service>() - .Root<IService>("Root"); + .Root<IService>("GetRoot"); var log = new List<string>(); var composition = new Composition(log); -var service = composition.Root; +var service = composition.GetRoot(33); -log.ShouldBe(ImmutableArray.Create("Dependency injected")); +log.ShouldBe([ + "Int32 injected", + "Dependency injected" +]); ``` The `OnDependencyInjectionContractTypeNameRegularExpression` hint helps identify the set of types that require injection control. You can use it to specify a regular expression to filter the full name of a type. @@ -64,7 +68,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(10)] public Composition() { _root = this; @@ -75,13 +79,10 @@ partial class Composition _root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root; } - public IService Root + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public IService GetRoot(int id) { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - return new Service(OnDependencyInjection<IDependency>(new Dependency(), null, Lifetime.Transient)); - } + return new Service(OnDependencyInjection<IDependency>(new Dependency(OnDependencyInjection<int>(id, null, Lifetime.Transient)), null, Lifetime.Transient)); } @@ -100,15 +101,18 @@ Class diagram: classDiagram Service --|> IService Dependency --|> IDependency - Composition ..> Service : IService Root + Dependency --|> IEquatableᐸDependencyᐳ + Composition ..> Service : IService GetRoot(int id) Service *-- Dependency : IDependency + Dependency o-- Int32 : Argument "id" namespace Pure.DI.UsageTests.Hints.OnDependencyInjectionHintScenario { class Composition { <<partial>> - +IService Root + +IService GetRoot(int id) } class Dependency { - +Dependency() + <<record>> + +Dependency(Int32 Id) } class IDependency { <<interface>> @@ -120,5 +124,13 @@ classDiagram +Service(IDependency dependency) } } + namespace System { + class IEquatableᐸDependencyᐳ { + <<interface>> + } + class Int32 { + <<struct>> + } + } ``` diff --git a/readme/onnewinstance-hint.md b/readme/onnewinstance-hint.md index a6bcccca9..27aecbeb6 100644 --- a/readme/onnewinstance-hint.md +++ b/readme/onnewinstance-hint.md @@ -39,7 +39,7 @@ internal partial class Composition ref T value, object? tag, Lifetime lifetime) => - _log.Add(typeof(T).Name); + _log.Add($"{typeof(T).Name} created"); } DI.Setup(nameof(Composition)) @@ -53,7 +53,10 @@ var composition = new Composition(log); var service1 = composition.Root; var service2 = composition.Root; -log.ShouldBe([nameof(Dependency), nameof(Service), nameof(Service)]); +log.ShouldBe([ + "Dependency created", + "Service created", + "Service created"]); ``` The `OnNewInstanceLifetimeRegularExpression` hint helps you define a set of lifetimes that require instance creation control. You can use it to specify a regular expression to filter bindings by lifetime name. @@ -69,7 +72,7 @@ partial class Composition private Dependency? _singletonDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/overriding-the-bcl-binding.md b/readme/overriding-the-bcl-binding.md index 248d291e6..c2736ca83 100644 --- a/readme/overriding-the-bcl-binding.md +++ b/readme/overriding-the-bcl-binding.md @@ -23,12 +23,9 @@ class Service(IDependency[] dependencies) : IService } DI.Setup(nameof(Composition)) - .Bind<IDependency[]>().To(_ => new IDependency[] - { - new AbcDependency(), - new XyzDependency(), - new AbcDependency() - }) + .Bind<IDependency[]>().To<IDependency[]>(_ => + [new AbcDependency(), new XyzDependency(), new AbcDependency()] + ) .Bind<IService>().To<Service>() // Composition root @@ -49,7 +46,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; @@ -65,12 +62,8 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - IDependency[] transient1 = new IDependency[] - { - new AbcDependency(), - new XyzDependency(), - new AbcDependency() - }; + IDependency[] transient1 = [new AbcDependency(), new XyzDependency(), new AbcDependency()] + ; return new Service(transient1); } } diff --git a/readme/partial-class.md b/readme/partial-class.md index acd172edd..5e0c9ae6c 100644 --- a/readme/partial-class.md +++ b/readme/partial-class.md @@ -73,7 +73,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/perblock.md b/readme/perblock.md index edfc83ee2..8a5e4589e 100644 --- a/readme/perblock.md +++ b/readme/perblock.md @@ -55,7 +55,7 @@ partial class Composition private (IDependency dep3, IDependency dep4) _singletonValueTuple44; private bool _singletonValueTuple44Created; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/perresolve.md b/readme/perresolve.md index 8a59a3ec3..d0714622e 100644 --- a/readme/perresolve.md +++ b/readme/perresolve.md @@ -55,7 +55,7 @@ partial class Composition private (IDependency dep3, IDependency dep4) _singletonValueTuple44; private bool _singletonValueTuple44Created; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/property-injection.md b/readme/property-injection.md index 793b9e03f..a918d1525 100644 --- a/readme/property-injection.md +++ b/readme/property-injection.md @@ -43,7 +43,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/resolve-hint.md b/readme/resolve-hint.md index a14fcbbe4..666a00d60 100644 --- a/readme/resolve-hint.md +++ b/readme/resolve-hint.md @@ -38,7 +38,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/resolve-methods.md b/readme/resolve-methods.md index f5f0b4fe5..b9493bb70 100644 --- a/readme/resolve-methods.md +++ b/readme/resolve-methods.md @@ -58,7 +58,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/root-binding.md b/readme/root-binding.md index 2e54a64b4..969aa5960 100644 --- a/readme/root-binding.md +++ b/readme/root-binding.md @@ -18,8 +18,8 @@ DI.Setup(nameof(Composition)) .Bind().As(Lifetime.Singleton).To<Dependency>() .RootBind<IService>("MyRoot").To<Service>(); // It's the same as: -// .Bind<IService>().To<Service>() -// .Root<IService>("MyRoot") +// .Bind<IService>().To<Service>() +// .Root<IService>("MyRoot") var composition = new Composition(); composition.MyRoot.ShouldBeOfType<Service>(); @@ -35,7 +35,7 @@ partial class Composition private Dependency? _singletonDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/scope.md b/readme/scope.md index e4db63771..5c23055b7 100644 --- a/readme/scope.md +++ b/readme/scope.md @@ -93,7 +93,7 @@ partial class Composition: IDisposable private Dependency? _scopedDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/service-collection.md b/readme/service-collection.md index db0836697..0ab5377b4 100644 --- a/readme/service-collection.md +++ b/readme/service-collection.md @@ -52,7 +52,7 @@ partial class Composition private Dependency? _singletonDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/service-provider-with-scope.md b/readme/service-provider-with-scope.md index b034e4992..3be477c6a 100644 --- a/readme/service-provider-with-scope.md +++ b/readme/service-provider-with-scope.md @@ -84,7 +84,7 @@ partial class Composition: IDisposable private Service? _scopedService44; private Dependency? _singletonDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/service-provider.md b/readme/service-provider.md index 7193d96b1..dd2dbad04 100644 --- a/readme/service-provider.md +++ b/readme/service-provider.md @@ -50,7 +50,7 @@ partial class Composition private Dependency? _singletonDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/simplified-binding.md b/readme/simplified-binding.md index c6f363694..08817a5bd 100644 --- a/readme/simplified-binding.md +++ b/readme/simplified-binding.md @@ -44,8 +44,8 @@ DI.Setup(nameof(Composition)) // for all abstract but NOT special types that are directly implemented. // So that's the equivalent of the following: // .Bind<IDependency, IOtherDependency, Dependency>() - // .As(Lifetime.PerBlock) - // .To<Dependency>() + // .As(Lifetime.PerBlock) + // .To<Dependency>() .Bind().As(Lifetime.PerBlock).To<Dependency>() .Bind().To<Service>() @@ -100,7 +100,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/simplified-factory.md b/readme/simplified-factory.md index 7bba26c57..3663595f2 100644 --- a/readme/simplified-factory.md +++ b/readme/simplified-factory.md @@ -65,7 +65,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/singleton.md b/readme/singleton.md index 277fca258..498db4b72 100644 --- a/readme/singleton.md +++ b/readme/singleton.md @@ -65,7 +65,7 @@ partial class Composition private Dependency? _singletonDependency43; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/span-and-readonlyspan.md b/readme/span-and-readonlyspan.md index ff9afe90d..d452d53a3 100644 --- a/readme/span-and-readonlyspan.md +++ b/readme/span-and-readonlyspan.md @@ -51,7 +51,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tag-attribute.md b/readme/tag-attribute.md index 01d42d975..fb37512b1 100644 --- a/readme/tag-attribute.md +++ b/readme/tag-attribute.md @@ -54,7 +54,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tag-on-a-constructor-argument.md b/readme/tag-on-a-constructor-argument.md index 4eae138dc..e87136ee3 100644 --- a/readme/tag-on-a-constructor-argument.md +++ b/readme/tag-on-a-constructor-argument.md @@ -39,9 +39,9 @@ class Service( DI.Setup(nameof(Composition)) .Bind(Tag.OnConstructorArg<Service>("dependency1")) - .To<AbcDependency>() + .To<AbcDependency>() .Bind(Tag.OnConstructorArg<Consumer<TT>>("myDep")) - .To<XyzDependency>() + .To<XyzDependency>() .Bind<IService>().To<Service>() // Specifies to create the composition root named "Root" @@ -63,7 +63,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tag-on-a-member.md b/readme/tag-on-a-member.md index 28037db73..4d489f654 100644 --- a/readme/tag-on-a-member.md +++ b/readme/tag-on-a-member.md @@ -28,7 +28,7 @@ class Service : IService DI.Setup(nameof(Composition)) .Bind().To<AbcDependency>() .Bind(Tag.OnMember<Service>(nameof(Service.Dependency))) - .To<XyzDependency>() + .To<XyzDependency>() .Bind<IService>().To<Service>() // Specifies to create the composition root named "Root" @@ -49,7 +49,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tag-on-a-method-argument.md b/readme/tag-on-a-method-argument.md index 6ca114cd2..3caaa0e24 100644 --- a/readme/tag-on-a-method-argument.md +++ b/readme/tag-on-a-method-argument.md @@ -32,7 +32,7 @@ class Service : IService DI.Setup(nameof(Composition)) .Bind().To<AbcDependency>() .Bind(Tag.OnMethodArg<Service>(nameof(Service.Initialize), "dep")) - .To<XyzDependency>() + .To<XyzDependency>() .Bind<IService>().To<Service>() // Specifies to create the composition root named "Root" @@ -53,7 +53,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tag-on-injection-site-with-wildcards.md b/readme/tag-on-injection-site-with-wildcards.md index 7645c0e74..d8d19650f 100644 --- a/readme/tag-on-injection-site-with-wildcards.md +++ b/readme/tag-on-injection-site-with-wildcards.md @@ -48,9 +48,9 @@ class Service( DI.Setup(nameof(Composition)) .Bind(Tag.On("*Service:Dependency3", "*Consumer:myDep")) - .To<AbcDependency>() + .To<AbcDependency>() .Bind(Tag.On("*Service:dependency?")) - .To<XyzDependency>() + .To<XyzDependency>() .Bind<IService>().To<Service>() // Specifies to create the composition root named "Root" @@ -74,7 +74,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tag-on-injection-site.md b/readme/tag-on-injection-site.md index b1b9cbdab..d8f04ff79 100644 --- a/readme/tag-on-injection-site.md +++ b/readme/tag-on-injection-site.md @@ -59,13 +59,13 @@ DI.Setup(nameof(Composition)) Tag.On("MyNamespace.Service.Service:dependency1"), // Tag on injection site for generic type Tag.On("MyNamespace.Consumer`1.Consumer:myDep")) - .To<AbcDependency>() + .To<AbcDependency>() .Bind( // Combined tag Tag.On( "MyNamespace.Service.Service:dependency2", "MyNamespace.Service:Dependency3")) - .To<XyzDependency>() + .To<XyzDependency>() .Bind<IService>().To<Service>() // Specifies to create the composition root named "Root" diff --git a/readme/tag-type.md b/readme/tag-type.md index 426fca0c5..def2880f0 100644 --- a/readme/tag-type.md +++ b/readme/tag-type.md @@ -65,7 +65,7 @@ partial class Composition private XyzDependency? _singletonXyzDependency44; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tag-unique.md b/readme/tag-unique.md index c17b8cf81..50b0d5de5 100644 --- a/readme/tag-unique.md +++ b/readme/tag-unique.md @@ -43,7 +43,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tags.md b/readme/tags.md index 47279e899..f0aa2130c 100644 --- a/readme/tags.md +++ b/readme/tags.md @@ -69,7 +69,7 @@ partial class Composition private XyzDependency? _singletonXyzDependency44; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/threadsafe-hint.md b/readme/threadsafe-hint.md index 88b22df73..323b02774 100644 --- a/readme/threadsafe-hint.md +++ b/readme/threadsafe-hint.md @@ -38,7 +38,7 @@ partial class Composition private Service? _singletonService44; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tostring-hint.md b/readme/tostring-hint.md index 61fdc67db..ef4cbacf3 100644 --- a/readme/tostring-hint.md +++ b/readme/tostring-hint.md @@ -35,7 +35,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tracking-async-disposable-instances-in-delegates.md b/readme/tracking-async-disposable-instances-in-delegates.md index 1b9bd7165..b4eba345c 100644 --- a/readme/tracking-async-disposable-instances-in-delegates.md +++ b/readme/tracking-async-disposable-instances-in-delegates.md @@ -78,7 +78,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tracking-async-disposable-instances-per-a-composition-root.md b/readme/tracking-async-disposable-instances-per-a-composition-root.md index c5cc3c0d9..f62aa0882 100644 --- a/readme/tracking-async-disposable-instances-per-a-composition-root.md +++ b/readme/tracking-async-disposable-instances-per-a-composition-root.md @@ -71,7 +71,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tracking-disposable-instances-in-delegates.md b/readme/tracking-disposable-instances-in-delegates.md index dff877715..1b01b4a36 100644 --- a/readme/tracking-disposable-instances-in-delegates.md +++ b/readme/tracking-disposable-instances-in-delegates.md @@ -71,7 +71,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tracking-disposable-instances-per-a-composition-root.md b/readme/tracking-disposable-instances-per-a-composition-root.md index 6765813e3..08a385256 100644 --- a/readme/tracking-disposable-instances-per-a-composition-root.md +++ b/readme/tracking-disposable-instances-per-a-composition-root.md @@ -67,7 +67,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tracking-disposable-instances-using-pre-built-classes.md b/readme/tracking-disposable-instances-using-pre-built-classes.md index 6419b2733..e9a27b77b 100644 --- a/readme/tracking-disposable-instances-using-pre-built-classes.md +++ b/readme/tracking-disposable-instances-using-pre-built-classes.md @@ -109,7 +109,7 @@ partial class Composition: IDisposable private Dependency? _singletonDependency44; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tracking-disposable-instances-with-different-lifetimes.md b/readme/tracking-disposable-instances-with-different-lifetimes.md index 016f99c31..cd0b7dea2 100644 --- a/readme/tracking-disposable-instances-with-different-lifetimes.md +++ b/readme/tracking-disposable-instances-with-different-lifetimes.md @@ -99,7 +99,7 @@ partial class Composition: IDisposable private Dependency? _singletonDependency44; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/transient.md b/readme/transient.md index ba6c58af2..5e0965d85 100644 --- a/readme/transient.md +++ b/readme/transient.md @@ -59,7 +59,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/tuple.md b/readme/tuple.md index ae631ed21..cf19208ff 100644 --- a/readme/tuple.md +++ b/readme/tuple.md @@ -41,7 +41,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/type-attribute.md b/readme/type-attribute.md index dc147b314..d698f73e0 100644 --- a/readme/type-attribute.md +++ b/readme/type-attribute.md @@ -50,7 +50,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/valuetask.md b/readme/valuetask.md index cfcebd433..a1435c947 100644 --- a/readme/valuetask.md +++ b/readme/valuetask.md @@ -47,7 +47,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/readme/weak-reference.md b/readme/weak-reference.md index 0316c6da4..a6183d228 100644 --- a/readme/weak-reference.md +++ b/readme/weak-reference.md @@ -36,7 +36,7 @@ partial class Composition { private readonly Composition _root; - [OrdinalAttribute(20)] + [OrdinalAttribute(256)] public Composition() { _root = this; diff --git a/src/Pure.DI.Core/Core/ApiInvocationProcessor.cs b/src/Pure.DI.Core/Core/ApiInvocationProcessor.cs index d82d0c93c..e595709dc 100644 --- a/src/Pure.DI.Core/Core/ApiInvocationProcessor.cs +++ b/src/Pure.DI.Core/Core/ApiInvocationProcessor.cs @@ -66,16 +66,20 @@ MemberAccessExpressionSyntax memberAccess when memberAccess.Kind() == SyntaxKind if (type is INamedTypeSymbol symbol) { if (symbol.TypeArguments.Length > 1 - && invocation.ArgumentList.Arguments[0].Expression is ParenthesizedLambdaExpressionSyntax { ParameterList.Parameters.Count: > 0 } parenthesizedLambdaExpressionSyntaxWithTypes && symbol.TypeArguments[0].ToDisplayString(NullableFlowState.None, SymbolDisplayFormat.FullyQualifiedFormat) != Names.ContextInterfaceName) { - VisitSimpleFactory( - metadataVisitor, - semanticModel, - invocation, - symbol.TypeArguments.Last(), - parenthesizedLambdaExpressionSyntaxWithTypes.ParameterList.Parameters.Select(i => i.Type!).ToList(), - parenthesizedLambdaExpressionSyntaxWithTypes); + switch (invocation.ArgumentList.Arguments[0].Expression) + { + case ParenthesizedLambdaExpressionSyntax { ParameterList.Parameters.Count: > 0 } parenthesizedLambda: + VisitSimpleFactory( + metadataVisitor, + semanticModel, + invocation, + symbol.TypeArguments.Last(), + parenthesizedLambda.ParameterList.Parameters.Select(i => i.Type!).ToList(), + parenthesizedLambda); + break; + } break; } @@ -205,21 +209,29 @@ MemberAccessExpressionSyntax memberAccess when memberAccess.Kind() == SyntaxKind if (genericName.TypeArgumentList.Arguments.Count > 1 && invocation.ArgumentList.Arguments.Count == 1) { - if (invocation.ArgumentList.Arguments[0].Expression is not ParenthesizedLambdaExpressionSyntax parenthesizedLambdaExpressionSyntax - || parenthesizedLambdaExpressionSyntax.ParameterList.Parameters.Count == 0) + switch (invocation.ArgumentList.Arguments[0].Expression) { - NotSupported(invocation); - break; + case ParenthesizedLambdaExpressionSyntax { ParameterList.Parameters.Count: > 0 } parenthesizedLambda: + VisitSimpleFactory( + metadataVisitor, + semanticModel, + invocation, + semantic.GetTypeSymbol<ITypeSymbol>(semanticModel, genericName.TypeArgumentList.Arguments.Last()), + genericName.TypeArgumentList.Arguments.Reverse().Skip(1).Reverse().ToList(), + parenthesizedLambda); + break; + + case SimpleLambdaExpressionSyntax simpleLambda: + VisitSimpleFactory( + metadataVisitor, + semanticModel, + invocation, + semantic.GetTypeSymbol<ITypeSymbol>(semanticModel, genericName.TypeArgumentList.Arguments.Last()), + genericName.TypeArgumentList.Arguments.Reverse().Skip(1).Reverse().ToList(), + simpleLambda); + break; } - VisitSimpleFactory( - metadataVisitor, - semanticModel, - invocation, - semantic.GetTypeSymbol<ITypeSymbol>(semanticModel, genericName.TypeArgumentList.Arguments.Last()), - genericName.TypeArgumentList.Arguments.Reverse().Skip(1).Reverse().ToList(), - parenthesizedLambdaExpressionSyntax); - break; } @@ -387,10 +399,21 @@ private void VisitSimpleFactory( InvocationExpressionSyntax source, ITypeSymbol returnType, List<TypeSyntax> argsTypes, - ParenthesizedLambdaExpressionSyntax lambdaExpression) + LambdaExpressionSyntax lambdaExpression) { CheckNotAsync(lambdaExpression); - var parameters = lambdaExpression.ParameterList.Parameters; + var parameters = new List<ParameterSyntax>(); + switch (lambdaExpression) + { + case ParenthesizedLambdaExpressionSyntax parenthesizedLambda: + parameters.AddRange(parenthesizedLambda.ParameterList.Parameters); + break; + + case SimpleLambdaExpressionSyntax simpleLambda: + parameters.Add(simpleLambda.Parameter); + break; + } + var paramAttributes = parameters.Select(i => i.AttributeLists.SelectMany(j => j.Attributes).ToList()).ToList(); var resolvers = new List<MdResolver>(); var namespaces = new HashSet<string>(); @@ -424,6 +447,7 @@ private void VisitSimpleFactory( source, returnType, lambdaExpression, + true, SyntaxFactory.Parameter(SyntaxFactory.Identifier("ctx_1182D127")), resolvers.ToImmutableArray(), ImmutableArray<MdInitializer>.Empty, @@ -648,6 +672,7 @@ tag is MemberAccessExpressionSyntax memberAccessExpression lambdaExpression, resultType, lambdaExpression, + false, contextParameter, resolvers, initializers, diff --git a/src/Pure.DI.Core/Core/Code/DefaultConstructorBuilder.cs b/src/Pure.DI.Core/Core/Code/DefaultConstructorBuilder.cs index 4b08e4e09..4ec6c8d95 100644 --- a/src/Pure.DI.Core/Core/Code/DefaultConstructorBuilder.cs +++ b/src/Pure.DI.Core/Core/Code/DefaultConstructorBuilder.cs @@ -24,7 +24,7 @@ public CompositionCode Build(CompositionCode composition) code.AppendLine("/// </summary>"); } - code.AppendLine($"[{Names.OrdinalAttributeName}(20)]"); + code.AppendLine($"[{Names.OrdinalAttributeName}(256)]"); code.AppendLine($"public {composition.Source.Source.Name.ClassName}()"); code.AppendLine("{"); using (code.Indent()) diff --git a/src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs b/src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs index 40e1a00b0..b0d00ffa0 100644 --- a/src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs +++ b/src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs @@ -34,7 +34,7 @@ public void Build(BuildContext ctx, in DpFactory factory) var originalLambda = factory.Source.Factory; // Simple factory - if (originalLambda is ParenthesizedLambdaExpressionSyntax parenthesizedLambda) + if (factory.Source.IsSimpleFactory) { var block = new List<StatementSyntax>(); foreach (var resolver in factory.Source.Resolvers) @@ -132,13 +132,13 @@ public void Build(BuildContext ctx, in DpFactory factory) } else { - if (parenthesizedLambda.Block is { } lambdaBlock) + if (originalLambda.Block is { } lambdaBlock) { block.AddRange(lambdaBlock.Statements); } else { - if (parenthesizedLambda.ExpressionBody is { } body) + if (originalLambda.ExpressionBody is { } body) { block.Add(SyntaxFactory.ReturnStatement(body)); } diff --git a/src/Pure.DI.Core/Core/Models/MdFactory.cs b/src/Pure.DI.Core/Core/Models/MdFactory.cs index b25684a26..6f48ed408 100644 --- a/src/Pure.DI.Core/Core/Models/MdFactory.cs +++ b/src/Pure.DI.Core/Core/Models/MdFactory.cs @@ -7,6 +7,7 @@ internal readonly record struct MdFactory( SyntaxNode Source, ITypeSymbol Type, LambdaExpressionSyntax Factory, + bool IsSimpleFactory, ParameterSyntax Context, in ImmutableArray<MdResolver> Resolvers, in ImmutableArray<MdInitializer> Initializers, diff --git a/src/Pure.DI.Core/Core/SetupsBuilder.cs b/src/Pure.DI.Core/Core/SetupsBuilder.cs index 6127f27bf..51cc6f158 100644 --- a/src/Pure.DI.Core/Core/SetupsBuilder.cs +++ b/src/Pure.DI.Core/Core/SetupsBuilder.cs @@ -325,6 +325,7 @@ from attribute in member.GetAttributes() source, contractType, FactoryCodeBuilder.DefaultBindAttrParenthesizedLambda, + true, FactoryCodeBuilder.DefaultCtxParameter, resolvers.ToImmutableArray(), ImmutableArray<MdInitializer>.Empty, diff --git a/tests/Pure.DI.IntegrationTests/SimpleFactoryTests.cs b/tests/Pure.DI.IntegrationTests/SimpleFactoryTests.cs index 4f30ec217..0062458b4 100644 --- a/tests/Pure.DI.IntegrationTests/SimpleFactoryTests.cs +++ b/tests/Pure.DI.IntegrationTests/SimpleFactoryTests.cs @@ -153,7 +153,7 @@ public static void Main() } [Fact] - public async Task ShouldSupportSimpleFactoryWhenSimpleLambdaWitgGenericParams() + public async Task ShouldSupportSimpleFactoryWhenSimpleLambdaWithGenericParams() { // Given @@ -299,6 +299,83 @@ public static void Main() result.Success.ShouldBeTrue(result); result.StdOut.ShouldBe(["True"], result); } + + [Fact] + public async Task ShouldSupportSimpleFactoryWhenInjectionWithOutTypeInLambda() + { + // Given + + // When + var result = await """ + using System; + using Pure.DI; + + namespace Sample + { + interface IDependency + { + DateTimeOffset Time { get; } + + bool IsInitialized { get; } + } + + class Dependency : IDependency + { + public DateTimeOffset Time { get; private set; } + + public bool IsInitialized { get; private set; } + + public IDependency Initialize(DateTimeOffset time) + { + Time = time; + IsInitialized = true; + return this; + } + } + + interface IService + { + IDependency Dependency { get; } + } + + class Service : IService + { + public Service(IDependency dependency) + { + Dependency = dependency; + } + + public IDependency Dependency { get; } + } + + static class Setup + { + private static void SetupComposition() + { + DI.Setup("Composition") + .Bind().To(_ => DateTimeOffset.Now) + .Bind().To<Dependency, IDependency>(dependency => dependency.Initialize(DateTimeOffset.Now)) + .Bind().To<Service>() + .Root<IService>("MyService"); + } + } + + public class Program + { + public static void Main() + { + var composition = new Composition(); + var service = composition.MyService; + Console.WriteLine(service.Dependency.IsInitialized); + } + } + } + """.RunAsync(); + + // Then + result.Success.ShouldBeTrue(result); + result.StdOut.ShouldBe(["True"], result); + } [Fact] public async Task ShouldSupportSimpleFactoryWhen2Injections() diff --git a/tests/Pure.DI.UsageTests/Advanced/TagOnConstructorArgScenario.cs b/tests/Pure.DI.UsageTests/Advanced/TagOnConstructorArgScenario.cs index 4454ceab8..e608fe9c2 100644 --- a/tests/Pure.DI.UsageTests/Advanced/TagOnConstructorArgScenario.cs +++ b/tests/Pure.DI.UsageTests/Advanced/TagOnConstructorArgScenario.cs @@ -63,9 +63,9 @@ public void Run() // { DI.Setup(nameof(Composition)) .Bind(Tag.OnConstructorArg<Service>("dependency1")) - .To<AbcDependency>() + .To<AbcDependency>() .Bind(Tag.OnConstructorArg<Consumer<TT>>("myDep")) - .To<XyzDependency>() + .To<XyzDependency>() .Bind<IService>().To<Service>() // Specifies to create the composition root named "Root" diff --git a/tests/Pure.DI.UsageTests/Advanced/TagOnInjectionSiteScenario.cs b/tests/Pure.DI.UsageTests/Advanced/TagOnInjectionSiteScenario.cs index f33ec6194..621153a5a 100644 --- a/tests/Pure.DI.UsageTests/Advanced/TagOnInjectionSiteScenario.cs +++ b/tests/Pure.DI.UsageTests/Advanced/TagOnInjectionSiteScenario.cs @@ -83,13 +83,13 @@ public void Run() Tag.On("MyNamespace.Service.Service:dependency1"), // Tag on injection site for generic type Tag.On("MyNamespace.Consumer`1.Consumer:myDep")) - .To<AbcDependency>() + .To<AbcDependency>() .Bind( // Combined tag Tag.On( "MyNamespace.Service.Service:dependency2", "MyNamespace.Service:Dependency3")) - .To<XyzDependency>() + .To<XyzDependency>() .Bind<IService>().To<Service>() // Specifies to create the composition root named "Root" diff --git a/tests/Pure.DI.UsageTests/Advanced/TagOnInjectionSiteWithWildcardsScenario.cs b/tests/Pure.DI.UsageTests/Advanced/TagOnInjectionSiteWithWildcardsScenario.cs index a929b3c89..1fd8891a7 100644 --- a/tests/Pure.DI.UsageTests/Advanced/TagOnInjectionSiteWithWildcardsScenario.cs +++ b/tests/Pure.DI.UsageTests/Advanced/TagOnInjectionSiteWithWildcardsScenario.cs @@ -72,9 +72,9 @@ public void Run() // { DI.Setup(nameof(Composition)) .Bind(Tag.On("*Service:Dependency3", "*Consumer:myDep")) - .To<AbcDependency>() + .To<AbcDependency>() .Bind(Tag.On("*Service:dependency?")) - .To<XyzDependency>() + .To<XyzDependency>() .Bind<IService>().To<Service>() // Specifies to create the composition root named "Root" diff --git a/tests/Pure.DI.UsageTests/Advanced/TagOnMemberScenario.cs b/tests/Pure.DI.UsageTests/Advanced/TagOnMemberScenario.cs index d1cb61a38..5e75a077a 100644 --- a/tests/Pure.DI.UsageTests/Advanced/TagOnMemberScenario.cs +++ b/tests/Pure.DI.UsageTests/Advanced/TagOnMemberScenario.cs @@ -52,7 +52,7 @@ public void Run() DI.Setup(nameof(Composition)) .Bind().To<AbcDependency>() .Bind(Tag.OnMember<Service>(nameof(Service.Dependency))) - .To<XyzDependency>() + .To<XyzDependency>() .Bind<IService>().To<Service>() // Specifies to create the composition root named "Root" diff --git a/tests/Pure.DI.UsageTests/Advanced/TagOnMethodArgScenario.cs b/tests/Pure.DI.UsageTests/Advanced/TagOnMethodArgScenario.cs index 0d1350979..b7997ed5c 100644 --- a/tests/Pure.DI.UsageTests/Advanced/TagOnMethodArgScenario.cs +++ b/tests/Pure.DI.UsageTests/Advanced/TagOnMethodArgScenario.cs @@ -56,7 +56,7 @@ public void Run() DI.Setup(nameof(Composition)) .Bind().To<AbcDependency>() .Bind(Tag.OnMethodArg<Service>(nameof(Service.Initialize), "dep")) - .To<XyzDependency>() + .To<XyzDependency>() .Bind<IService>().To<Service>() // Specifies to create the composition root named "Root" diff --git a/tests/Pure.DI.UsageTests/BaseClassLibrary/FuncScenario.cs b/tests/Pure.DI.UsageTests/BaseClassLibrary/FuncScenario.cs index 12b3d850e..551d08d42 100644 --- a/tests/Pure.DI.UsageTests/BaseClassLibrary/FuncScenario.cs +++ b/tests/Pure.DI.UsageTests/BaseClassLibrary/FuncScenario.cs @@ -28,11 +28,11 @@ interface IService class Service(Func<IDependency> dependencyFactory) : IService { - public ImmutableArray<IDependency> Dependencies { get; } = + public ImmutableArray<IDependency> Dependencies => [ - ..Enumerable - .Range(0, 10) - .Select(_ => dependencyFactory()) + dependencyFactory(), + dependencyFactory(), + dependencyFactory() ]; } // } @@ -52,7 +52,7 @@ public void Run() var composition = new Composition(); var service = composition.Root; - service.Dependencies.Length.ShouldBe(10); + service.Dependencies.Length.ShouldBe(3); // } composition.SaveClassDiagram(); } diff --git a/tests/Pure.DI.UsageTests/BaseClassLibrary/OverridingBclBindingScenario.cs b/tests/Pure.DI.UsageTests/BaseClassLibrary/OverridingBclBindingScenario.cs index 691502a3e..cf57b6352 100644 --- a/tests/Pure.DI.UsageTests/BaseClassLibrary/OverridingBclBindingScenario.cs +++ b/tests/Pure.DI.UsageTests/BaseClassLibrary/OverridingBclBindingScenario.cs @@ -39,12 +39,9 @@ public void Run() { // { DI.Setup(nameof(Composition)) - .Bind<IDependency[]>().To(_ => new IDependency[] - { - new AbcDependency(), - new XyzDependency(), - new AbcDependency() - }) + .Bind<IDependency[]>().To<IDependency[]>(_ => + [new AbcDependency(), new XyzDependency(), new AbcDependency()] + ) .Bind<IService>().To<Service>() // Composition root diff --git a/tests/Pure.DI.UsageTests/Basics/ResolveMethodsScenario.cs b/tests/Pure.DI.UsageTests/Basics/ResolveMethodsScenario.cs index 8b3eac9be..967cf6c07 100644 --- a/tests/Pure.DI.UsageTests/Basics/ResolveMethodsScenario.cs +++ b/tests/Pure.DI.UsageTests/Basics/ResolveMethodsScenario.cs @@ -21,6 +21,7 @@ #pragma warning disable CS9113 // Parameter is unread. namespace Pure.DI.UsageTests.Basics.ResolveMethodsScenario; +using System.Diagnostics.CodeAnalysis; using Shouldly; using Xunit; @@ -36,6 +37,7 @@ class Service(IDependency dependency) : IService; class OtherService : IService; // } +[SuppressMessage("Usage", "CA2263:Предпочитать универсальную перегрузку, если тип известен")] public class Scenario { [Fact] diff --git a/tests/Pure.DI.UsageTests/Basics/RootBindScenario.cs b/tests/Pure.DI.UsageTests/Basics/RootBindScenario.cs index b0f63ef19..94c035750 100644 --- a/tests/Pure.DI.UsageTests/Basics/RootBindScenario.cs +++ b/tests/Pure.DI.UsageTests/Basics/RootBindScenario.cs @@ -37,8 +37,8 @@ public void Run() .Bind().As(Lifetime.Singleton).To<Dependency>() .RootBind<IService>("MyRoot").To<Service>(); // It's the same as: - // .Bind<IService>().To<Service>() - // .Root<IService>("MyRoot") + // .Bind<IService>().To<Service>() + // .Root<IService>("MyRoot") var composition = new Composition(); composition.MyRoot.ShouldBeOfType<Service>(); diff --git a/tests/Pure.DI.UsageTests/Basics/SimplifiedBindingScenario.cs b/tests/Pure.DI.UsageTests/Basics/SimplifiedBindingScenario.cs index 988a3d9f7..a7f11cb5a 100644 --- a/tests/Pure.DI.UsageTests/Basics/SimplifiedBindingScenario.cs +++ b/tests/Pure.DI.UsageTests/Basics/SimplifiedBindingScenario.cs @@ -101,8 +101,8 @@ public void Run() // for all abstract but NOT special types that are directly implemented. // So that's the equivalent of the following: // .Bind<IDependency, IOtherDependency, Dependency>() - // .As(Lifetime.PerBlock) - // .To<Dependency>() + // .As(Lifetime.PerBlock) + // .To<Dependency>() .Bind().As(Lifetime.PerBlock).To<Dependency>() .Bind().To<Service>() diff --git a/tests/Pure.DI.UsageTests/Hints/OnCannotResolveHintScenario.cs b/tests/Pure.DI.UsageTests/Hints/OnCannotResolveHintScenario.cs index a301e6d84..14f2b21e3 100644 --- a/tests/Pure.DI.UsageTests/Hints/OnCannotResolveHintScenario.cs +++ b/tests/Pure.DI.UsageTests/Hints/OnCannotResolveHintScenario.cs @@ -49,7 +49,7 @@ private partial T OnCannotResolve<T>( { if (typeof(T) == typeof(string)) { - return (T)(object)"Dependency with name"; + return (T)(object)"My name"; } throw new InvalidOperationException("Cannot resolve."); @@ -73,7 +73,7 @@ public void Run() var composition = new Composition(); var service = composition.Root; - service.Dependency.ToString().ShouldBe("Dependency with name"); + service.Dependency.ToString().ShouldBe("My name"); // } composition.SaveClassDiagram(); diff --git a/tests/Pure.DI.UsageTests/Hints/OnDependencyInjectionHintScenario.cs b/tests/Pure.DI.UsageTests/Hints/OnDependencyInjectionHintScenario.cs index ff88e14b7..fca1a16fe 100644 --- a/tests/Pure.DI.UsageTests/Hints/OnDependencyInjectionHintScenario.cs +++ b/tests/Pure.DI.UsageTests/Hints/OnDependencyInjectionHintScenario.cs @@ -16,9 +16,9 @@ // ReSharper disable ArrangeTypeModifiers // ReSharper disable UnusedMember.Global +// ReSharper disable NotAccessedPositionalProperty.Global namespace Pure.DI.UsageTests.Hints.OnDependencyInjectionHintScenario; -using System.Collections.Immutable; using Shouldly; using Xunit; @@ -27,7 +27,7 @@ namespace Pure.DI.UsageTests.Hints.OnDependencyInjectionHintScenario; interface IDependency; -class Dependency : IDependency; +record Dependency(int Id) : IDependency; interface IService { @@ -66,16 +66,20 @@ public void Run() // { // OnDependencyInjection = On DI.Setup(nameof(Composition)) - .Hint(OnDependencyInjectionContractTypeNameRegularExpression, nameof(IDependency)) + .Hint(OnDependencyInjectionContractTypeNameRegularExpression, "(.*IDependency|int)$") + .RootArg<int>("id") .Bind().To<Dependency>() .Bind().To<Service>() - .Root<IService>("Root"); + .Root<IService>("GetRoot"); var log = new List<string>(); var composition = new Composition(log); - var service = composition.Root; + var service = composition.GetRoot(33); - log.ShouldBe(ImmutableArray.Create("Dependency injected")); + log.ShouldBe([ + "Int32 injected", + "Dependency injected" + ]); // } composition.SaveClassDiagram(); } diff --git a/tests/Pure.DI.UsageTests/Hints/OnNewInstanceHintScenario.cs b/tests/Pure.DI.UsageTests/Hints/OnNewInstanceHintScenario.cs index 0889472d8..bcc9bf8ad 100644 --- a/tests/Pure.DI.UsageTests/Hints/OnNewInstanceHintScenario.cs +++ b/tests/Pure.DI.UsageTests/Hints/OnNewInstanceHintScenario.cs @@ -54,7 +54,7 @@ partial void OnNewInstance<T>( ref T value, object? tag, Lifetime lifetime) => - _log.Add(typeof(T).Name); + _log.Add($"{typeof(T).Name} created"); } // } @@ -76,7 +76,10 @@ public void Run() var service1 = composition.Root; var service2 = composition.Root; - log.ShouldBe([nameof(Dependency), nameof(Service), nameof(Service)]); + log.ShouldBe([ + "Dependency created", + "Service created", + "Service created"]); // } composition.SaveClassDiagram(); } diff --git a/tests/Pure.DI.UsageTests/Interception/AdvancedInterceptionScenario.cs b/tests/Pure.DI.UsageTests/Interception/AdvancedInterceptionScenario.cs index fded96e59..725e88a98 100644 --- a/tests/Pure.DI.UsageTests/Interception/AdvancedInterceptionScenario.cs +++ b/tests/Pure.DI.UsageTests/Interception/AdvancedInterceptionScenario.cs @@ -11,6 +11,7 @@ // ReSharper disable ConvertIfStatementToReturnStatement // ReSharper disable ArrangeTypeModifiers +// ReSharper disable UnusedMethodReturnValue.Global namespace Pure.DI.UsageTests.Interception.AdvancedInterceptionScenario; using System.Collections.Immutable; @@ -22,30 +23,26 @@ namespace Pure.DI.UsageTests.Interception.AdvancedInterceptionScenario; // { public interface IDependency { - void DependencyCall(); + int DependencyRun(); } class Dependency : IDependency { - public void DependencyCall() - { - } + public int DependencyRun() => 33; } public interface IService { IDependency Dependency { get; } - void ServiceCall(); + string ServiceRun(); } class Service(IDependency dependency) : IService { public IDependency Dependency { get; } = dependency; - - public void ServiceCall() - { - } + + public string ServiceRun() => "Abc"; } internal partial class Composition : IInterceptor @@ -78,8 +75,8 @@ private partial T OnDependencyInjection<T>( public void Intercept(IInvocation invocation) { - _log.Add(invocation.Method.Name); invocation.Proceed(); + _log.Add($"{invocation.Method.Name} returns {invocation.ReturnValue}"); } private static class ProxyFactory<T> @@ -127,14 +124,14 @@ public void Run() var log = new List<string>(); var composition = new Composition(log); var service = composition.Root; - service.ServiceCall(); - service.Dependency.DependencyCall(); + service.ServiceRun(); + service.Dependency.DependencyRun(); log.ShouldBe( ImmutableArray.Create( - "ServiceCall", - "get_Dependency", - "DependencyCall")); + "ServiceRun returns Abc", + "get_Dependency returns Castle.Proxies.IDependencyProxy", + "DependencyRun returns 33")); // } composition.SaveClassDiagram(); } diff --git a/tests/Pure.DI.UsageTests/Lifetimes/AutoScopedScenario.cs b/tests/Pure.DI.UsageTests/Lifetimes/AutoScopedScenario.cs index f8c86432e..7afce83b2 100644 --- a/tests/Pure.DI.UsageTests/Lifetimes/AutoScopedScenario.cs +++ b/tests/Pure.DI.UsageTests/Lifetimes/AutoScopedScenario.cs @@ -51,14 +51,11 @@ static void Setup() => // Session composition root .Root<Service>("SessionRoot", kind: RootKinds.Private) // Auto scoped - .Bind().To<IService>(ctx => + .Bind().To(IService (Composition baseComposition) => { - // Injects a base composition - ctx.Inject(out Composition baseComposition); - // Creates a session var session = new Composition(baseComposition); - + // Provides a root return session.SessionRoot; })