Codesmith generates database view entity classes

Source: Internet
Author: User

Codesmith generates database view entity classes

 

Open start -- all programs --- codesmith professional 5.0.1.4983 -- codesmith Explorer

The name of the new connection to the database to be operated is customized As DB

Open start -- all programs --- codesmith professional 5.0.1.4983 -- codesmith Studio

Create a new template file businessobjctforview. CST (the template file can be built and run)

Create a new file viewutility. CS (the database field is mapped to the vs2005 data type, and the namespace is consistent with the namespace above)

Copy the following code and click tools/build/run to generate the object class file of the data table.

Note that the template businessobjctforview. name of the data table generated by CST. the namespace of the CS "object class file is common. model and called (viewutility. CS) class file namespace common. whether data exists. it must exist in the vs2005 project or be changed as needed.

 

The businessobjctforview. CST code is as follows:

<% @ Codetemplate Language = "C #" responseencoding = "UTF-8" targetlanguage = "T-SQL" Description = "Stored Procedure for generating Update views." %>
<% @ Assembly name = "schemaexplorer" %>
<% @ Import namespace = "schemaexplorer" %>
<% @ Property name = "sourcetable" type = "schemaexplorer. viewschema" Category = "context" Description = "view name of Stored Procedure operation." %>
<% @ Assembly src = "viewutility. cs" %>
<% @ Import namespace = "common. Data" %>
<SCRIPT runat = "template">
Viewutility rule = new viewutility ();
</SCRIPT>
Using system;
Using system. collections;
Using system. Collections. Generic;
Using system. Data;

Namespace common. Model
{
/// <Summary>
/// Read the view '<% = sourcetable. name %>' corresponding to the database'
/// </Summary>
[Serializable]
Public class <% = sourcetable. name %>
{
# Region private member
<% For (INT I = 0; I <sourcetable. Columns. Count; I ++) {%>
Private <% = rule. getcsharpvariabletype (sourcetable. Columns [I]) %> _ <% = sourcetable. Columns [I]. name %>;
<% }%>
# Endregion

# Region Constructor
Public <% = sourcetable. name %> (){}

Public <% = sourcetable. name %> (
<% For (INT I = 0; I <sourcetable. Columns. Count-1; I ++) {%>
<% = Rule. getcsharpvariabletype (sourcetable. Columns [I]) %> <% = sourcetable. Columns [I]. name %>,
<% }%>
<% = Rule. getcsharpvariabletype (sourcetable. columns [sourcetable. columns. count-1]) %> <% = sourcetable. columns [sourcetable. columns. count-1]. name %>
)
{
<% For (INT I = 0; I <sourcetable. Columns. Count; I ++) {%>
_ <% = Sourcetable. Columns [I]. name % >=< % = sourcetable. Columns [I]. name %>;
<% }%>
}
# Endregion

# Region public properties
<% For (INT I = 0; I <sourcetable. Columns. Count; I ++) {%>
Public <% = rule. getcsharpvariabletype (sourcetable. Columns [I]) %> <% = rule. getcamelcasename (sourcetable. Columns [I]. Name) %>
{
Get {return _ <% = sourcetable. Columns [I]. name %> ;}
Set {_ <% = sourcetable. Columns [I]. name %> = value ;}
}
<% }%>
# Endregion
}
}

The viewutility. CS code is as follows:

Using system;
Using system. text;
Using codesmith. engine;
Using schemaexplorer;
Using system. componentmodel;
Using system. Data;

Namespace common. Data
{
//// <Summary>
/// Codesmith generates an entity script for the SQL Server View
/// </Summary>
Public class viewutility
{

// Get columns info by tablename
Public viewcolumnschemacollection getcolumncollectionbytable (viewschema table)
{
Viewcolumnschemacollection columns = new viewcolumnschemacollection (table. columns );
Return columns;
}

// Get camelcase name, such as customer,
Public String getcamelcasename (string Str)
{
Return Str. substring (0, 1). toupper () + Str. substring (1 );
}

// Get, user, private const string user_field = "user"
Public String getmemberconstantdeclarationstatement (columnschema column)
{
Return getmemberconstantdeclarationstatement ("Public const string", column );
}

// Such as public const string user_table = "user"
Public String gettableconstantdeclarationstatement (viewschema table)
{
Return getmemberconstantdeclarationstatement ("Public const string", table );
}
// Suck as user_table
Public String getupperstatement (viewschema table)
{
Return table. Name. toupper () + "_ table ";
}
// Suck as user_field
Public String getupperstatement (columnschema column)
{
Return column. Name. toupper () + "_ field ";
}

// Such as user_table = "user"
Public String getmemberconstantdeclarationstatement (string protectionlevel, viewschema table)
{
Return protectionlevel + getupperstatement (table) + "=" + getcamelcasename (table. Name) + "";
}

// Such as userid_field = "userid"
Public String getmemberconstantdeclarationstatement (string protectionlevel, columnschema column)
{
Return protectionlevel + getupperstatement (column) + "=" + getcamelcasename (column. Name) + "";
}

Public String getcsharpvariabletype (viewcolumnschema column)
{
Switch (column. datatype)
{
Case dbtype. ansistring: Return "string ";
Case dbtype. ansistringfixedlength: Return "string ";
Case dbtype. Binary: Return "byte []";
Case dbtype. boolean: Return "bool ";
Case dbtype. byte: Return "int ";
Case dbtype. Currency: Return "decimal ";
Case dbtype. Date: Return "datatime ";
Case dbtype. datetime: 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 "timespan ";
Case dbtype. uint16: Return "ushort ";
Case dbtype. uint32: Return "uint ";
Case dbtype. uint64: Return "ulong ";
Case dbtype. varnumeric: Return "decimal ";
}

Return NULL;
}

Public String getcsharpbasetype (viewcolumnschema column)
{
Switch (column. datatype)
{
Case dbtype. ansistring: Return "system. String ";
Case dbtype. ansistringfixedlength: Return "system. String ";
Case dbtype. Binary: Return "system. byte []";
Case dbtype. boolean: Return "system. boolean ";
Case dbtype. byte: Return "system. int32 ";
Case dbtype. Currency: Return "system. Decimal ";
Case dbtype. Date: Return "system. datatime ";
Case dbtype. datetime: Return "system. datatime ";
Case dbtype. Decimal: Return "system. Decimal ";
Case dbtype. Double: Return "system. Double ";
Case dbtype. guid: Return "system. guid ";
Case dbtype. int16: Return "system. int16 ";
Case dbtype. int32: Return "system. int32 ";
Case dbtype. int64: Return "system. int64 ";
Case dbtype. Object: Return "system. Object ";
Case dbtype. sbyte: Return "system. sbyte ";
Case dbtype. Single: Return "system. Single ";
Case dbtype. String: Return "system. String ";
Case dbtype. stringfixedlength: Return "system. String ";
Case dbtype. Time: Return "system. timespan ";
Case dbtype. uint16: Return "system. uint16 ";
Case dbtype. uint32: Return "system. uint32 ";
Case dbtype. uint64: Return "system. uint64 ";
Case dbtype. varnumeric: Return "system. Decimal ";
}
Return NULL;
}
}
}

 

 

 

 

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.