ipjohnson / Grace

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

LocateException when resolving factory dependency with extra data #152

Closed krasin-ga closed 6 years ago

krasin-ga commented 6 years ago

Code to reproduce:

  internal class Program
    {
        private static void Main(string[] args)
        {
            var container = new DependencyInjectionContainer();
            container.Configure(ConfigureRoot);

            var extraData = new
                            {
                                cd = (IInjectionContextDependency) new InjectionContextDependency()
                            };

            container.Locate<RootDependency>(extraData);
        }

        private static void ConfigureRoot(IExportRegistrationBlock obj)
        {
            obj.Export<RootDependency>();
            obj.ExportFactory<IExportLocatorScope, ChildDependency>(
              //exception here
                   s => new ChildDependency(s.Locate<IInjectionContextDependency>()))
               .As<ChildDependency>();
        }
    }

    public interface IInjectionContextDependency
    {
    }

    class InjectionContextDependency : IInjectionContextDependency
    {
    }

    public class ChildDependency
    {
        public ChildDependency(IInjectionContextDependency injectionContextDependency)
        {
        }
    }

    public class RootDependency
    {
        public RootDependency(ChildDependency childDependency)
        {
        }
    }
}
ipjohnson commented 6 years ago

I've checked in a change to address this but it won't be available till next release (hopefully this weekend), Here are some tests for this change.

You will have to options for your registration factory.

// Locate yourself and pass context through
obj.ExportFactory<IExportLocatorScope, IInjectionContext, ChildDependency>(
                 (s,c) => new ChildDependency(s.Locate<IInjectionContextDependency>(c)));
// depend directly on context dependency
obj.ExportFactory<IInjectionContextDependency, ChildDependency>(
                 d => new ChildDependency(d));