How does codesmith generate objects from multiple selected tables?

Source: Internet
Author: User

1. generate an object from multiple selected tables

We will generate an object class for a table, so the entity classes for multiple tables can be generated at a time? BR>How to implement it? /Strong>
The previous Entity class generation template can only generate one table. Now we write a new template to register the original entity class template in this template. In this way, we can generate multiple objects of the original entity class template in this new template, and then call the rendering method of the original object to generate Code
For example, there is already an entity class that generates the template entitysingle. CST. Now we create a new template and register the entitysingle. CST template in the new template.
<% @ Codetemplate Language = "C #" targetlanguage = "text" src = "" inherits = "" DEBUG = "false" Description = "" %>
<% @ Property name = "TDB" type = "schemaexplorer. tableschemacollection" default = "" Optional = "false" Category = "" %>
<% @ Register name = "se" template = "entitysingle. CST" mergeproperties = "false" excludeproperties = "" %>
<% @ Assembly name = "schemaexplorer" %>
<% @ Assembly name = "system. Data" %>
<% @ Import namespace = "schemaexplorer" %>
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Collections" %>
<%
Foreach (tableschema ts in TDB)
{
Se S = new Se ();
S. tTable = ts;
S. Render (this. Response );
}
%>
<SCRIPT runat = "template">
</SCRIPT>
Description? BR><% @ Register name = "se" template = "entitysingle. CST" mergeproperties = "false" excludeproperties = "" %>
This is to register entitysingle. CST to the current template. The name is Se? BR>
<% @ Property name = "TDB" type = "schemaexplorer. tableschemacollection" default = "" Optional = "false" Category = "" %>
This statement defines the attributes of a table set so that you can select multiple tables to generate their entity classes.

Se S = new Se ();
Instantiate the se template object.
S. tTable = ts;
Assign the tTable attribute to the entitysingle. CST template instance? BR>
S. Render (this. Response );
Call the presentation method of the SE object (entitysingle. CST template instance) to generate the entity class code of the table.
Running result

2. Generate the entity class of a single table into a file
In the preceding example, multiple object classes can be generated at the same time, but all of them are displayed on the right? B style = "color: black; Background-color: # a0ffff"> in the output area, copy and paste the image into the source file. Here we mainly study how to generate a generation? B style = "color: black; Background-color: # a0ffff"> output to the specified file? BR> ��? B style = "color: black; Background-color: # ffff66"> codesmith, Codetemplate. Render Method Used to modifyCodesmith Output Event processing. You can rewrite the render method to modify the modulo? B style = "color: black; Background-color: # a0ffff"> output mode? BR> Render method? BR> Public override void render (textwriter writer)
{}
For example, in the template's default Output ? B style = "color: black; Background-color: # a0ffff"> file output function
Public override void render (textwriter tw)
{
Streamwriter fs1 = new streamwriter ("C :\\" + tTable. Name. tostring () + ". cs", true );
This. response. addtextwriter (fs1 );
Base. Render (TW );
Fs1.close ();
}

Note: Base. Render (TW) must be added here. Otherwise, the default Output ��? BR>
Filenameeditor class:
The filenameeditor class provides Codesmith In the properties panel, the open or save file dialog box is displayed. When using the dialog box, add Program Set Codesmith. Customproperties . Then you can define an attribute in the template to use filenameeditor? BR> <% @ codetemplate Language = "C #" targetlanguage = "text" src = "" inherits = "" DEBUG = "false" Description = "" %>
<% @ Assembly name ="Codesmith. Customproperties "%>
<% @ Import namespace ="Codesmith. Customproperties "%>

<SCRIPT runat = "template">
Private string _ filename;
[Editor (typeof (filenameeditor), typeof (system. drawing. design. uitypeeditor), filedialogattrigat( filedialogtype. save, Title = "Save as", filter = "C # file | (*. CS) ")]
Public String targetfilename

{
Get
{
Return _ filename;
}
Set
{
_ Filename = value;
}

}
</SCRIPT>
Filedialogattrigatclass: sets the style of the dialog box.
Filedialogtype: The operator of the dialog box? Open/close), such as: filedialogtype. Save
Filter: file type filter, for example, filter = "C # file | (*. CS )"
Title: The title of the dialog box. For example, Title = "save"
Defaultextension: default file extension? BR> checkfileexists: True-only existing files can be selected.
Checkpathexists: True-only existing paths are allowed.

 

