From a55f0dcf70fe85712b9b8da3b1925b84336d5ef0 Mon Sep 17 00:00:00 2001 From: "Jeremy D. Miller" Date: Sun, 5 Mar 2023 11:18:44 -0600 Subject: [PATCH] Changed the signature for Instance.RequiresServiceProvider to a method that takes in IMethodVariables --- src/Lamar.Testing/IoC/Instances/ConstructorInstanceTests.cs | 5 +++-- src/Lamar.Testing/IoC/Instances/LambdaInstanceTests.cs | 2 +- src/Lamar/IoC/Frames/ServiceVariableSource.cs | 6 +++--- src/Lamar/IoC/Instances/Instance.cs | 2 +- src/Lamar/IoC/Instances/LambdaInstance.cs | 2 +- src/Lamar/IoC/Instances/ReferencedInstance.cs | 2 +- src/Lamar/IoC/Lazy/FuncByNameInstance.cs | 2 +- src/Lamar/IoC/Lazy/FuncInstance.cs | 2 +- src/Lamar/IoC/Lazy/LazyInstance.cs | 2 +- src/Lamar/Lamar.csproj | 4 ++-- 10 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Lamar.Testing/IoC/Instances/ConstructorInstanceTests.cs b/src/Lamar.Testing/IoC/Instances/ConstructorInstanceTests.cs index 1482988f..7dde3596 100644 --- a/src/Lamar.Testing/IoC/Instances/ConstructorInstanceTests.cs +++ b/src/Lamar.Testing/IoC/Instances/ConstructorInstanceTests.cs @@ -7,6 +7,7 @@ using Lamar.IoC.Instances; using Lamar.IoC.Resolvers; using Microsoft.Extensions.DependencyInjection; +using NSubstitute; using Shouldly; using StructureMap.Testing.Widget; using Xunit; @@ -179,7 +180,7 @@ public void requires_service_provider_with_dependencies_negative() instance.CreatePlan(theGraph); - instance.RequiresServiceProvider.ShouldBeFalse(); + instance.RequiresServiceProvider(Substitute.For()).ShouldBeFalse(); } [Fact] @@ -196,7 +197,7 @@ public void requires_service_provider_with_dependencies_positive() instance.CreatePlan(theGraph); - instance.RequiresServiceProvider.ShouldBeTrue(); + instance.RequiresServiceProvider(Substitute.For()).ShouldBeTrue(); } diff --git a/src/Lamar.Testing/IoC/Instances/LambdaInstanceTests.cs b/src/Lamar.Testing/IoC/Instances/LambdaInstanceTests.cs index 63c348c2..8f0fbaa5 100644 --- a/src/Lamar.Testing/IoC/Instances/LambdaInstanceTests.cs +++ b/src/Lamar.Testing/IoC/Instances/LambdaInstanceTests.cs @@ -22,7 +22,7 @@ public void derive_the_default_name() public void requires_service_provider() { LambdaInstance.For(s => new Clock()) - .RequiresServiceProvider.ShouldBeTrue(); + .RequiresServiceProvider(null).ShouldBeTrue(); } diff --git a/src/Lamar/IoC/Frames/ServiceVariableSource.cs b/src/Lamar/IoC/Frames/ServiceVariableSource.cs index 746e3351..9c1185e7 100644 --- a/src/Lamar/IoC/Frames/ServiceVariableSource.cs +++ b/src/Lamar/IoC/Frames/ServiceVariableSource.cs @@ -60,10 +60,10 @@ public Variable Create(Type type) return standin; } - // TODO -- later, do we use other variables? - public void ReplaceVariables() + public void ReplaceVariables(IMethodVariables method) { - if (_usesNestedContainerDirectly || _standins.Any(x => x.Instance.RequiresServiceProvider)) + // TODO -- MORE HERE!!!! + if (_usesNestedContainerDirectly || _standins.Any(x => x.Instance.RequiresServiceProvider(method))) { useServiceProvider(); } diff --git a/src/Lamar/IoC/Instances/Instance.cs b/src/Lamar/IoC/Instances/Instance.cs index 44ab2dcc..489a84a8 100644 --- a/src/Lamar/IoC/Instances/Instance.cs +++ b/src/Lamar/IoC/Instances/Instance.cs @@ -125,7 +125,7 @@ public virtual object QuickResolve(Scope scope) public int Hash { get; set; } - public virtual bool RequiresServiceProvider => Dependencies.Any(x => x.RequiresServiceProvider || x.ImplementationType == typeof(IContainer) || x.ImplementationType == typeof(IServiceProvider)); + public virtual bool RequiresServiceProvider(IMethodVariables method) => Dependencies.Any(x => x.RequiresServiceProvider(method) || x.ImplementationType == typeof(IContainer) || x.ImplementationType == typeof(IServiceProvider)); public ServiceLifetime Lifetime { get; set; } = ServiceLifetime.Transient; diff --git a/src/Lamar/IoC/Instances/LambdaInstance.cs b/src/Lamar/IoC/Instances/LambdaInstance.cs index f041badf..2d252f13 100644 --- a/src/Lamar/IoC/Instances/LambdaInstance.cs +++ b/src/Lamar/IoC/Instances/LambdaInstance.cs @@ -39,7 +39,7 @@ public LambdaInstance(Type serviceType, Func factory, Servi // This is important. If the lambda instance is a singleton, it's injected as a singleton // to an object constructor and does not need the ServiceProvider - public override bool RequiresServiceProvider => Lifetime != ServiceLifetime.Singleton; + public override bool RequiresServiceProvider(IMethodVariables method) => Lifetime != ServiceLifetime.Singleton; public string Description { get; set; } public override Variable CreateVariable(BuildMode mode, ResolverVariables variables, bool isRoot) diff --git a/src/Lamar/IoC/Instances/ReferencedInstance.cs b/src/Lamar/IoC/Instances/ReferencedInstance.cs index 557960a8..4d8948ce 100644 --- a/src/Lamar/IoC/Instances/ReferencedInstance.cs +++ b/src/Lamar/IoC/Instances/ReferencedInstance.cs @@ -38,7 +38,7 @@ public override object QuickResolve(Scope scope) return _inner.QuickResolve(scope); } - public override bool RequiresServiceProvider => _inner.RequiresServiceProvider; + public override bool RequiresServiceProvider(IMethodVariables method) => _inner.RequiresServiceProvider(method); public override Variable CreateInlineVariable(ResolverVariables variables) { diff --git a/src/Lamar/IoC/Lazy/FuncByNameInstance.cs b/src/Lamar/IoC/Lazy/FuncByNameInstance.cs index 97e9285d..9a4c2ab0 100644 --- a/src/Lamar/IoC/Lazy/FuncByNameInstance.cs +++ b/src/Lamar/IoC/Lazy/FuncByNameInstance.cs @@ -21,7 +21,7 @@ public override Variable CreateVariable(BuildMode mode, ResolverVariables variab } - public override bool RequiresServiceProvider { get; } = true; + public override bool RequiresServiceProvider(IMethodVariables method) => true; public override Func ToResolver(Scope topScope) { diff --git a/src/Lamar/IoC/Lazy/FuncInstance.cs b/src/Lamar/IoC/Lazy/FuncInstance.cs index b4e2d85a..a7582138 100644 --- a/src/Lamar/IoC/Lazy/FuncInstance.cs +++ b/src/Lamar/IoC/Lazy/FuncInstance.cs @@ -22,7 +22,7 @@ public override Variable CreateVariable(BuildMode mode, ResolverVariables variab } - public override bool RequiresServiceProvider { get; } = true; + public override bool RequiresServiceProvider(IMethodVariables method) => true; public override Func ToResolver(Scope topScope) { diff --git a/src/Lamar/IoC/Lazy/LazyInstance.cs b/src/Lamar/IoC/Lazy/LazyInstance.cs index fd8363e3..a78738d3 100644 --- a/src/Lamar/IoC/Lazy/LazyInstance.cs +++ b/src/Lamar/IoC/Lazy/LazyInstance.cs @@ -22,7 +22,7 @@ public override Variable CreateVariable(BuildMode mode, ResolverVariables variab } - public override bool RequiresServiceProvider { get; } = true; + public override bool RequiresServiceProvider(IMethodVariables method) => true; public override object Resolve(Scope scope) { diff --git a/src/Lamar/Lamar.csproj b/src/Lamar/Lamar.csproj index c3a8d304..579f2fd8 100644 --- a/src/Lamar/Lamar.csproj +++ b/src/Lamar/Lamar.csproj @@ -20,8 +20,8 @@ - - + +