JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
566 stars 117 forks source link

Services added after construction cannot be consumed by previously added services. #247

Closed AaronAllBright closed 4 years ago

AaronAllBright commented 4 years ago

When Lamar ingests the ServiceRegistry provided during construction, it triggers planning for the services registered. If any dependencies of any of these services are missing, they are still marked as "planned". If the required service is later added by a call to container.Configure, it will be accessible in the container, but not to previously planned services.

    class Program
    {
        public interface IDependency {}
        public class Dependency : IDependency {}
        public interface IMyService {}
        public class MyService : IMyService
        {
            private readonly IDependency _dependency;
            public MyService(IDependency dependency)
            {
                _dependency = dependency;
            }
        }

        static void Main(string[] args)
        {
            var container = new Container(c =>
            {
                c.For<IMyService>().Use<MyService>();
            });
            container.Configure(sc =>
            {
                var c = (ServiceRegistry) sc;
                c.For<IDependency>().Use<Dependency>();
            });
            var depenency = container.GetInstance<IDependency>(); // Succeeds
            var instance = container.GetInstance<IMyService>(); // Fails
        }
    }
lilasquared commented 4 years ago

I've been scratching my head over this for a good hour or two trying to figure out what I was doing wrong in my registration. +1 to getting this fixed ASAP. It's critical for vertical slice integration tests that need to mock out one or two services.