예제 #1
0
        public override DependencyInjectionData Generate(string dependency, bool useShortName)
        {
            var dependencyInjectionData = new DependencyInjectionData
            {
                FieldType = dependency,
                ConstructorParameterType = dependency
            };

            // Default implementation with the lowest priority that generates the field and parameter name by adding
            // the underscore and shortens it if required.
            if (dependency.Length < 2)
            {
                dependencyInjectionData.FieldName = GetLowerInvariantStringWithUnderscore(dependency);
                dependencyInjectionData.ConstructorParameterName = GetLowerInvariantString(dependency);

                return(dependencyInjectionData);
            }

            var cleanedDependency = RemoveFirstLetterIfInterface(dependency);

            if (useShortName)
            {
                dependencyInjectionData.FieldName = GetShortNameWithUnderscore(cleanedDependency);
                dependencyInjectionData.ConstructorParameterName = GetShortName(cleanedDependency);

                return(dependencyInjectionData);
            }

            dependencyInjectionData.FieldName = GetStringWithUnderscore(GetCamelCased(cleanedDependency));
            dependencyInjectionData.ConstructorParameterName = GetCamelCased(cleanedDependency);

            return(dependencyInjectionData);
        }
        public IResult Inject(Document document, DependencyInjectionData dependencyInjectionData)
        {
            var context = new DependencyInjectionContext
            {
                FieldName    = dependencyInjectionData.FieldName,
                VariableName = dependencyInjectionData.ConstructorParameterName,
                FieldType    = dependencyInjectionData.FieldType,
                VariableType = dependencyInjectionData.ConstructorParameterType,
                ClassName    = GetExpectedClassName(document),
                Document     = document
            };

            // Get code lines from the document.
            GetCodeLines(context);

            // Determine the brace styling.
            DetermineBraceStyling(context);

            // Find the initial line of the class.
            GetClassStartLineIndex(context);
            if (context.ClassStartLineIndex == -1)
            {
                return(Result.FailedResult(DependencyInjectorErrorCodes.ClassNotFound));
            }

            // Find the initial line of the first constructor. If it hasn't been created yet then create it.
            GetConstructorLineIndex(context);
            if (context.ConstructorLineIndex == -1)
            {
                CreateConstructor(context);
            }

            // Update inner code of the constructor first.
            InsertConstructorCodeLine(context);

            // Update the parameters of the constructor.
            InsertInjectionToConstructor(context);

            // Add the private field to the class.
            InsertPrivateField(context);

            // Finally update the editor window and select the class name to be able to add usings if necessary.
            UpdateCodeEditorAndSelectDependency(context);

            return(Result.SuccessResult);
        }