ipjohnson / Grace.DependencyInjection.Extensions

Grace Extensions for ASP.Net Core
19 stars 7 forks source link

Wrong constructor used with Func<ArgClass,ResultClass> #28

Closed mgth closed 3 years ago

mgth commented 3 years ago

I would expect second constructor to be used in injectedB, but first is being used with new ClassA created and classB just dropped.

    class ClassA
    { }

    class ClassB
    { }

    class InjectedClass
    {
        public ClassA ClassA;
        public ClassB ClassB;

        public InjectedClass(ClassA classA)
        {
            ClassA = classA;
        }

        public InjectedClass(ClassB classB)
        {
            ClassB = classB;
        }
    }

    public class ConstructorSelection
    {
        [Fact]
        public void Test()
        {
            var container = new DependencyInjectionContainer();
            container.Configure(c => c.Export<ClassA>().As<ClassA>());
            container.Configure(c => c.Export<ClassB>().As<ClassB>());
            container.Configure(c => c.Export<InjectedClass>().As<InjectedClass>());

            var funcA = container.Locate<Func<ClassA, InjectedClass>>();
            var classA = container.Locate<ClassA>();
            var injectedA = funcA(classA);

            var funcB = container.Locate<Func<ClassB, InjectedClass>>();
            var classB = container.Locate<ClassB>();
            var injectedB = funcB(classB);

            Assert.Equal(classA,injectedA.ClassA);
            Assert.Equal(classB,injectedB.ClassB);
        }
    }
mgth commented 3 years ago

sorry posted on wrong depot.