Codesmith creates an ADO. Net custom template (2)

Source: Internet
Author: User

 

 

 

Codesmith creates an ADO. Net custom template (2)

 

Article 1: codesmith creates an ADO. Net custom template (1)

 

 

Create the second C # template: step2_model.cst (entity class template)

 

Code:

<% @ Codetemplate Language = "C #" targetlanguage = "C #" responseencoding = "UTF-8" Description = "entity class" %>
<% @ Property name = "namespace" type = "system. String" default = "model" Category = "property" Description = "namespace" %>
<% @ Property name = "author" type = "system. String" default = "Wilson" Category = "property" Description = "author name" %>
<% @ Property name = "sourcetable" type = "schemaexplorer. tableschema" Optional = "true" Category = "DB" Description = "Table ing file" %>
<% @ Assembly name = "schemaexplorer" %>
<% @ Assembly name = "system. Data" %>
<% @ Import namespace = "schemaexplorer" %>
<% @ Import namespace = "system. Data" %>

// --------------------- The above will not be explained. For details, see codesmith create a custom ADO. net template (1 )--------------------------

<SCRIPT runat = "template"> // you can write custom methods in this tag.
/// <Summary>
/// Convert the database type to C #
/// </Summary>
Public String datatype2csharptype (system. Data. dbtype) // If any omission exists, you can manually add
{
Switch (dbtype)
{
Case dbtype. ansistring:
Return "string ";
Case dbtype. ansistringfixedlength:
Return "string ";
Case dbtype. Binary:
Return "byte []";
Case dbtype. boolean:
Return "bool ";
Case dbtype. byte:
Return "Byte ";
Case dbtype. Currency:
Return "decimal ";
Case dbtype. Date:
Return "datetime ";
Case dbtype. datetime:
Return "datetime ";
Case dbtype. datetime2:
Return "datetime ";
Case dbtype. datetimeoffset:
Return "datetime ";
Case dbtype. Decimal:
Return "decimal ";
Case dbtype. Double:
Return "double ";
Case dbtype. guid:
Return "guid ";
Case dbtype. int16:
Return "short ";
Case dbtype. int32:
Return "int ";
Case dbtype. int64:
Return "long ";
Case dbtype. Object:
Return "object ";
Case dbtype. sbyte:
Return "sbyte ";
Case dbtype. Single:
Return "float ";
Case dbtype. String:
Return "string ";
Case dbtype. stringfixedlength:
Return "string ";
Case dbtype. Time:
Return "datetime ";
Case dbtype. uint16:
Return "ushort ";
Case dbtype. uint32:
Return "uint ";
Case dbtype. uint64:
Return "ulong ";
Case dbtype. varnumeric:
Return "decimal ";
Case dbtype. xml:
Return "string ";
Default:
Return "object ";
}
}
</SCRIPT>

Using system;
Using system. collections;
Using system. Collections. Generic;
Using system. text;

Namespace <% = namespace %>
{
/// <Summary>
/// Function: entity class (<% = sourcetable. Description %>) // description in your database
/// Creator: <% = author %> // The custom attribute mentioned in the previous article.
/// Creation date: <% = datetime. Now. tow.datestring () %> // here, and the above usage is the output variable of C #
/// </Summary>
[Serializable]
Public class <% = sourcetable. name %>
{
Public <% = sourcetable. name %> ()
{

}

# Region # <% = sourcetable. name %> entity

<% For (INT I = 0; I <sourcetable. Columns. Count; I ++) %>
<% {%>

/// <Summary>
/// <% = Sourcetable. Columns [I]. Description %>
/// </Summary>
Public <% = datatype2csharptype (sourcetable. Columns [I]. datatype) %> <% = sourcetable. Columns [I]. name %> {Get; set ;}

<% }%>

# Endregion
}
}

 

Datatype2csharptype is used to convert the database type to C # type...

 

Very simple. Several labels are already inCodesmith creates an ADO. Net custom template (1)Introduced in

<% @ Property name = "sourcetable" type = "schemaexplorer. tableschema" Optional = "true" Category = "DB" Description = "Table ing file" %>

 

In this case, you can see the sourcetable attribute in the database directory in the attribute column. A button is displayed in the attribute box. Click it to display the database selection window.

 

Alternatively, in the tool's scheme Explorer toolbar, click managerdatasource to add a database ..

 

In this way, you can select a database table

 

<% = Sourcetable. name %>: the name of the table you selected.

<% = Sourcetable. Description %>: This is the table description in your database.

Sourcetable. Columns. Count: Total number of columns in the database

Sourcetable. Columns [I]. datatype: the type of the column whose index is I.

Sourcetable. Columns [I]. Name: name of the I Column Used for Indexing

 

It is necessary to explain the following sentence

<% @ Codetemplate Language = "C #" targetlanguage = "C #" responseencoding = "UTF-8" Description = "entity class" %>

Language: This indicates the language used for writing output and functions (C #, VB, and other languages can be used.)

Targetlanguage: the language of your generated code

Responseencoding: This is the encoding output format.

You can also set attributes such as debug and SRC.

 

PS: there are smart prompts in codesmith studio. You can try other attributes on your own.

 

 

There are two more articles (data access layer and business logic layer). If you are interested, you can take a look.

There is not much content, but only implementation. If you have read the first two articles,

Writing a codesmith template should be okay,

 

Related documents:

 

Codesmith creates an ADO. Net custom template (1)

 

Codesmith creates an ADO. Net custom template (3)

 

Codesmith creates an ADO. Net custom template (4) PS: Article 4 prompts codesmith to directly generate folders and files. If you need to expand

 

 

 

Source code download: http://download.csdn.net/source/3535328

Source code download two: http://files.cnblogs.com/zhongweiv/AdoTemp.rar

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.