Пример #1
0
        /// <summary>
        /// Get type instance from factory
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public object Get(Type type)
        {
            if (!collections.IsBuild)
            {
                throw new FactoryConfigurationException("TinyFactory is not configured");
            }
            var descriptor = collections.FirstOrDefault(o => o.ImplementationType.Equals(type) || o.ServiceType.Equals(type));

            if (ThrowNotExist && descriptor == null)
            {
                throw new FactoryConfigurationException($"Unknown parameter type ({type.Name})");
            }
            return(descriptor?.Resolve(this));
        }
Пример #2
0
        internal static bool AllowAddServiceToCollection <TService>(this IFactoryCollection collection)
        {
            if (collection.IsReadOnly)
            {
                throw new InvalidOperationException("You cannot add to collection. Factory Collection is read-only");
            }

            if (collection.FirstOrDefault(o => o.ServiceType.Equals(typeof(TService))) != null)
            {
                throw new Exception("This type of service has been registered to the factory before");
            }

            var constructors = typeof(TService).GetConstructors();

            if (constructors == null || constructors.Length == 0)
            {
                throw new Exception("This type of service has no public constructors");
            }

            return(true);
        }