Exemple #1
0
        /// <summary>
        /// Defines the transient registration statement that will register the class.
        /// </summary>
        /// <param name="classData">The class model to get the registration from.</param>
        /// <param name="serviceCollectionParameterName">The name of the service collection parameter that the transient is being made to.</param>
        /// <param name="manager">Optional parameter that contains the namespace manager that contains the known using statements and target namespace for the class that will host this registration data.</param>
        /// <returns>The formatted transient registration call or null if the class does not meet the criteria.</returns>
        private static string FormatTransientRegistration(CsClass classData, string serviceCollectionParameterName, NamespaceManager manager = null)
        {
            //Cannot find the class data will return null
            if (classData == null)
            {
                return(null);
            }

            string registrationType = null;
            string classType        = null;

            ICsMethod constructorData = classData.Constructors.FirstOrDefault();

            //Confirming we have a constructor
            if (constructorData == null)
            {
                return(null);
            }

            //Getting the fully qualified type name for the formatters library for the class.
            classType = classData.CSharpFormatBaseTypeName(manager);

            //if we are not able to format the class name correctly return null.
            if (classType == null)
            {
                return(null);
            }

            //Assuming the first interface inherited will be used for dependency injection if any are provided.
            if (classData.InheritedInterfaces.Any())
            {
                CsInterface interfaceData = classData.InheritedInterfaces.FirstOrDefault();

                if (interfaceData != null)
                {
                    registrationType = interfaceData.CSharpFormatInheritanceTypeName(manager);
                }
            }

            //Creating statement to add the the container.
            string diStatement = registrationType != null
                ? $"{serviceCollectionParameterName}.AddTransient<{registrationType},{classType}>();" :
                                 $"{serviceCollectionParameterName}.AddTransient<{classType}>();";

            return(diStatement);
        }
Exemple #2
0
        /// <summary>
        /// Defines the transient registration statement that will register a target class in dependency injection.
        /// </summary>
        /// <param name="classData">The class model to get the registration from.</param>
        /// <param name="serviceCollectionParameterName">The name of the service collection parameter that the transient is being made to.</param>
        /// <param name="targetInterface">The target interface model to use for transient registration, this is an optional parameter </param>
        /// <param name="manager">Optional parameter that contains the namespace manager that contains the known using statements and target namespace for the class that will host this registration data.</param>
        /// <returns>The formatted transient registration call or null if the class data is missing.</returns>
        public static string FormatTransientRegistrationForDependencyInjection(CsClass classData, string serviceCollectionParameterName, CsInterface targetInterface = null, NamespaceManager manager = null)
        {
            //Cannot find the class data will return null
            if (classData == null)
            {
                return(null);
            }

            string registrationType = null;
            string classType        = null;

            ICsMethod constructorData = classData.Constructors.FirstOrDefault();

            //Confirming we have a constructor
            if (constructorData == null)
            {
                return(null);
            }

            //Getting the fully qualified type name for the formatters library for the class.
            classType = classData.CSharpFormatBaseTypeName(manager);

            //if we are not able to format the class name correctly return null.
            if (classType == null)
            {
                return(null);
            }

            //If the interface was provided extract the name for setting up the registration.
            if (targetInterface != null)
            {
                registrationType = targetInterface.CSharpFormatInheritanceTypeName(manager);
            }

            //Creating statement to add the the container.
            string diStatement = registrationType != null
                ? $"{serviceCollectionParameterName}.AddTransient<{registrationType},{classType}>();" :
                                 $"{serviceCollectionParameterName}.AddTransient<{classType}>();";

            return(diStatement);
        }