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);
        }
Ejemplo n.º 2
0
        private static string FindServiceGenCode(IDictionary <string, string> serviceGenCodes, string wcfClassName)
        {
            var controllerName = ServiceNamesMapper.MapToControllerName(wcfClassName);
            var serviceGenCode = serviceGenCodes.ContainsKey(controllerName)
                ? serviceGenCodes[controllerName]
                : "";

            return(serviceGenCode);
        }
        private static async Task <string> GetControllerNamespace(string code)
        {
            // get api controllers' namespace

            var wcfNamespace = await CodeParser.ParseNamespace(code);

            var controllerNamespace = new[] { code, wcfNamespace }.Any(string.IsNullOrEmpty)
                ? ServiceNamesMapper.GetDefaultControllerNamespace()
                : ServiceNamesMapper.MapToControllersNamespace(wcfNamespace);

            return(controllerNamespace);
        }
        public static Dictionary <string, Task <WcfServiceClassInfo> > Create(string wcfServiceDir)
        {
            var fileCodes = SourceCodeAccess.GetServiceFilesCode(wcfServiceDir);

            var wcfClasses = fileCodes
                             .Select(async fileCode =>
            {
                var code         = (await fileCode).code;
                var wcfClassName = await CodeParser.ParseClassName(code);

                var info = new WcfServiceClassInfo
                {
                    ControllerName       = ServiceNamesMapper.MapToControllerName(wcfClassName),
                    ControllerNamespace  = await GetControllerNamespace(code),
                    WcfServiceSourceCode = code,
                    WcfMethods           = await CodeParser.ParseMethodNames(code),
                    IsAsmx = (await fileCode).isAsmx,
                };

                var pair = new
                {
                    wcfClassName,
                    info,
                };

                return(pair);
            })
                             .ToDictionary(pair =>
            {
                // avoid using task object as key for dictionary so wait to complete
                var className = pair.Result.wcfClassName;

                return(className);
            }, async pair =>
            {
                var info = (await pair).info;

                return(info);
            });

            return(wcfClasses);
        }