public void Should_Use_Factory_Only_If_The_Value_Was_Not_Present()
        {
            //The key is already present, so it doesn't use the factory to create a new one
            Assert.Equal("TestValue1", _singletonCache.GetOrAdd("TestKey1", () => "TestValue1_Changed"));

            _singletonCache.Remove("TestKey1");

            //The key is not present, so it uses the factory to create a new one
            Assert.Equal("TestValue1_Changed", _singletonCache.GetOrAdd("TestKey1", () => "TestValue1_Changed"));
        }
示例#2
0
        public object GetService(Type serviceType, string name)
        {
            if (disposedValue)
            {
                throw new ObjectDisposedException("Has been disposed.");
            }
            var defintion = Defintions.TryGet(serviceType, name);

            if (defintion != null)
            {
                switch (defintion.Lifetime)
                {
                case Lifetime.Singleton:
                    return(SingletonCache.GetOrAdd(defintion, d => d.ImplementationFactory(Root)));

                case Lifetime.Scoped:
                    return(scopedCache.GetOrAdd(defintion, d => d.ImplementationFactory(this)));

                case Lifetime.Transient:
                    return(defintion.ImplementationFactory(this));

                default:
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }