Beispiel #1
0
        /// <summary>
        /// Determines if the class meets the criteria for dependency injection and returns the registration information.
        /// </summary>
        /// <param name="classData">The class to check.</param>
        /// <param name="rejectedClassNames">Optional parameter that determines if the class name matches the name in the list it cannot be used for registration.</param>
        /// <param name="rejectedBaseClasses">Optional parameter that determines if the class inherits the target base class it cannot be used for registration.</param>
        /// <param name="targetInterfaceTypeForRegistration">Optional parameter that will determine which interface will be used for registration, This will be the interface itself or anything that inherits the interface.</param>
        /// <returns>Null if the class does not qualify for dependency injection or the registration information if it does.</returns>
        public static DependencyInjectionRegistrationInformation IsTransientClass(CsClass classData, IEnumerable <string> rejectedClassNames = null, IEnumerable <ModelLookupData> rejectedBaseClasses = null, IEnumerable <ModelLookupData> targetInterfaceTypeForRegistration = null)
        {
            if (classData == null)
            {
                return(null);
            }
            if (!classData.IsLoaded)
            {
                return(null);
            }

            if (classData.IsStatic)
            {
                return(null);
            }

            if (!classData.Constructors.Any())
            {
                return(null);
            }
            if (classData.Constructors.Count(c => !c.IsStatic) > 1)
            {
                return(null);
            }

            var constructor = classData.Constructors.FirstOrDefault();

            if (constructor == null)
            {
                return(null);
            }
            if (constructor.Parameters.Any(p => p.ParameterType.IsWellKnownType))
            {
                return(null);
            }

            var className = classData.Name;

            if (rejectedClassNames != null)
            {
                if (rejectedClassNames.Any(c => c == className))
                {
                    return(null);
                }
            }

            if (rejectedBaseClasses != null)
            {
                if (rejectedBaseClasses.Any(r => classData.InheritsBaseClass(r.Name, r.Namespace)))
                {
                    return(null);
                }
            }

            CsInterface targetInterface = null;

            if (targetInterfaceTypeForRegistration != null)
            {
                foreach (var modelLookupData in targetInterfaceTypeForRegistration)
                {
                    targetInterface = classData.HasInterface(modelLookupData);
                    if (targetInterface != null)
                    {
                        break;
                    }
                }
            }

            if (targetInterface != null)
            {
                return(DependencyInjectionRegistrationInformation.Init(classData, targetInterface));
            }

            if (classData.InheritedInterfaces.Count == 1)
            {
                targetInterface = classData.InheritedInterfaces[0];
            }

            return(DependencyInjectionRegistrationInformation.Init(classData, targetInterface));
        }