Skip to content

Commit

Permalink
Use default TT factory to resolve Func<> if it has no binding
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Aug 17, 2022
1 parent d19a37d commit 04cbe20
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 33 deletions.
54 changes: 54 additions & 0 deletions Pure.DI.Tests/Integration/FuncResolveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -691,4 +691,58 @@ public static T Resolve<T>()
"1"
}, generatedCode);
}

[Fact]
// ReSharper disable once InconsistentNaming
public void ShouldSupportResolveFunctionTTWhenFuncWithArg()
{
// Given

// When
var output = @"
namespace Sample
{
using System;
using Pure.DI;
public class CompositionRoot
{
public readonly int Value;
internal CompositionRoot([Tag(1)] System.Func<string, int> value) => Value = value(""Abc"");
}
public interface IDep<T> { }
public class Dep<T>: IDep<T> { }
internal static partial class Composer
{
static Composer()
{
DI.Setup()
.Bind<TT>(1).To(_ => Other.Utils.Resolve<TT>())
.Bind<CompositionRoot>().To<CompositionRoot>();
}
}
}
namespace Other
{
public class Utils
{
public static T Resolve<T>()
{
if (typeof(T) == typeof(System.Func<string, int>)) return (T)(object)new System.Func<string, int>(str => 1);
return default(T);
}
}
}
".Run(out var generatedCode);

// Then
output.ShouldBe(new[]
{
"1"
}, generatedCode);
}
}
52 changes: 19 additions & 33 deletions Pure.DI/Core/TypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,50 +173,24 @@ private Dependency Resolve(SemanticType dependency, ExpressionSyntax? tag, bool
}
else
{
var dependency1 = typeSelector(dependency);
Dependency? resolvedDependency;
bool ret;
var key = new Key(dependency1, tag);
var realDependency = typeSelector(dependency);
var key = new Key(realDependency, tag);
if (_map.TryGetValue(key, out var implementationEntry))
{
var typesMap = _typesMapFactory();
typesMap.Setup(implementationEntry.Details, dependency1);
resolvedDependency = _factories.TryGetValue(key, out var factory)
? new Dependency(factory.Metadata, dependency1, tag, _factoryBuilder(), typesMap)
typesMap.Setup(implementationEntry.Details, realDependency);
var resolvedDependency = _factories.TryGetValue(key, out var factory)
? new Dependency(factory.Metadata, realDependency, tag, _factoryBuilder(), typesMap)
: new Dependency(implementationEntry.Metadata, implementationEntry.Details, tag, _constructorBuilder(), typesMap);

ret = true;
}
else
{
if (_defaultFactory.HasValue)
{
var defaultBinding = _defaultFactory.Value;
var typesMap = _typesMapFactory();
foreach (var defaultDependency in defaultBinding.Metadata.Dependencies)
{
typesMap.Setup(defaultDependency, dependency1);
}

resolvedDependency = new Dependency(defaultBinding.Metadata, dependency1, tag, _factoryBuilder(), typesMap);
ret = true;
}
else
{
resolvedDependency = default;
ret = false;
}
}

if (ret)
{
return resolvedDependency!.Value;
return resolvedDependency;
}
}

if (
!dependency.Type.IsAbstract
&& !_implementations.Contains(dependency)
&& !dependency.Name.StartsWith("System.Func`")
&& !GetSpecialTypes(dependency.SemanticModel).Contains(dependency)
&& dependency.IsValidTypeToResolve)
{
Expand All @@ -232,7 +206,19 @@ private Dependency Resolve(SemanticType dependency, ExpressionSyntax? tag, bool
AddBinding(newBinding);
return new Dependency(newBinding, dependency, tag, _constructorBuilder(), typesMap);
}

if (_defaultFactory.HasValue)
{
var defaultBinding = _defaultFactory.Value;
var typesMap = _typesMapFactory();
foreach (var defaultDependency in defaultBinding.Metadata.Dependencies)
{
typesMap.Setup(defaultDependency, dependency);
}

return new Dependency(defaultBinding.Metadata, dependency, tag, _factoryBuilder(), typesMap);
}

break;
}

Expand Down

0 comments on commit 04cbe20

Please sign in to comment.