ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

ExtraData isn't used #236

Closed SuperJMN closed 4 years ago

SuperJMN commented 4 years ago

Given this test, I expect myClass to be instatiated, but it's not. It tried to create an instance of DependencyA instead of the provided one (via ExtraData).

    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {
            var c = new DependencyInjectionContainer();
            c.Configure(x => x.Export<MyClass>());
            c.Configure(x => x.Export<DependencyB>());

            var myClass = c.Locate<MyClass>(new { depA = new DependencyA("greetings")});
        }
    }

    public class MyClass
    {
        public MyClass(DependencyA depA, DependencyB depB)
        {

        }
    }

    public class DependencyB
    {
    }

    public class DependencyA
    {
        public DependencyA(string str)
        {

        }
    }
ipjohnson commented 4 years ago

@SuperJMN I'm leaving a comment just in case other people find this as we already discussed this offline.

The reason why the extra data is not used is because grace will attempt to concrete types first instead of looking in extra data. There are a couple ways to address this.

c.Configure(c => c.ExportFactory<IInjectionContext,DependencyA>(
     context => context.GetExtraData('depA')));