Ef+T4模板實現代碼快速生成器


轉載請注明地址:http://www.cnblogs.com/cainiaodage/p/4953601.html 

效果如圖,demo(點擊demo可下載案例)

 

項目結構如圖

T4BLL添加BLL.tt文件;

T4Model添加Model文件;

T4DAL添加DAL.tt文件;

T4DAL 添加ADO.NET Entity Data Model選擇dababase first 模式;


 

打開Model1.edmx文件下的Model1.tt打開,復制內容替換了T4Model項目下Model.tt文件;

打開Model1.edmx文件下的Model.tt從<#=codeStringGenerator.EntityClassOpening(entity)#>{ 開始選擇至 }<# EndNamespace(code); 刪除

如下圖

復制Model1.tt內容替換BLL.tt、DAL.tt內容,現在Model1.tt已經沒有什么意義了,可以刪除。


 

打開T4Model的T4模板,修改如下代碼,修改完成后保存下就可以生成出Edmx中添加類的Model。

const string inputFile = @"Model1.edmx";
//更改為
string inputFile = Host.ResolveAssemblyReference("$(ProjectDir)").Replace("T4Model","T4DAL")+"/Model1.edmx"; 

 

打開T4DAL的T4模板

fileManager.StartNewFile(entity.Name + ".cs");
//更改為
fileManager.StartNewFile(entity.Name + "_DAL.cs");
//---分割線---
 public string EntityClassOpening(EntityType entity)
    {
        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1}partial class {2}{3}",
            Accessibility.ForType(entity),
            _code.SpaceAfter(_code.AbstractOption(entity)),
            _code.Escape(entity),
            _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
    }
//更改為
 public string EntityClassOpening(EntityType entity)
    {
        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1}partial class {2}_DAL{3}",
            Accessibility.ForType(entity),
            _code.SpaceAfter(_code.AbstractOption(entity)),
            _code.Escape(entity),
            _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
    }
//---分割線---更改為
foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
    fileManager.StartNewFile(entity.Name + "_DAL.cs");
    BeginNamespace(code);
#>

using T4Model;
<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
<#=codeStringGenerator.EntityClassOpening(entity)#>
{
    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public <#= entity.Name #> Add(<#= entity.Name #> entity){
        //內容實現
        return null;
    }

}
<#
    EndNamespace(code);
}

 


 

打開T4BLL的T4模板

const string inputFile = @"Model1.edmx";
//更改為
string inputFile = Host.ResolveAssemblyReference("$(ProjectDir)").Replace("T4BLL","T4DAL")+"/Model1.edmx"; 
//---分割線---
fileManager.StartNewFile(entity.Name + ".cs");
//更改為
fileManager.StartNewFile(entity.Name + "_BLL.cs");
//---分割線---
 public string EntityClassOpening(EntityType entity)
    {
        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1}partial class {2}{3}",
            Accessibility.ForType(entity),
            _code.SpaceAfter(_code.AbstractOption(entity)),
            _code.Escape(entity),
            _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
    }
//更改為
 public string EntityClassOpening(EntityType entity)
    {
        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1}partial class {2}_BLL{3}",
            Accessibility.ForType(entity),
            _code.SpaceAfter(_code.AbstractOption(entity)),
            _code.Escape(entity),
            _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
    }
//---分割線---更為
foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
    fileManager.StartNewFile(entity.Name + "_BLL.cs");
    BeginNamespace(code);
#>
using T4DAL;
using T4Model;
<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
<#=codeStringGenerator.EntityClassOpening(entity)#>
{
    <#= entity.Name #>_DAL dal = new <#= entity.Name #>_DAL();
    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public <#= entity.Name #> Add(<#= entity.Name #> entity){
        return dal.Add(entity);
    }
}
<#
    EndNamespace(code);
}

Host.ResolveAssemblyReference("$(ProjectDir)")是獲取項目路徑的方法,通過替換項目名稱來尋找到edmx文件,從而實現T4模板分離在不同的類庫中。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM