private IEnumerable <object> ResolveGenerical(Type dep, string name)
        {
            List <object> result = new List <object>();
            IEnumerable <ImplementationInfo.ImplementationInfo> implementationInformation
                = _dependencyConfig.GetImplementations(dep)
                  .Where((impl) =>
                         !_recursionExcludTypes[Thread.CurrentThread.ManagedThreadId].Contains(impl.ImplementationType));

            if (name != null)
            {
                implementationInformation = implementationInformation.Where(
                    (container) => container.Name == name);
            }
            object instance = null;

            foreach (ImplementationInfo.ImplementationInfo info in implementationInformation)
            {
                instance = CreateByConstructor(info.ImplementationType.GetGenericTypeDefinition()
                                               .MakeGenericType(dep.GenericTypeArguments));
                if (instance != null)
                {
                    result.Add(instance);
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        public void NonGenericsTypeRegisterTest()
        {
            _dependencyConfig.Register <ISimpleInterface, SimpleImplementation1>();
            _dependencyConfig.Register <ISimpleInterface, SimpleImplementation2>();
            var registeredImplementations = _dependencyConfig.GetImplementations(typeof(ISimpleInterface)).ToList();

            Assert.AreEqual(2, registeredImplementations.Count);
            List <Type> expectedRegisteredTypes = new List <Type>
            {
                typeof(SimpleImplementation1),
                typeof(SimpleImplementation2)
            };

            CollectionAssert.AreEquivalent(expectedRegisteredTypes,
                                           registeredImplementations.Select((implementation) => implementation.ImplementationType).ToList());
        }
Exemplo n.º 3
0
        internal object Resolve(Type dependency, string name = null)
        {
            Implementation[] impls;
            object           result;

            var currThreadId = Thread.CurrentThread.ManagedThreadId;

            if (!_recursionControlStackByThreadId
                .TryGetValue(currThreadId, out var atResolvingTypes))
            {
                atResolvingTypes = new Stack <Type>();

                if (!_recursionControlStackByThreadId.TryAdd(currThreadId, atResolvingTypes))
                {
                    throw new ApplicationException();
                }
            }
            else
            {
                //check recursion dependency
                if (atResolvingTypes.Contains(dependency))
                {
                    //try create it by constructor
                    result = CreateByConstructor(dependency);
                    if (result != null)
                    {
                        return(result);
                    }

                    throw new ArgumentException("Dependencies are recursive.");
                }
            }

            atResolvingTypes.Push(dependency);

            if (dependency.IsPrimitive ||
                !dependency.IsClass && !dependency.IsInterface)
            {
                result = Activator.CreateInstance(dependency);
            }
            else if (typeof(IEnumerable).IsAssignableFrom(dependency))
            {
                var dependencyType = dependency.GetGenericArguments()[0];
                impls = _config.GetImplementations(dependencyType).ToArray();

                var implInstances =
                    (object[])Activator.CreateInstance(dependencyType.MakeArrayType(), impls.Length);

                for (var i = 0; i < impls.Length; i++)
                {
                    implInstances[i] = impls[i].ResolveOrReturnSingletonInstance(this, name);
                }

                result = implInstances;
            }
            else
            {
                impls = _config.GetImplementations(dependency)?.ToArray();
                if (impls == null && dependency.IsGenericType) //handle search for open generic types impls as well
                {
                    impls = _config.GetImplementations(dependency.GetGenericTypeDefinition())?.ToArray();
                }

                if (impls != null)
                {
                    var implToUse = impls.First();
                    if (name != null)
                    {
                        implToUse = Array.Find(impls, impl => impl.Name == name);
                    }

                    result = implToUse?.ResolveOrReturnSingletonInstance(this,
                                                                         name); //TODO: resolve dependency on GenericType of impl of open generic dependency
                }
                else
                {
                    result = CreateByConstructor(dependency);
                }
            }

            atResolvingTypes.Pop();
            return(result);
        }