MVC使用T4模板生成其他类的具体实现学习笔记2
在前篇中我们已经将User类中的代码做了具体的实现,但仍然有多个实体类未实现,以后可能还会增加新的数据表,数据表结构也有可能发生变化,所以我们使用T4模板来完成类的生成,这样就算数据库表发生了改变,也会自动根据改变后的实体对类进行重新生成。
下面是数据访问层的T4模板文件Dal.tt
<#@templatelanguage="C#"debug="false"hostspecific="true"#> <#@includefile="EF.Utility.CS.ttinclude"#><#@ outputextension=".cs"#> <# CodeGenerationToolscode=newCodeGenerationTools(this); MetadataLoaderloader=newMetadataLoader(this); CodeRegionregion=newCodeRegion(this,1); MetadataToolsef=newMetadataTools(this); //EF实体文件在项目中的路径 stringinputFile=@"..\\PMS.Model\\PMS.edmx"; EdmItemCollectionItemCollection=loader.CreateEdmItemCollection(inputFile); stringnamespaceName=code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManagerfileManager=EntityFrameworkTemplateFileManager.Create(this); #> <#//这里为命名空间部分,手动更改为相应的命名空间#> usingPMS.IDAL; usingPMS.Model; usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; namespacePMS.DAL { <# //EmitEntityTypes foreach(EntityTypeentityinItemCollection.GetItems<EntityType>().OrderBy(e=>e.Name)) { //fileManager.StartNewFile(entity.Name+"RepositoryExt.cs"); //BeginNamespace(namespaceName,code); #> publicpartialclass<#=entity.Name#>Dal:BaseDal<<#=entity.Name#>>,I<#=entity.Name#>Dal { } <#}#> }
我们将EF实体文件路径、命名空间更改为对应的值时,Ctrl+S保存,即可生成对应的其他类型的数据访问类
其他层中也大同小异,只需要做对应的更改即可,下面我将提供相应的代码
IDAL层
IDal.tt
<#@templatelanguage="C#"debug="false"hostspecific="true"#> <#@includefile="EF.Utility.CS.ttinclude"#><#@ outputextension=".cs"#> <# CodeGenerationToolscode=newCodeGenerationTools(this); MetadataLoaderloader=newMetadataLoader(this); CodeRegionregion=newCodeRegion(this,1); MetadataToolsef=newMetadataTools(this); stringinputFile=@"..\\PMS.Model\\PMS.edmx"; EdmItemCollectionItemCollection=loader.CreateEdmItemCollection(inputFile); stringnamespaceName=code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManagerfileManager=EntityFrameworkTemplateFileManager.Create(this); #> usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingPMS.Model; namespacePMS.IDAL { <# //EmitEntityTypes foreach(EntityTypeentityinItemCollection.GetItems<EntityType>().OrderBy(e=>e.Name)) { //fileManager.StartNewFile(entity.Name+"RepositoryExt.cs"); //BeginNamespace(namespaceName,code); #> publicpartialinterfaceI<#=entity.Name#>Dal:IBaseDal<<#=entity.Name#>> { } <#}#> }
IDbSession.tt
<#@templatelanguage="C#"debug="false"hostspecific="true"#> <#@includefile="EF.Utility.CS.ttinclude"#><#@ outputextension=".cs"#> <# CodeGenerationToolscode=newCodeGenerationTools(this); MetadataLoaderloader=newMetadataLoader(this); CodeRegionregion=newCodeRegion(this,1); MetadataToolsef=newMetadataTools(this); stringinputFile=@"..\\PMS.Model\\PMS.edmx"; EdmItemCollectionItemCollection=loader.CreateEdmItemCollection(inputFile); stringnamespaceName=code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManagerfileManager=EntityFrameworkTemplateFileManager.Create(this); #> usingSystem; usingSystem.Collections.Generic; usingSystem.Data.Entity; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; namespacePMS.IDAL { publicpartialinterfaceIDbSession { <# //EmitEntityTypes foreach(EntityTypeentityinItemCollection.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
<#@templatelanguage="C#"debug="false"hostspecific="true"#> <#@includefile="EF.Utility.CS.ttinclude"#><#@ outputextension=".cs"#> <# CodeGenerationToolscode=newCodeGenerationTools(this); MetadataLoaderloader=newMetadataLoader(this); CodeRegionregion=newCodeRegion(this,1); MetadataToolsef=newMetadataTools(this); stringinputFile=@"..\\PMS.Model\\PMS.edmx"; EdmItemCollectionItemCollection=loader.CreateEdmItemCollection(inputFile); stringnamespaceName=code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManagerfileManager=EntityFrameworkTemplateFileManager.Create(this); #> usingSW.OA.IDAL; usingSystem; usingSystem.Collections.Generic; usingSystem.Configuration; usingSystem.Linq; usingSystem.Reflection; usingSystem.Text; usingSystem.Threading.Tasks; namespaceSW.OA.DALFactory { publicpartialclassAbstractFactory { <# foreach(EntityTypeentityinItemCollection.GetItems<EntityType>().OrderBy(e=>e.Name)) { #> publicstaticI<#=entity.Name#>DalCreate<#=entity.Name#>Dal() { stringfullClassName=NameSpace+".<#=entity.Name#>Dal"; returnCreateInstance(fullClassName)asI<#=entity.Name#>Dal; } <#}#> } }
DbSession.tt
<#@templatelanguage="C#"debug="false"hostspecific="true"#> <#@includefile="EF.Utility.CS.ttinclude"#><#@ outputextension=".cs"#> <# CodeGenerationToolscode=newCodeGenerationTools(this); MetadataLoaderloader=newMetadataLoader(this); CodeRegionregion=newCodeRegion(this,1); MetadataToolsef=newMetadataTools(this); stringinputFile=@"..\\PMS.Model\\PMS.edmx"; EdmItemCollectionItemCollection=loader.CreateEdmItemCollection(inputFile); stringnamespaceName=code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManagerfileManager=EntityFrameworkTemplateFileManager.Create(this); #> usingPMS.DAL; usingPMS.IDAL; usingPMS.Model; usingSystem; usingSystem.Collections.Generic; usingSystem.Data.Entity; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; namespacePMS.DALFactory { publicpartialclassDBSession:IDBSession { <# //EmitEntityTypes foreach(EntityTypeentityinItemCollection.GetItems<EntityType>().OrderBy(e=>e.Name)) { //fileManager.StartNewFile(entity.Name+"RepositoryExt.cs"); //BeginNamespace(namespaceName,code); #> privateI<#=entity.Name#>Dal_<#=entity.Name#>Dal; publicI<#=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
<#@templatelanguage="C#"debug="false"hostspecific="true"#> <#@includefile="EF.Utility.CS.ttinclude"#><#@ outputextension=".cs"#> <# CodeGenerationToolscode=newCodeGenerationTools(this); MetadataLoaderloader=newMetadataLoader(this); CodeRegionregion=newCodeRegion(this,1); MetadataToolsef=newMetadataTools(this); stringinputFile=@"..\\PMS.Model\\PMS.edmx"; EdmItemCollectionItemCollection=loader.CreateEdmItemCollection(inputFile); stringnamespaceName=code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManagerfileManager=EntityFrameworkTemplateFileManager.Create(this); #> usingPMS.IBLL; usingPMS.Model; usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; namespacePMS.BLL { <# //EmitEntityTypes foreach(EntityTypeentityinItemCollection.GetItems<EntityType>().OrderBy(e=>e.Name)) { //fileManager.StartNewFile(entity.Name+"RepositoryExt.cs"); //BeginNamespace(namespaceName,code); #> publicpartialclass<#=entity.Name#>Service:BaseService<<#=entity.Name#>>,I<#=entity.Name#>Service { publicoverridevoidSetCurrentDal() { CurrentDal=this.CurrentDbSession.<#=entity.Name#>Dal; } } <#}#> }
BLL层
IService.tt
<#@templatelanguage="C#"debug="false"hostspecific="true"#> <#@includefile="EF.Utility.CS.ttinclude"#><#@ outputextension=".cs"#> <# CodeGenerationToolscode=newCodeGenerationTools(this); MetadataLoaderloader=newMetadataLoader(this); CodeRegionregion=newCodeRegion(this,1); MetadataToolsef=newMetadataTools(this); stringinputFile=@"..\\PMS.Model\\PMS.edmx"; EdmItemCollectionItemCollection=loader.CreateEdmItemCollection(inputFile); stringnamespaceName=code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManagerfileManager=EntityFrameworkTemplateFileManager.Create(this); #> usingPMS.Model; usingPMS.Model.Search; usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; namespacePMS.IBLL { <# //EmitEntityTypes foreach(EntityTypeentityinItemCollection.GetItems<EntityType>().OrderBy(e=>e.Name)) { //fileManager.StartNewFile(entity.Name+"RepositoryExt.cs"); //BeginNamespace(namespaceName,code); #> publicpartialinterfaceI<#=entity.Name#>Service:IBaseService<<#=entity.Name#>> { } <#}#> }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。