Refer to the key points for compiling the codesmith template compiled by nettiers.

Source: Internet
Author: User

// Basic template knowledge

// The first sentence required by any template is used to specify the template editing language and target language:
<% @ Codetemplate Language = "C #" targetlanguage = "T-SQL" Description = "generates a update stored procedure." %>

// The parameters that need to be introduced from outside the template are written next.
<% @ Property name = "sourcedatabase" type = "schemaexplorer. databaseschema" Category = "context" Description = "Database" %>

// Codesmith functions and methods are used in the template. The corresponding package needs to be introduced, which is generally
<% @ Assembly name = "schemaexplorer" %>
<% @ Import namespace = "schemaexplorer" %>

I understand that assembly is the introduction of DLL, and import is the introduction of namespaces in DLL. Some of these DLL files are self-contained by codesmith and can be introduced and used for the DLL provided by vs.net.

// All codesmith functions are defined in the script tag, including variables
<SCRIPT runat = "template">
Private string _ outputdirectory = string. empty;

</SCRIPT>

The call is enclosed by <%>. Generally, there is a main function to execute the function of the entire template.
<%
This. Go ();
%>

Insert the generated variables to any place in the template that requires dynamic generation. You can directly write the variables in ASP. For example:
Namespace <% = dalnamespace %>. sqlclient

 

// Functions and usage of General templates

/// <Summary>
/// Copy the specified object
/// </Summary>
Public void safecopyfile (string path, string destination)
{
Fileinfo file1 = new fileinfo (PATH );
File1.copyto (destination, true );
}

/// <Summary>
/// Create a specified directory
/// </Summary>
Public void safecreatedirectory (string path)
{
If (! Directory. exists (PATH ))
{
Directory. createdirectory (PATH );
}
}

/// <Summary>
/// Generate the specified file based on the specified Template
/// </Summary>
Public void rendertofile (string templatename, string path, bool overwrite)
{
This. _ currentfilename = path;
This. gettemplate (templatename). rendertofile (path, overwrite );
This. _ counter ++;
}

/// <Summary>
/// Open the file directory. The [Editor] label indicates that the specified editor is called; category indicates the directory to which the parameter belongs; descript indicates the description of the parameter; defaultvalue indicates the default value.
Codetemplateproperty indicates whether the parameter is optional or required. codetemplatepropertyoption. Optional is optional, and codetemplatepropertyoption. required is required.

Required
/// </Summary>
Private string _ outputdirectory = string. empty;
[Editor (typeof (system. Windows. Forms. Design. foldernameeditor), typeof (system. Drawing. Design. uitypeeditor)]
[Codetemplateproperty (codetemplatepropertyoption. Optional)]
[Category ("general")]
[Description ("the directory to output the results to.")]
[Defaultvalue ("")]
Public String outputdirectory
{
Get
{
If (_ outputdirectory. Length = 0)
{
Return @ "C: \ nettiers \ output ");
}
Else
{
Return _ outputdirectory;
}
}
Set
{
If (value. endswith ("\") value = value. substring (0, value. Length-1 );
_ Outputdirectory = value;
}
}

// Obtain the path of the currently opened Template
This. codetemplateinfo. directoryname

// For a template, the currently opened template can be accessed using codetemplateinfo. For other subtemplates, you must first load the template according to the file name and path, edit the template, assign parameters, and generate a file.

// Set the template path
Private string [] _ templatesfilenames = new string [] {
"Vsnet2003.project. CST ",
"Vsnet2005.project. CST ",
"Vsnet2003.solution. CST ",
"Vsnet2005.solution. CST ",
"ASP. NET \ 2.0 \ entty_aspx_resx.cst"
};

// Set the hashtable saved in the edited sub-template. In hashtable, the key is the file name, so the full set of templates cannot have duplicate file names.
// Compile and load all them in a collection
Private system. Collections. hashtable _ codetemplates = new system. Collections. hashtable ();

// Load the Template
// Load all the templates and put them into an hashtable
Public void loadtemplates ()
{
Foreach (string _ templatesfilename in _ templatesfilenames)
{
String key = system. Io. Path. getfilename (_ templatesfilename );

If (_ codetemplates. Contains (key ))
{
Continue;
}

_ Codetemplates. Add (key, this. compiletemplate (this. codetemplateinfo. directoryname + _ templatesfilename ));

// Set the properties that all the commonsqlcode inherited templates shold set
// Todo: Use reflection to check that the templates inherits from commonsql
Try
{
(Codesmith. Engine. codetemplate) _ codetemplates [Key]). setproperty ("entityformat", entityformat );
(Codesmith. Engine. codetemplate) _ codetemplates [Key]). setproperty ("collectionformat ",

Collectionformat );
(Codesmith. Engine. codetemplate) _ codetemplates [Key]). setproperty ("providerformat", providerformat );
(Codesmith. Engine. codetemplate) _ codetemplates [Key]). setproperty ("interfaceformat", interfaceformat );
(Codesmith. Engine. codetemplate) _ codetemplates [Key]). setproperty ("baseclassformat", baseclassformat );
(Codesmith. Engine. codetemplate) _ codetemplates [Key]). setproperty ("enumformat", enumformat );
(Codesmith. Engine. codetemplate) _ codetemplates [Key]). setproperty ("manytomanyformat ",

Manytomanyformat );
(Codesmith. Engine. codetemplate) _ codetemplates [Key]). setproperty ("aliasfilepath", aliasfilepath );
(Codesmith. Engine. codetemplate) _ codetemplates [Key]). setproperty ("strippedtableprefixes ",

Strippedtableprefixes );
}
Catch (exception ){}
}
}

