Code Generation Tool-NCodeGenerate tutorial (3) generate code to a file. Code Generation Tool-What is NCodeGenerate? Code Generation Tool-NCodeGenerate tutorial (1) traverse all the table code in the database

Source: Internet
Author: User

NCodeGenerate can directly output the generated content to the file.

This mechanism is implemented through a class named FileRender. Here is an example.

Create a template and enter the following code in the Model:

 1 using System; 2 using System.ComponentModel; 3  4 using NCodeGenerateIDE; 5 using NCodeGenerate.DBSchema; 6 namespace DynamicCodeGenerate 7 { 8     public class myModel 9     {10       11         private System.String _Namespace = string.Empty;12         public System.String Namespace13         {14             get { return _Namespace; }15             set { _Namespace = value; }16         }17         private System.String _RemoveTablePrefix = string.Empty;18         public System.String RemoveTablePrefix19         {20             get { return _RemoveTablePrefix; }21             set { _RemoveTablePrefix = value; }22         }23         private TableSchema _SourceTable;24         [Editor(typeof(TableEditor), typeof(TableEditor))]25         public TableSchema SourceTable26         {27             get { return _SourceTable; }28             set { _SourceTable = value; }29         }30         private DatabaseSchema _DataBase;31         [Editor(typeof(DataBaseEditor), typeof(DataBaseEditor))]32         public DatabaseSchema DataBase33         {34             get { return _DataBase; }35             set { _DataBase = value; }36         }37         private System.String _OutPutDir = string.Empty;38         [Editor(typeof(FolderEditor), typeof(FolderEditor))]39         public System.String OutPutDir40         {41             get { return _OutPutDir; }42             set { _OutPutDir = value; }43         }44        45 46     }47 }

Note: The first line in the Code is,

[Editor (typeof (FolderEditor), typeof (FolderEditor)]
This Code defines the public System. String OutPutDir field of the output path. On the attribute setting interface, the "select path" dialog box is called.
After setting this feature, the following page is displayed:

 

Step 2: Enter the following code in the template:

 1 @model DynamicCodeGenerate.myModel 2 @using NCodeGenerateIDE 3  4 @{ 5  6  7     foreach(var item in Model.DataBase.Tables) 8     { 9         FileRender render = new FileRender(Request.TemplatePath+"\\demo1.cg",Model.OutPutDir + "\\" +item.Name + ".cs");10          var model = render.Model;11             12                13                 model.Namespace = "myproc";14                 model.RemoveTablePrefix = "";15                 model.SourceTable = item;16                 17                 18            19          render.Render();20          21         <text>TableName=@item.Name</text>22     }23 }

 

The Code FileRender render = new FileRender (Request. templatePath + "\ demo1.cg", Model. outPutDir + "\" + item. name + ". cs ") is to generate a FileRender object, there are two parameters, the first is the template file path, the second output file path. In the above Code, Request is a Request object and is an attribute inherent in the template. It has a field TemplatePath representing the path of the current template.

Assign a value to the Model after generating FileRender. Code var model = render. Model; obtain the Model object of FileRender, and assign values to the attributes of the Model one by one. What are the attributes of a specific Model.

You need to see the Model definition in the template file corresponding to Request. TemplatePath + "\ demo1.cg.

The Model Class Name of the current template should be emphasized here. The namespace cannot conflict with the class name and namespace of the Model called template. Otherwise, the binding error will be reported during runtime.

Finally, call render. Render (); input the generated result to the file.

 

Appendix: NCodeGenerate adds the NCodeGenerate. DBSchema document, which is connected in the following document.

Attached:

NCodeGenerate.zip

Document: Document

NCodeGenerate series of articles:

I. What is NCodeGenerate? Ii. Code Generation Tool-NCodeGenerate tutorial (1) traverse all tables in the database III. Code Generation Tool-NCodeGenerate tutorial (2) code Generation Tool-NCodeGenerate tutorial (3) generate code to a file. 5. Code Generation Tool-NCodeGenerate tutorial (4) CodeSmith template conversion 6. Code Generation Tool-NCodeGenerate tutorial (5) support for multiple databases 7. Code Generation Tool-NCodeGenerate tutorial (6) NTrace output for debugging functions 8. Code Generation Tool-NCodeGenerate tutorial (7) unveil the mystery of the debugging function 9. Code Generation Tool-NCodeGenerate tutorial (8) unveil the secrets of the Razor template engine 10. Code Generation Tool-NCodeGenerate tutorial (9) Data Type ing Map function

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.