Generates the CodeDOM for data contracts.
Inheritance: ICodeGenerator
コード例 #1
0
        /// <summary>
        /// Executes the code generation workflow.
        /// </summary>        
        public CodeWriterOutput GenerateCode(CodeGenerationOptions options)
        {
            // Step 1 - Parse the code generation options and create the code provider.
            codeGenerationOptions = CodeGenerationOptionsParser.ParseCodeGenerationOptions(options);

			CreateCodeProvider();

        	ExtendedCodeDomTree extendedCodeDomTree;

            MetadataSet metadataSet = null;

			if (options.GenerateDataContracts)
			{
				// Step 2 - Build the set of XML schemas.
				XmlSchemas schemas = MetadataFactory.GetXmlSchemas(codeGenerationOptions.MetadataResolverOptions);
                
				// Step 3 - Generate the data contract code and get the CodeNamespace.
				DataContractGenerator dataContractGenerator = new DataContractGenerator(schemas, codeGenerationOptions.PrimaryOptions, codeProvider);
				CodeNamespace codeNamespace = dataContractGenerator.GenerateCode();

				// Step 4 - Wrap the CodeDOM in the custom object model.
				extendedCodeDomTree = new ExtendedCodeDomTree(codeNamespace, codeGenerationOptions.WriterOptions.Language, null);
			}
			else
			{
				// Step 2 - Build the service metadata.
				metadataSet = MetadataFactory.GetMetadataSet(codeGenerationOptions.MetadataResolverOptions);

                // Step 3 - Generate the client/service code and get the CodeNamespace.
				ClientServiceGenerator clientServiceGenerator = new ClientServiceGenerator(metadataSet, codeGenerationOptions.PrimaryOptions, codeProvider);
				CodeNamespace codeNamespace = clientServiceGenerator.GenerateCode();

				// Step 4 - Wrap the CodeDOM in the custom object model.
				extendedCodeDomTree = new ExtendedCodeDomTree(codeNamespace, codeGenerationOptions.WriterOptions.Language, clientServiceGenerator.Configuration);
			}
                       
            
            // Step 5 - Apply the code decorations.
            CodeDecorators decorators = new CodeDecorators(metadataSet);
            decorators.ApplyDecorations(extendedCodeDomTree, codeGenerationOptions.CustomOptions);

            // Step 6 - Restore the original CodeDOM.
        	CodeNamespace cns = extendedCodeDomTree.UnwrapCodeDomTree();
				
            // Step 6 - Finally, write out the code to physical files.
            return CodeWriter.Write(cns, extendedCodeDomTree.Configuration, codeGenerationOptions.WriterOptions, extendedCodeDomTree.TextFiles, codeProvider);            
        }
コード例 #2
0
        /// <summary>
        /// Executes the code generation workflow.
        /// </summary>
        public CodeWriterOutput GenerateCode(CodeGenerationOptions options)
        {
            // Step 1 - Parse the code generation options and create the code provider.
            codeGenerationOptions = CodeGenerationOptionsParser.ParseCodeGenerationOptions(options);

            CreateCodeProvider();

            ExtendedCodeDomTree extendedCodeDomTree;

            if (options.GenerateDataContracts)
            {
                // Step 2 - Build the set of XML schemas.
                XmlSchemas schemas = MetadataFactory.GetXmlSchemas(codeGenerationOptions.MetadataResolverOptions);

                // Step 3 - Generate the data contract code and get the CodeNamespace.
                DataContractGenerator dataContractGenerator = new DataContractGenerator(schemas, codeGenerationOptions.PrimaryOptions, codeProvider);
                CodeNamespace         codeNamespace         = dataContractGenerator.GenerateCode();

                // Step 4 - Wrap the CodeDOM in the custom object model.
                extendedCodeDomTree = new ExtendedCodeDomTree(codeNamespace, codeGenerationOptions.WriterOptions.Language, null);
            }
            else
            {
                // Step 2 - Build the service metadata.
                MetadataSet metadataSet = MetadataFactory.GetMetadataSet(codeGenerationOptions.MetadataResolverOptions);

                // Step 3 - Generate the client/service code and get the CodeNamespace.
                ClientServiceGenerator clientServiceGenerator = new ClientServiceGenerator(metadataSet, codeGenerationOptions.PrimaryOptions, codeProvider);
                CodeNamespace          codeNamespace          = clientServiceGenerator.GenerateCode();

                // Step 4 - Wrap the CodeDOM in the custom object model.
                extendedCodeDomTree = new ExtendedCodeDomTree(codeNamespace, codeGenerationOptions.WriterOptions.Language, clientServiceGenerator.Configuration);
            }

            // Step 5 - Apply the code decorations.
            CodeDecorators decorators = new CodeDecorators();

            decorators.ApplyDecorations(extendedCodeDomTree, codeGenerationOptions.CustomOptions);

            // Step 6 - Restore the original CodeDOM.
            CodeNamespace cns = extendedCodeDomTree.UnwrapCodeDomTree();

            // Step 6 - Finally, write out the code to physical files.
            return(CodeWriter.Write(cns, extendedCodeDomTree.Configuration, codeGenerationOptions.WriterOptions, extendedCodeDomTree.TextFiles, codeProvider));
        }