// The template must be compiled during loading.
Public codetemplate compiletemplate (string templatename)
{
This. _ currentfilename = templatename;

Codetemplatecompiler compiler = new codetemplatecompiler (templatename );
Compiler. Compile ();

If (compiler. errors. Count = 0)
{
Return compiler. createinstance ();
}
Else
{
For (INT I = 0; I <compiler. errors. Count; I ++)
{
Response. writeline (compiler. errors [I]. tostring ());
}
Return NULL;
}
}

// Obtain the Template
Public codetemplate gettemplate (string templatetype)
{
Return (codesmith. Engine. codetemplate) _ codetemplates [templatetype];
}

// Assign a value to the template
This. gettemplate ("entityproviderbase. CST"). setproperty ("sourcetable", sourcetable );

 

// Database attributes
<% @ Property name = "sourcedatabase" type = "schemaexplorer. databaseschema" Optional = "false" Category = "datasource"

Description = "database that the stored procedures shoshould be based on." %>

// Traverse all data tables in the database
Private tableschemacollection _ sourcetables;
Private viewschemacollection _ sourceviews;
For (INT I = 0; I <sourcedatabase. Tables. Count; I ++)
{
_ Sourcetables. Add (sourcedatabase. Tables [I]);
}

// Traverse all views in the database
For (INT I = 0; I <sourcedatabase. Views. Count; I ++)
{
_ Sourceviews. Add (sourcedatabase. views [I]);
}

// Obtain the table name
Sourcetables [I]. Name

// Traverse the table structure
For (INT I = 0; I <sourcetable. Columns. Count; I ++ ){
Response. Write ("\" "+ sourcetable. Columns [I]. Name + "\"");}

// Specify which tables to open or which tables to open
[Category ("datasource")]
[Description ("the tables to generate.")]
[Codetemplateproperty (codetemplatepropertyoption. Optional)]
Public tableschemacollection sourcetables
{
Get
{
If (this. _ sourcetables! = NULL & this. _ sourcetables. Count> 0)
Return this. _ sourcetables;
Else
Return NULL;
}
Set
{
This. _ sourcetables = value;
}
}

// I wrote my own code for Traversing folders and copying all files in folders. There is no use case in nettiers:
// Copy the Directory
Public void copydirectory (string path)
{
Safecreatedirectory (outputdirectory + path );

Directoryinfo dir = new directoryinfo (this. codetemplateinfo. directoryname + path );
Fileinfo [] filelist = dir. getfiles ();
Foreach (fileinfo sourcefile in filelist)
{
Sourcefile. copyto (outputdirectory + path + "\" + sourcefile. Name, true );
Response. writeline ("copy file:" + sourcefile. Name );
}
}

// Traverse the folder: First open the Directory and then load the files under the directory. This operation is completely written in C # Code. For more functions, see msdn
Directoryinfo dir = new directoryinfo (this. codetemplateinfo. directoryname + path );
Fileinfo [] filelist = dir. getfiles ();
Foreach (fileinfo sourcefile in filelist)
{
Sourcefile. copyto (outputdirectory + path + "\" + sourcefile. Name, true );
Response. writeline ("copy file:" + sourcefile. Name );

}

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.