diff --git a/src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs b/src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs index 66e3327cc..5bd8d7122 100644 --- a/src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs +++ b/src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs @@ -6,7 +6,8 @@ namespace Pure.DI.Core.Code; internal class FactoryCodeBuilder( IIdGenerator idGenerator, INodeInfo nodeInfo, - IArguments arguments) + IArguments arguments, + ITypeResolver typeResolver) : ICodeBuilder { private static readonly string InjectionStatement = $"{Names.InjectionMarker};"; @@ -80,7 +81,7 @@ public void Build(BuildContext ctx, in DpFactory factory) using (code.Indent(indent.Value)) { ctx.StatementBuilder.Build(injectionsCtx with { Level = level, Variable = argument.Current, LockIsRequired = lockIsRequired }, argument); - code.AppendLine($"{(injection.DeclarationRequired ? "var " : "")}{injection.VariableName} = {ctx.BuildTools.OnInjected(ctx, argument.Current)};"); + code.AppendLine($"{(injection.DeclarationRequired ? $"{typeResolver.Resolve(argument.Current.Injection.Type)} " : "")}{injection.VariableName} = {ctx.BuildTools.OnInjected(ctx, argument.Current)};"); } } else diff --git a/tests/Pure.DI.IntegrationTests/FactoryTests.cs b/tests/Pure.DI.IntegrationTests/FactoryTests.cs index ef211ef1a..67ebea0c8 100644 --- a/tests/Pure.DI.IntegrationTests/FactoryTests.cs +++ b/tests/Pure.DI.IntegrationTests/FactoryTests.cs @@ -1159,4 +1159,81 @@ public static void Main() result.Success.ShouldBeTrue(result); result.GeneratedCode.Split(Environment.NewLine).Count(i => i.Contains(" = new Sample.Dependency2();")).ShouldBe(1); } + + [Fact] + public async Task ShouldSupportFactoryWhenExplicitImplementation() + { + // Given + + // When + var result = await """ +using System; +using Pure.DI; + +namespace Sample +{ + interface IDependency +{ + void Init(); +} + +class Dependency : IDependency +{ + void IDependency.Init() + { + } +} + +interface IService +{ +} + +class Service : IService +{ + public Service(IDependency dependency) + { + } +} + +partial class Composition +{ + private void Setup() => + DI.Setup(nameof(Composition)) + .Hint(Hint.OnNewInstance, "On") + .Hint(Hint.OnDependencyInjection, "On") + .Bind().As(Lifetime.Singleton).To() + .Bind().To(ctx => + { + ctx.Inject(out IDependency dependency); + dependency.Init(); + return new Service(dependency); + }) + .Root("MyService"); + + partial void OnNewInstance(ref T value, object? tag, Lifetime lifetime) + { + System.Console.WriteLine(typeof(T)); + } + + private partial T OnDependencyInjection(in T value, object? tag, Lifetime lifetime) + { + System.Console.WriteLine(typeof(T)); + return value; + } +} + + public class Program + { + public static void Main() + { + var service = new Composition().MyService; + } + } +} +""".RunAsync(new Options(LanguageVersion.CSharp9)); + + // Then + result.Success.ShouldBeTrue(result); + result.StdOut.ShouldBe(["Sample.Dependency", "Sample.IDependency", "Sample.Service", "Sample.IService"], result); + } } \ No newline at end of file