ipjohnson / Grace

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

LocateKey doesn't correctly carry #99

Closed ipjohnson closed 7 years ago

ipjohnson commented 7 years ago

Func<T> and other wrappers should carry the locate key through the request. This test should pass when finished. Relates to issue #98

        public class FuncFactoryClass
        {
            private Func<DisposableService> _func;

            public FuncFactoryClass(Func<DisposableService> func)
            {
                _func = func;
            }

            public DisposableService CreateService()
            {
                return _func();
            }
        }

        [Fact]
        public void Keyed_Factory_And_NonKeyed_With_Different_Lifestyle()
        {
            var container = new DependencyInjectionContainer();

            container.Configure(c =>
            {
                c.Export<DisposableService>().Lifestyle.SingletonPerNamedScope("CustomScopeName");
                c.Export<DisposableService>().AsKeyed<DisposableService>("TransientKey").ExternallyOwned();
                c.Export<FuncFactoryClass>().WithCtorParam<Func<DisposableService>>().LocateWithKey("TransientKey");
            });

            bool disposedService = false;
            bool disposedTransient = false;

            using (var scope = container.BeginLifetimeScope("CustomScopeName"))
            {
                var service = scope.Locate<DisposableService>();

                Assert.Same(service, scope.Locate<DisposableService>());

                service.Disposing += (sender, args) => disposedService = true;

                var factory = scope.Locate<FuncFactoryClass>();

                var transientService = factory.CreateService();

                Assert.NotSame(service, transientService);

                transientService.Disposing += (sender, args) => disposedTransient = true;
            }

            Assert.True(disposedService);
            Assert.False(disposedTransient);
        }