예제 #1
0
        internal object GetInstance(Type type, Scoped scope)
        {
            Func <Scoped, object?>?func = _getInstanceCache.GetOrDefault(type);

            if (func == null)
            {
                func = DependencyGraph.Resolve(type)?.Factory ?? (s => null !);
                _getInstanceCache.Add(type, func);
            }
            return(func(scope) !);
        }
예제 #2
0
        internal object GetInstance(Type type, Scoped scope)
        {
            Func <Scoped, object> func = _getInstanceCache.GetOrDefault(type);

            if (func == null)
            {
                func = _dependencyGraph.Resolve(type).Factory;
                _getInstanceCache.Add(type, func);
            }
            return(func(scope));
        }
예제 #3
0
        internal void LateInject(object instance, Scoped scope)
        {
            Type type = instance.GetType();
            Action <Scoped, object>?action = _injectionCache.GetOrDefault(type);

            if (action == null)
            {
                action = GenerateLateInjector(type);
                _injectionCache.Add(type, action);
            }
            action(scope, instance);
        }
예제 #4
0
        internal object?GetInstanceOrDefault(Type type, Scoped scope)
        {
            Func <Scoped, object?>?func = _getInstanceOrDefaultCache.GetOrDefault(type);

            if (func == null)
            {
                func = DependencyGraph.TryResolve(type)?.Factory;
                if (func == null)
                {
                    func = s => null;
                }

                _getInstanceOrDefaultCache.Add(type, func);
            }
            return(func(scope));
        }
        public void AddAndGet()
        {
            var testCases = new List <(ReferenceInt input, object output)>();

            for (var i = 0; i < 100; i++)
            {
                testCases.Add((new ReferenceInt(i), new object()));
            }

            var dic = new ThreadSafeDictionary <ReferenceInt, object>();

            var tester = new ThreadSafetyTester <(ReferenceInt input, object expectedOutput)>(() => testCases);

            tester.Test(testCase =>
            {
                dic.Add(testCase.input, testCase.expectedOutput);
                object output = dic.GetOrDefault(testCase.input);
                Assert.Equal(testCase.expectedOutput, output);
            });

            Assert.Equal(testCases.Count * tester.TaskCount, dic.Count);
        }