/// <summary>
        ///     Gets an instance from the container, optionally by key.
        /// </summary>
        /// <typeparam name="T">The type of object registered in the container. </typeparam>
        /// <param name="key">The key.</param>
        /// <returns>The requested value from the container</returns>
        /// <exception cref="System.InvalidOperationException">Thrown if more than one builder of the specified type is registered</exception>
        public T Get <T>(string key = null) where T : class
        {
            if (internalChillContainer.IsRegistered(typeof(T)))
            {
                return(internalChillContainer.Get <T>());
            }

            // Combine the type and key into a string
            var initializedValuesKey = new Tuple <Type, string>(typeof(T), key);

            if (initializedValues.ContainsKey(initializedValuesKey))
            {
                return(initializedValues[initializedValuesKey] as T);
            }

            lock (syncRoot)
            {
                if (initializedValues.ContainsKey(initializedValuesKey))
                {
                    return(initializedValues[initializedValuesKey] as T);
                }

                var applicableMothers = objectMothers
                                        .Where(m => m.Applies(typeof(T)))
                                        .OrderBy(m => m.IsFallback)
                                        .ToList();

                if (!applicableMothers.Any())
                {
                    return(internalChillContainer.Get <T>(key));
                }

                if (applicableMothers.Count(m => !m.IsFallback) > 1)
                {
                    throw new InvalidOperationException(
                              $"There are more than one builders that apply to build: {typeof(T).Name}. Namely: {string.Join(",", applicableMothers.Select(x => x.GetType().Name))}.");
                }

                object item = applicableMothers
                              .First()
                              .Create(typeof(T), new ContainerResolverAdapter(internalChillContainer));

                initializedValues.Add(initializedValuesKey, item);
                return((T)item);
            }
        }
示例#2
0
        /// <summary>
        /// Gets an instance from the container, optionally by key.
        /// </summary>
        /// <typeparam name="T">The type of object registered in the container. </typeparam>
        /// <param name="key">The key.</param>
        /// <returns>The requested value from the container</returns>
        /// <exception cref="System.InvalidOperationException">Thrown if more than one builder of the specified type is registered</exception>
        public T Get <T>(string key = null) where T : class
        {
            if (internalChillContainer.IsRegistered <T>())
            {
                return(internalChillContainer.Get <T>());
            }

            // Combine the type and key into a string
            var initializedValuesKey = new Tuple <Type, string>(typeof(T), key);

            if (initializedValues.ContainsKey(initializedValuesKey))
            {
                return(initializedValues[initializedValuesKey] as T);
            }

            lock (syncRoot)
            {
                if (initializedValues.ContainsKey(initializedValuesKey))
                {
                    return(initializedValues[initializedValuesKey] as T);
                }

                var applicableMothers = autoMothers.Where(x => x.Applies(typeof(T))).ToList();

                if (!applicableMothers.Any())
                {
                    return(internalChillContainer.Get <T>(key));
                }
                if (applicableMothers.Count > 1)
                {
                    throw new InvalidOperationException(string.Format("There are more than one builders that apply to build: {0}. Namely: {1}.", typeof(T).Name, string.Join(",", applicableMothers.Select(x => x.GetType().Name))));
                }
                var item = applicableMothers.First().Create <T>(internalChillContainer);
                initializedValues.Add(initializedValuesKey, item);
                return(item);
            }
        }