MVC uses the T4 template to generate specific implementation Notes for other classes 2, mvct4
In the previous article, we have implemented the code in the User class, but there are still multiple entity classes that are not implemented, and new data tables may be added in the future, the data table structure may also change, so we use the T4 template to generate classes, so that even if the database table is changed, the class is automatically regenerated Based on the changed object.
Below is the data access layer's T4 template file 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); // path of the EF object file in the project string inputFile = @".. \ PMS. model \ PMS. edmx "; EdmItemCollection ItemCollection = loader. createEdmItemCollection (inputFile); string namespaceName = code. vsNamespaceSuggestion (); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager. create (this); #>< # // here is the namespace section, manually changed to the corresponding namespace #> using PMS. IDAL; using PMS. model; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace PMS. DAL {<# // Emit Entity Typesforeach (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 {}<##>}
When we change the EF object file path and namespace to the corresponding value, Ctrl + S save to generate the corresponding data category class for other types
Similar to other layers, you only need to make the corresponding changes. Below I will provide the corresponding code
IDAL Layer
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 Typesforeach (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 Typesforeach (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 Layer
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 Typesforeach (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 Layer
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 Typesforeach (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 Layer
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 Typesforeach (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#>> { } <#}#> }
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.