在很多场景下,我们都需要代码生成。你可以使用CodeSmith,不过它是商业软件。VisualStudio2008中自带也有代码生成功能。那就是T4 (Text Template Transformation Toolkit)模板。最近写了个简单Entity模板,直接看内容,连接Northwind database 生成所有Table的Entity:
1: <#@ template language="C#" hostspecific="true" #>
2: <#@ output extension="cs" #>
3: <#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
4: <#@ assembly name="Microsoft.SqlServer.Smo" #>
5: <#@ assembly name="Microsoft.SqlServer.Management.Sdk.Sfc" #>
6: <#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
7: <#
8: // Config variables
9: string serverName = "USER\\SQLEXPRESS2008";
10: string dbName = "NORTHWND";
11: #>
12: using System;
13:
14: /// Right click .tt file "Run Custom Tool" to update partial model.
15: /// Author Petter Liu http://wintersun.cnblogs.com
16: namespace MyApp.CommonEntity
17: {
18: <#
19: // Iterate over tables and generate procs
20: Server server = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection(serverName, "sa", "sa"));
21: Database database = new Database(server, dbName);
22: database.Refresh();
23:
24: foreach (Table table in database.Tables)
25: {
26: #>
27: /// <summary>
28: /// <#= table.Name #> Entity
29: /// </summary>
30: public partial class <#= table.Name #>
31: {
32: <#
33: table.Refresh();
34: string columnCsharptype="";
35: string dbtypeStr="";
36: foreach (Column column in table.Columns)
37: {
38: dbtypeStr=column.DataType.ToString();
39: if (dbtypeStr=="nvarchar" || dbtypeStr=="nchar")
40: columnCsharptype="string";
41: else if (column.DataType.ToString()=="ntext")
42: columnCsharptype="string";
43: else if (column.DataType.ToString()=="varchar")
44: columnCsharptype="string";
45: else if (column.DataType.ToString()=="char")
46: columnCsharptype="string";
47: else if (column.DataType.ToString()=="text")
48: columnCsharptype="string";
49: else if (column.DataType.ToString()=="smallint")
50: columnCsharptype="Int16";
51: else if (column.DataType.ToString()=="datetime")
52: columnCsharptype="DateTime";
53: else if (column.DataType.ToString()=="money")
54: columnCsharptype="decimal";
55: else if (column.DataType.ToString()=="bit")
56: columnCsharptype="bool";
57: else if (column.DataType.ToString()=="image")
58: columnCsharptype="byte";
59: else if (column.DataType.ToString()=="real")
60: columnCsharptype="single";
61: else
62: columnCsharptype=column.DataType.ToString();
63: #>
64: /// <summary>
65: /// Gets or sets the <#=column.Name#>
66: /// </summary>
67: /// <value>The <#=column.Name.ToLower()#></value>
68: public <#=columnCsharptype #> <#=column.Name#> {get;set;}
69:
70: <#
71: }
72: #>
73: }
74:
75: <#
76: }
77: #>
78: }
你可以把它另存为扩展名为.tt的文件,我起名叫CommonEntity.tt。 然后在VisualStudio2008中选中它点击右键执行Run Custom Tool,就自动生成这样的代码:
1: using System;
2:
3: /// Right click .tt file "Run Custom Tool" to update partial model.
4: /// Author Petter Liu http://wintersun.cnblogs.com
5: namespace MyApp.CommonEntity
6: {
7: /// <summary>
8: /// Categories Entity
9: /// </summary>
10: public partial class Categories
11: {
12: /// <summary>
13: /// Gets or sets the CategoryID
14: /// </summary>
15: /// <value>The categoryid</value>
16: public int CategoryID {get;set;}
17:
18: /// <summary>
19: /// Gets or sets the CategoryName
20: /// </summary>
21: /// <value>The categoryname</value>
22: public string CategoryName {get;set;}
23:
24: /// <summary>
25: /// Gets or sets the Description
26: /// </summary>
27: /// <value>The description</value>
28: public string Description {get;set;}
29:
30: /// <summary>
31: /// Gets or sets the Picture
32: /// </summary>
33: /// <value>The picture</value>
34: public byte Picture {get;set;}
35:
36: }
这是最简约的Entity code了,实际你可扩展你所需要的模板。关于T4模板,网上有些资料可以参考:
T4 (Text Template Transformation Toolkit) Code Generation - Best Kept Visual Studio Secret
还有一些教程:
希望这篇Post对您有帮助。
Author:Petter Liu http://wintersun.cnblogs.com