MVC使用T4模板生成其他類的具體實現(xiàn)學(xué)習(xí)筆記2
在前篇中我們已經(jīng)將User類中的代碼做了具體的實現(xiàn),但仍然有多個實體類未實現(xiàn),以后可能還會增加新的數(shù)據(jù)表,數(shù)據(jù)表結(jié)構(gòu)也有可能發(fā)生變化,所以我們使用T4模板來完成類的生成,這樣就算數(shù)據(jù)庫表發(fā)生了改變,也會自動根據(jù)改變后的實體對類進行重新生成。
下面是數(shù)據(jù)訪問層的T4模板文件 Dal.tt
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
//EF實體文件在項目中的路徑
string inputFile = @"..\\PMS.Model\\PMS.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
<#//這里為命名空間部分,手動更改為相應(yīng)的命名空間 #>
using PMS.IDAL;
using PMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PMS.DAL
{
<#
// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
//fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
//BeginNamespace(namespaceName, code);
#>
public partial class <#=entity.Name#>Dal :BaseDal<<#=entity.Name#>>,I<#=entity.Name#>Dal
{
}
<#}#>
}
我們將EF實體文件路徑、命名空間更改為對應(yīng)的值時,Ctrl+S保存,即可生成對應(yīng)的其他類型的數(shù)據(jù)訪問類
其他層中也大同小異,只需要做對應(yīng)的更改即可,下面我將提供相應(yīng)的代碼
IDAL層
IDal.tt
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"..\\PMS.Model\\PMS.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PMS.Model;
namespace PMS.IDAL
{
<#
// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
//fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
//BeginNamespace(namespaceName, code);
#>
public partial interface I<#=entity.Name#>Dal :IBaseDal<<#=entity.Name#>>
{
}
<#}#>
}
IDbSession.tt
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"..\\PMS.Model\\PMS.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PMS.IDAL
{
public partial interface IDbSession
{
<#
// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
//fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
//BeginNamespace(namespaceName, code);
#>
I<#=entity.Name#>Dal <#=entity.Name#>Dal{get;set;}
<#}#>
}
}
DALFactory層
SimpleDalFactory.tt
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile =@"..\\PMS.Model\\PMS.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using SW.OA.IDAL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace SW.OA.DALFactory
{
public partial class AbstractFactory
{
<#
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
#>
public static I<#=entity.Name#>Dal Create<#=entity.Name#>Dal()
{
string fullClassName = NameSpace + ".<#=entity.Name#>Dal";
return CreateInstance(fullClassName) as I<#=entity.Name#>Dal;
}
<#}#>
}
}
DbSession.tt
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"..\\PMS.Model\\PMS.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using PMS.DAL;
using PMS.IDAL;
using PMS.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PMS.DALFactory
{
public partial class DBSession : IDBSession
{
<#
// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
//fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
//BeginNamespace(namespaceName, code);
#>
private I<#=entity.Name#>Dal _<#=entity.Name#>Dal;
public I<#=entity.Name#>Dal <#=entity.Name#>Dal
{
get
{
if(_<#=entity.Name#>Dal == null)
{
_<#=entity.Name#>Dal = AbstractFactory.Create<#=entity.Name#>Dal();
}
return _<#=entity.Name#>Dal;
}
set { _<#=entity.Name#>Dal = value; }
}
<#}#>
}
}
BLL層
Service.tt
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"..\\PMS.Model\\PMS.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using PMS.IBLL;
using PMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PMS.BLL
{
<#
// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
//fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
//BeginNamespace(namespaceName, code);
#>
public partial class <#=entity.Name#>Service :BaseService<<#=entity.Name#>>,I<#=entity.Name#>Service
{
public override void SetCurrentDal()
{
CurrentDal = this.CurrentDbSession.<#=entity.Name#>Dal;
}
}
<#}#>
}
BLL層
IService.tt
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"..\\PMS.Model\\PMS.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using PMS.Model;
using PMS.Model.Search;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PMS.IBLL
{
<#
// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
//fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
//BeginNamespace(namespaceName, code);
#>
public partial interface I<#=entity.Name#>Service : IBaseService<<#=entity.Name#>>
{
}
<#}#>
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET之Excel下載模板、導(dǎo)入、導(dǎo)出操作
- asp.net GridView控件中模板列CheckBox全選、反選、取消
- asp.net使用npoi讀取excel模板并導(dǎo)出下載詳解
- 使用ASP.NET模板生成HTML靜態(tài)頁面的五種方案
- asp.net下將頁面內(nèi)容導(dǎo)入到word模板中的方法
- asp.net TemplateField模板中的Bind方法和Eval方法
- asp.net GridView模板列中實現(xiàn)選擇行功能
- asp.net(C#)壓縮圖片,可以指定圖片模板高寬
- asp.net 按指定模板導(dǎo)出word,pdf實例代碼
- ASP.NET中實現(xiàn)模板頁
相關(guān)文章
asp.net 獲取Datalist中Checkbox的值的小結(jié)
最近開發(fā)過程中遇到一個小問題,要獲取checkbox的值,在網(wǎng)上搜索了一下,發(fā)現(xiàn)基本上都是用JS實現(xiàn)的,現(xiàn)在我將自己的做法記錄一下,以便以后繼續(xù)使用。2010-04-04
jQuery AJax調(diào)用asp.net webservers的實現(xiàn)代碼
代碼是轉(zhuǎn)載來的 本來今天寫的 但是到現(xiàn)在還沒搞懂,慚愧啊2009-12-12
asp.net(c#)ref,out ,params的區(qū)別
C#中有三個關(guān)鍵字-ref,out ,params,雖然本人不喜歡這三個關(guān)鍵字,因為它們疑似破壞面向?qū)ο筇匦浴5羌热籱$把融入在c#體系中,那么我們就來認識一下參數(shù)修飾符ref,out ,params吧,還有它們的區(qū)別。2009-12-12
ASP.NET 2.0 中收集的小功能點(轉(zhuǎn))
ASP.NET 2.0 中收集的小功能點(轉(zhuǎn))...2006-12-12
在ASP.NET Core中應(yīng)用HttpClient獲取數(shù)據(jù)和內(nèi)容
這篇文章主要介紹了在ASP.NET Core中集成和使用HttpClient獲取數(shù)據(jù)和內(nèi)容,幫助大家更好的理解和學(xué)習(xí)使用ASP.NET Core,感興趣的朋友可以了解下2021-03-03
ASP.NET設(shè)計網(wǎng)絡(luò)硬盤之查看文件夾實現(xiàn)代碼
下面要介紹的實例包括網(wǎng)上硬盤的許多功能,將一步步為大家進行介紹。首先創(chuàng)建工程實例,然后進行主界面的設(shè)計,最后對各個功能的實現(xiàn)分別進行介紹2012-10-10

