public static async Task <IEnumerable <MemberDeclarationSyntax> > CreateMethodDeclarations(string controllerName, SyntaxNode wcfUnit,
                                                                                                   IEnumerable <string> wcfMethodNames, bool isAsmx)
        {
            // create new web api methods from filtered list of wcf methods
            var wcfMethods = wcfUnit
                             .DescendantNodes()
                             .OfType <MethodDeclarationSyntax>()
                             .Where(wcfMethod => wcfMethodNames.Contains(wcfMethod.Identifier.ValueText))
                             .ToList();

            // create members including methods and nested classes
            var wcfClassName = wcfUnit
                               .DescendantNodes()
                               .OfType <ClassDeclarationSyntax>()
                               .First()
                               .Identifier
                               .ValueText;

            // build unique method name mappings if any methods (i.e. overload) have duplicate names
            var namesMap = DuplicateMethodNamesGenerator.CreateMappings(wcfMethods);

            var methodsTasks = wcfMethods
                               .Select(async wcfMethod => await CreateMethods(controllerName, wcfMethod, wcfClassName, namesMap, isAsmx))
                               .ToList();

            var methods = (await Task.WhenAll(methodsTasks))
                          .SelectMany(method => method)
                          .ToList();

            return(methods);
        }
        private static async Task <IEnumerable <MemberDeclarationSyntax> > CreateMethods(string controllerName,
                                                                                         MethodDeclarationSyntax wcfMethod, string wcfClassName, IDictionary <string, string> duplicateMethodNamesMap, bool isAsmx)
        {
            var members = new List <MemberDeclarationSyntax>();

            var wcfMethodName = wcfMethod.Identifier.ValueText;
            var wcfParameters = wcfMethod.ParameterList.Parameters;

            // create method & parameter names
            var methodName = DuplicateMethodNamesGenerator.TransformMethodNameIfDuplicate(duplicateMethodNamesMap, wcfMethodName, wcfParameters);
            var parameters = ServiceCodeParametersGenerator.CreateParameters(wcfParameters);

            parameters = ServiceCodeMultipleComplexTypesGenerator.TransformMultipleComplexTypeParameters(parameters, methodName);

            // create service type for method comment
            var serviceType = ServiceNamesMapper.GetServiceTypeName(isAsmx);

            // create http verb
            var httpVerb = CreateHttpVerb(wcfMethodName, parameters);

            // create route name
            var routeUriTemplate = ServiceNamesMapper.MapToRouteUriTemplate(controllerName, methodName);

            // create return type
            var wcfReturnType = wcfMethod
                                .ReturnType
                                .NormalizeWhitespace()
                                .ToFullString();
            var returnType = ServiceCodeOutKeywordGenerator.TransformOutKeywordReturnType(wcfReturnType, wcfParameters, methodName);

            // create method block arguments
            var arguments = CreateWcfArguments(wcfParameters);

            arguments = ServiceCodeMultipleComplexTypesGenerator.TransformMultipleComplexTypeArguments(arguments, wcfParameters, methodName);

            // create method block
            var block = CreateMethodBlock(wcfClassName, wcfMethodName, arguments, returnType);

            block = ServiceCodeOutKeywordGenerator.TransformBlockWithOutKeyword(block, wcfParameters, arguments, methodName);

            // create new method
            var method = await CreateMethodDeclaration(methodName, httpVerb, routeUriTemplate, parameters, block, returnType, serviceType);

            // add nested class model if have multiple complex params
            var className    = ComplexTypeNamesMapper.MapToServiceComplexClassType(methodName);
            var complexClass = await MultipleComplexTypesGenerator.CreateClassFromComplexTypes(wcfParameters, className);

            if (complexClass != null)
            {
                complexClass = ServiceCodeOutKeywordGenerator.TransformComplexClassWithOutKeyword(complexClass, wcfParameters, wcfReturnType);

                members.Add(complexClass);
            }

            // add new method
            members.Add(method);

            return(members);
        }
예제 #3
0
        private static MethodDeclarationSyntax FindServiceGenMethod(MethodDeclarationSyntax wcfMethod,
                                                                    IEnumerable <MethodDeclarationSyntax> wcfMethods, IEnumerable <MethodDeclarationSyntax> serviceGenMethods)
        {
            var wcfMethodName = wcfMethod.Identifier.ValueText;
            var wcfParameters = wcfMethod.ParameterList.Parameters;

            // need to account for overload methods (duplicate)
            var duplicateMethodNamesMap = DuplicateMethodNamesGenerator.CreateMappings(wcfMethods);
            var serviceMethodName       = DuplicateMethodNamesGenerator.TransformMethodNameIfDuplicate(duplicateMethodNamesMap, wcfMethodName, wcfParameters);

            var serviceGenMethod = serviceGenMethods.SingleOrDefault(serviceMethod =>
            {
                var name = serviceMethod.Identifier.ValueText;

                return(name == serviceMethodName);
            });

            return(serviceGenMethod);
        }