MVC使用T4模板生成其他類的具體實(shí)現(xiàn)學(xué)習(xí)筆記2
在前篇中我們已經(jīng)將User類中的代碼做了具體的實(shí)現(xiàn),但仍然有多個(gè)實(shí)體類未實(shí)現(xiàn),以后可能還會(huì)增加新的數(shù)據(jù)表,數(shù)據(jù)表結(jié)構(gòu)也有可能發(fā)生變化,所以我們使用T4模板來(lái)完成類的生成,這樣就算數(shù)據(jù)庫(kù)表發(fā)生了改變,也會(huì)自動(dòng)根據(jù)改變后的實(shí)體對(duì)類進(jìn)行重新生成。
下面是數(shù)據(jù)訪問(wèn)層的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實(shí)體文件在項(xiàng)目中的路徑 string inputFile = @"..\\PMS.Model\\PMS.edmx"; EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string namespaceName = code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this); #> <#//這里為命名空間部分,手動(dòng)更改為相應(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實(shí)體文件路徑、命名空間更改為對(duì)應(yīng)的值時(shí),Ctrl+S保存,即可生成對(duì)應(yīng)的其他類型的數(shù)據(jù)訪問(wèn)類
其他層中也大同小異,只需要做對(duì)應(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#>> { } <#}#> }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET之Excel下載模板、導(dǎo)入、導(dǎo)出操作
- asp.net GridView控件中模板列CheckBox全選、反選、取消
- asp.net使用npoi讀取excel模板并導(dǎo)出下載詳解
- 使用ASP.NET模板生成HTML靜態(tài)頁(yè)面的五種方案
- asp.net下將頁(yè)面內(nèi)容導(dǎo)入到word模板中的方法
- asp.net TemplateField模板中的Bind方法和Eval方法
- asp.net GridView模板列中實(shí)現(xiàn)選擇行功能
- asp.net(C#)壓縮圖片,可以指定圖片模板高寬
- asp.net 按指定模板導(dǎo)出word,pdf實(shí)例代碼
- ASP.NET中實(shí)現(xiàn)模板頁(yè)
相關(guān)文章
微信公眾平臺(tái)開(kāi)發(fā)之認(rèn)證"成為開(kāi)發(fā)者".Net代碼解析
這篇文章主要為大家詳細(xì)解析了微信公眾平臺(tái)開(kāi)發(fā)之認(rèn)證"成為開(kāi)發(fā)者".Net代碼,感興趣的小伙伴們可以參考一下2016-06-06asp.net后臺(tái)彈窗如何實(shí)現(xiàn)
這篇文章主要介紹了asp.net后臺(tái)彈窗如何實(shí)現(xiàn),需要的朋友可以參考下2014-02-02ASP.NET MVC中使用jQuery時(shí)的瀏覽器緩存問(wèn)題詳解
這篇文章主要介紹了ASP.NET MVC中使用jQuery時(shí)的瀏覽器緩存問(wèn)題詳解,需要的朋友可以參考下。2016-06-06C#實(shí)現(xiàn)上傳照片到物理路徑,并且將地址保存到數(shù)據(jù)庫(kù)的小例子
這篇文章主要介紹了c#上傳圖片,并將地址保存到數(shù)據(jù)庫(kù)中的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-12-12asp.net MVC使用PagedList.MVC實(shí)現(xiàn)分頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了asp.net MVC使用PagedList.MVC實(shí)現(xiàn)分頁(yè)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07MVC文件上傳支持批量上傳拖拽及預(yù)覽文件內(nèi)容校驗(yàn)功能
這篇文章主要介紹了MVC文件上傳支持批量上傳拖拽及預(yù)覽文件內(nèi)容校驗(yàn)功能,需要的朋友可以參考下2017-03-03ASP.NET Core DI手動(dòng)獲取注入對(duì)象的方法
這篇文章主要給大家介紹了關(guān)于ASP.NET Core DI手動(dòng)獲取注入對(duì)象的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11