Codesmith ADO. net template (1)
(This article is an entry level. Please forgive me! Haha)
Start creating your own ADO. net template
This article uses codesmith professional 5.0.1.4983 to write
First, open codesmith Studio
Codesmith has two EXE files: codesmith studio.exe and codesmith policer.exe
Codesmith studio.exe: used to create a custom template
Codesmith assumer.exe: used to import a template and generate code
1. Create a folder: adotemp (name customization)
2. Click Create a template folder cut in template explorer of the tool and select the created folder.
3. Right-click the folder and choose new ----> C # template.
4. Official writing. First, let's get to know several tags necessary for codesmith to write templates.
Let's take a look at referencing a namespace.
<% @ Assembly name = "schemaexplorer" %>
<% @ Import namespace = "schemaexplorer" %>
// Reference the application set above. If you want to use a database table, this schemaexplorer is required
Assemby: Assembly, name: Assembly name
Import: This is easy to understand and everyone is familiar with. namespace: The namespace to be imported.
Let's take a look at the writing of custom attributes.
<% @ 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" %>
Property has nothing to explain.
Name: indicates the name.
Type: the type of this attribute.
Default: the default value of this attribute.
Categroy: group name, which is shown in the attribute column below
Description: Description
If you have written the above custom attributes, you can see them in the property bar of the development tool.
The previous figure makes it easier to understand
<%= flag %>
This form should be familiar with output variables
<%> Write some operation operations: for example, for loop: <% for (INT I = 0; I <flag; I ++) {%> I <%} %>
5. Create a C # template trainer and put the connection string in a tool class. The Template Name Is step1_utility.cst.
<% @ Codetemplate Language = "C #" targetlanguage = "C #" responseencoding = "UTF-8" Description = "entity class" %>
<% @ Property name = "namespace" type = "system. String" default = "utility" Category = "property" Description = "namespace" %>
<% @ Property name = "author" type = "system. String" default = "Wilson" Category = "property" Description = "author name" %>
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Namespace <% = namespace %>
{
/// <Summary>
/// Connection string
/// Created by: <% = author %>
/// Creation Time: <% = datetime. Now. tow.datestring () %>
/// </Summary>
Public class webconn
{
# Region # connection string
/// <Summary>
/// Function: connection string
/// Created by: <% = author %>
/// Creation Time: <% = datetime. Now. tow.datestring () %>
/// </Summary>
Public static readonly string connstring = system. configuration. configurationmanager. connectionstrings ["Wilson"]. connectionstring;
# Endregion
}
Enter the namespace and author attributes in the Properties window.
For F5 generation, the generated code is as follows. A simple template is complete.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Namespace Utility
{
/// <Summary>
/// Connection string
/// Created by Wilson
/// Creation Time: 2011/8/20
/// </Summary>
Public class webconn
{
# Region # connection string
/// <Summary>
/// Function: connection string
/// Created by Wilson
/// Creation Time: 2011/8/20
/// </Summary>
Public static readonly string connstring = system. configuration. configurationmanager. connectionstrings ["Wilson"]. connectionstring;
# Endregion
}
}
This simple codesmith ADO. net template consists of four steps: public class, entity class, data entity class, and business logic class.
Part II:
Codesmith creates an ADO. Net custom template (2)
Codesmith creates an ADO. Net custom template (3)
Codesmith creates an ADO. Net custom template (4)
Source code download: http://download.csdn.net/source/3535328
Source code download two: http://files.cnblogs.com/zhongweiv/AdoTemp.rar