Which of the following code will generate an object? B style = "color: black; Background-color: # a0ffff"> output to the corresponding. CS File
<% @ Codetemplate Language = "C #" targetlanguage = "text" src = "" inherits = "" DEBUG = "false" Description = "template description here." %>
<% @ Property name = "tTable" type = "schemaexplorer. tableschema "default =" "Optional =" false "Category =" "Description =" "onchanged =" "editor =" "editorbase =" "serializer =" "%>
<% @ Assembly name ="Codesmith. Customproperties "%>
<% @ Assembly name = "system. Data" %>
<% @ Assembly name = "schemaexplorer" %>
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "schemaexplorer" %>
<% @ Import namespace = "system. Io" %>
<% @ Import namespace ="Codesmith. Customproperties "%>
Using system;
<%
String classname = tTable. Name. toupper (). substring (0, 1) + tTable. Name. tolower (). substring (1, tTable. Name. Length-1 );
String entitytype = "";
String entityname = "";
%>
Public class <% = classname %> data
{
<% For (INT I = 0; I <tTable. Columns. Count; I ++) {%>
<% IF (! TTable. Columns [I]. isforeignkeymember) {%>
Private <% = createcolumntype (tTable. Columns [I]) %> _ <% = tTable. Columns [I]. Name. tolower () %>;
<%}
Else
{
Foreach (tablekeyschema TKs in tTable. foreignkeys)
{
Foreach (membercolumnschema MCs in TKs. foreignkeymembercolumns)
{
If (MCS. Name = tTable. Columns [I]. Name)
{
Entityname = TKs. primarykeytable. name. toupper (). substring (0, 1) + TKs. primarykeytable. name. tolower (). substring (1, TKs. primarykeytable. name. length-1 );
Entitytype = entityname + "data ";
Break;
}
}
}
%>
Private <% = entitytype % >_< % = entityname. tolower () %>;
<% }%>
<% IF (! TTable. Columns [I]. isforeignkeymember) {%>
Public <% = createcolumntype (tTable. columns [I]) %> <% = tTable. columns [I]. name. toupper (). substring (0, 1) + tTable. columns [I]. name. tolower (). substring (1, tTable. columns [I]. name. length-1) %>
{
Get
{
Return _ <% = tTable. Columns [I]. Name. tolower () %>;
}
Set
{
_ <% = TTable. Columns [I]. Name. tolower () %> = value;
}
}
<%} Else {%>
Public <% = entitytype %> <% = entityname. toupper (). substring (0, 1) + entityname. tolower (). substring (1, entityname. Length-1) %>
{
Get
{
Return _ <% = entityname. tolower () %>;
}
Set
{
_ <% = Entityname. tolower () %> = value;
}
}
<% }%>
<% }%>
}
<SCRIPT runat = "template">
Private string _ SaveFile;
[Editor (typeof (filenameeditor), typeof (system. drawing. design. uitypeeditor), filedialogattrigat( filedialogtype. save, Title = "Save as", filter = "C # file | (*. CS) ")]
Public String SaveFile
{
Get
{
Return _ SaveFile;
}
Set
{
_ SaveFile = value;
}
}
Public override void render (textwriter tw)
{
Streamwriter fs1 = new streamwriter (_ SaveFile, true );
This. response. addtextwriter (fs1 );
Base. Render (TW );
Fs1.close ();
}
Private string createcolumntype (columnschema CS)
{
Switch (CS. datatype)
{
Case dbtype. ansistring:
Case dbtype. String:
Return "string ";
Case dbtype. Binary:
Return "byte []";
Case dbtype. boolean:
Return "bool ";
Case dbtype. Date:
Case dbtype. datetime:
Case dbtype. Time:
Return "datetime ";
Case dbtype. int16:
Case dbtype. int32:
Case dbtype. int64:
Case dbtype. uint16:
Case dbtype. uint32:
Case dbtype. uint64:
Return "int ";
Default:
Return "object ";
}
}
</SCRIPT>
3. Generate entity classes of multiple tables into files
To generate entity classes for multiple tables to a file, we actually combine the above two methods? BR>Why? /Strong>
1. Create a template A that can generate an object class and set a tableschema attribute and file path? BR> 2. Create another template B, where register and reference the above Entity class model? BR> 3. Add a tableschemacollection attribute to template B to select which tables to generate entities? BR> 4. Add an attribute to template B to select the directory in which the generated object class files are stored.
5. generate an instance of template A in template B and assign the two attributes of template? BR> 6. Use the render method to implementOutput
Foldernameeditor class: Path Selection dialog box? BR><SCRIPT runat = "template">
Private string _ filename;
[Editor (typeof (system. Windows. Forms. Design. filenameeditor), typeof (system. Drawing. Design. uitypeeditor)]
Public String targetfilename
{
Get
{
Return _ filename;
}
Set
{
_ Filename = value;
}
}

Code? BR> <% @ codetemplate Language = "C #" targetlanguage = "text" src = "" inherits = "" DEBUG = "false" Description = "template description here." %>
<% @ Property name = "TB" type = "schemaexplorer. tableschemacollection "default =" "Optional =" true "Category =" strings "Description =" This is a sample string property. "%>
<% @ Register name = "SGE" template = "entitysingle. CST" mergeproperties = "false" excludeproperties = "" %>
<% @ Assembly name ="Codesmith. Customproperties "%>

<% @ Assembly name = "schemaexplorer" %>
<% @ Assembly name = "system. Data" %>
<% @ Import namespace = "schemaexplorer" %>
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Io" %>
<% @ Import namespace = "system. Windows. Forms. Design" %>
<% @ Import namespace ="Codesmith. Customproperties "%>
<%
Foreach (tableschema ts in TB)
{
Sge s = new SGE ();
S. tTable = ts;
S. SaveFile = This. generatepath +"
\ "+ S. tTable. Name. tostring () +". CS ";
S. Render (this. Response );
}

%>
<SCRIPT runat = "template">
Private string _ path;
[Editor (typeof (system. Windows. Forms. Design. foldernameeditor), typeof (system. Drawing. Design. uitypeeditor)]
Public String generatepath
{
Get
{
Return _ path;
}
Set
{
_ Path = value;
}
}
</SCRIPT>

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.