1. 在專案中新增一個項目,Add New Item -> General 選擇 Text Template
2. 新增完成後可以看到剛剛新增的項目(.tt)與它底下包含的項目(.txt)
3. 修改output extension=".txt",就可以改變產生檔案的附檔名
4. 透過一些自行寫的程式就可以產生一些固定格式的內容如下:
Product.tt
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="Likol.VisualStudio" #>
<#@ import namespace="Likol.VisualStudio.Template" #>
<#
string ConnectionName = "Template"; // ConnectionString Name in Web.Config
string TableName = "Product"; // Table Name
TableTemplate tableTemplate = new TableTemplate(this.Host, ConnectionName, TableName);
tableTemplate.Fill();
#>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by Likol.VisualStudio.Template Tool.
// Last generater time: <#= DateTime.Now.ToString()#>
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Likol.Web.Validation;
namespace Likol.Web.TestWebSite.Data
{
public class <#= tableTemplate.TableName #>
{
<#
foreach(TableColumn tableColumn in tableTemplate.Columns)
{
#>
private <#= tableColumn.DataType #> _<#= tableColumn.Name #>;
<#
}
#>
<#
foreach(TableColumn tableColumn in tableTemplate.Columns)
{
#>
<# if (!tableColumn.Nullable){ #>
[Required]
<#}#>
<# if (tableColumn.Length != 0){ #>
[Length(<#= tableColumn.Length.ToString() #>)]
<#}#>
public <#= tableColumn.DataType #> <#= tableColumn.Name #>
{
get { return this._<#= tableColumn.Name #>; }
set { this._<#= tableColumn.Name #> = value; }
}
<#
}
#>
}
}
Product.cs - 產生的結果
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by Likol.VisualStudio.Template Tool.
// Last generater time: 2011/7/1 上午 09:54:25
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Likol.Web.Validation;
namespace Likol.Web.TestWebSite.Data
{
public class Product
{
private int _ID;
private Guid _CategotyID;
private string _Name;
[Required]
public int ID
{
get { return this._ID; }
set { this._ID = value; }
}
[Required]
public Guid CategotyID
{
get { return this._CategotyID; }
set { this._CategotyID = value; }
}
[Required]
[Length(50)]
public string Name
{
get { return this._Name; }
set { this._Name = value; }
}
}
}
TableTemplate是一個用來與SQL Server溝通讀取Table Schema的Class,它會讀取Table中欄位相關的資訊,並做一些轉換.
0 意見:
張貼意見