Several code generation solutions integrated with VS [blog Summary (8 in total)]

Source: Internet
Author: User
I wrote a lot of articles about code generation a while ago, and introduced some solutions to automatically generate code through VS, such as CodeDOM, T4, and BuildProvider of ASP. NET. Now we will make a summary of them for the readers to make a reference. [Article 1st]Use CodeDOM to define the structure of the generated code

I don't know if you are familiar with CodeDOM's code generation mechanism, but you can be sure that if you have used Visual Studio, You should have experienced it to facilitate programming. You can list three typical code generation scenarios: when creating a strong DataSet, VS automatically generates the corresponding C # Or VB according to the Schema. NET code. When we edit the Resource file, the corresponding background code is also automatically generated. When we call Web Service or WCF Service by adding a Web Reference, VS automatically generates the Service proxy code and corresponding configuration. In general, dynamic code generation tools integrated with VS allow us to program in a "strong type" manner to provide efficiency and reduce the chance of errors.

CodeDOM provides types that represent many common source code element types. You can design a program that generates the source code model and use the CodeDOM element to form an object graph. This object graph contains C # Or VB. NET Code contains the basic elements: namespace, type, type members (methods, attributes, constructors, events, etc.), and includes the specific Statement (Statement) of method implementation ). That is to say, its structure is a reflection of a specific. vb or. cs file code. I will not introduce the CodeDOM architecture here ...... [Read the full text]

[Article 2nd]Use Visual Studio's Custom Tool to define code generators

In the previous article, we have defined the code structure to be generated in the form of CodeDOM, and this CodeDOM object is CodeCompileUnit. CodeCompileUnit itself is irrelevant to the programming language. We can use CodeDomProvider based on a certain programming language (VB. NET or C #) to convert the CodeCompileUnit object into specific code text. In order for VS to automatically generate code for us, we need to write the corresponding code generator in the form of Custom Tool. The code generator we compiled finally provides external services through COM components, so it involves the registration of COM components. To achieve integration with VS, the corresponding registry settings are also involved ...... [Read the full text]

[Article 3rd]Code Generation Mechanism different from CodeDOM-T4

The previous two articles show you how to use CodeDOM + Custom Tool to generate code automatically. In fact, the most commonly used code generation was not CodeDOM, but T4. It was a more powerful and widely used code generation technology. T4 is short for "Text Template Transformation Toolkit" (4 TB. T4 is directly included in VS2008 and VS2010. It is a text file conversion toolkit. The core of T4 is a conversion Engine Based on "text templates". We can use it to generate all types of text files. For example, common code file types include C # and VB.. NET, T-SQL, XML, and even configuration files.

For code generation through T4, all we need to do is based on the Conversion Source (such as data tables and XML) (because the example is simple, the HelloWord template has no input source) and the target text (such as the eventually required C # Or T-SQL code) define the corresponding template. The T4 template is equivalent to the XSLT used in the XML conversion process.

The definition of T4 template is very simple. The content of the template includes two forms: static form and dynamic. The former is the text directly written in the template as the original output, and the latter is the code written in a certain language, and the T4 engine will dynamically execute them. This is the same as ASP. NET page is very similar: HTML is static, with C # Or VB. NET code. [Read the full text]

[Article 4th]Use the T4 template to generate code for a single file

In the previous article, I introduced the structure and syntax of the T4 template and the working principle of the T4 engine, in addition, a T4 template is compiled to convert an XML into C # code. To provide readers with a better understanding of T4, we can use T4 to do more practical things-SQL Generator. Here, we can use SQL Generator to automatically generate a stored procedure for inserting, modifying, and deleting a data table ...... [Read the full text]

[Article 5th]Use the T4 template to generate code for multiple files

In the previous article, we used the T4 template to successfully generate the stored procedure required for adding, modifying, and deleting data tables. But this is a solution based on a single file, that is, we must create a template for each generated stored procedure. If we provide a multi-file-based code generation method, it will bring great convenience to programmers. Using the open-source ToolBox T4 ToolBox, the implementation of multi-file SQL Generator becomes very simple. [Read the full text]

[Article 6th]Five solutions to reference the assembly of the T4 Template

If your T4 template needs to call a custom type and the type is defined in a non-system assembly, you need to use T4 <# @ assembly... #> The command references the assembly. In the VS 2008 environment, you only need to add the corresponding assembly reference for the current project and use <@ assembly... #> The command name attribute specifies the file name of the Assembly (Dll. However, this method does not work for VS 2010 because the T4 engine does not locate the target Assembly from the project reference list when parsing and compiling the template. This article introduces five different assembly reference methods in VS2010 ...... [Read the full text]

[Article 7th]Two unavoidable topics of coding T4 templates: "Assembly Locking" & "Debug"

To improve performance, the T4 engine will always reuse the same AppDomain when performing Template Transformation based on code generation. Because the AppDomain will not be automatically uninstalled, this will cause the AppDomain to always lock all the Assembly loaded by it. If we need to release the Assembly, we have to restart. However, for the development and debugging phase of the T4 template, it is unacceptable to release the Assembly by restarting VS to ensure that our project can be compiled successfully. So, is there a solution that can ensure that the T4 engine can perform normal template conversion, and prevent it from forcibly locking the referenced assembly?

VS and some T4 editors provide basic smart sensing support, but most of them are equivalent to writing plain text scripts. Therefore, for some complicated template conversion logic, we need to find some unavoidable problems through Debug ...... [Read the full text]

[Article 8th]Provides code generation for ASP. NET through custom BuildProvider

For ASP. NET application developers, you may not know what is BuildProvider, but you almost never use the code generation mechanism it brings. When you create a. aspx file, why does it automatically create the corresponding source code? When you add a button in XML format on the. aspx page, why does the source code automatically add an attribute with the same name. In fact, ASP. NET converts the content of the. aspx file to the corresponding source code through a special BuildProvider. This special. aspx file is PageBuildProvider. Based on different file types, ASP. NET uses different BuildProvider for source code generation. For example, UserControlBuildProvider and MasterPageBuildProvider implement source code generation based on the user control file (. ascx) and the master page (. master), respectively ...... [Read the full text]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.