因为创建Model、Repository、Service、Controller有大量重复的工作。
除了一些通用的方法会整合到BaseRepository和BaseService里,剩余的还需要自定义。
这时候就需要T4模板的出马了,T4模板可以根据事先设定好的模板文件生成代码。以Modal为例
一、新建T4模板项目
二、新建文本模板文件
三、编写模板文件
以Modal.tt为例
<#@ template debug="false" hostspecific="false" language="C#" #> <#@ assembly name="System.Core" #> <#@ assembly name="System.Data" #> <#@ assembly name="System.Xml" #> <#@ assembly name="Microsoft.SqlServer.Smo" #> <#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #> <#@ assembly name="Microsoft.SqlServer.Management.Sdk.Sfc" #> <#@ import namespace="System" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="Microsoft.SqlServer.Management.Smo" #> <#@ import namespace="System.Collections.Generic" #> <#@ output extension=".cs" #> <# string sqlServer = ".\\"; string sqlLogin = "usrfordb"; string sqlPassword = "111111"; string sqlDatabase = "fang"; string sqlTableName = "TBPosition"; string destinationFolder = @"D:\\Randy.Fang.API-备用代码\\Create"; Server server = new Server(sqlServer); server.ConnectionContext.LoginSecure = false; server.ConnectionContext.Login = sqlLogin; server.ConnectionContext.Password = sqlPassword; server.ConnectionContext.Connect(); foreach (Table table in server.Databases[sqlDatabase].Tables) { string[] tables = new[] { sqlTableName }; if(!tables.Contains(table.Name)) { continue; } #> using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Randy.Fang.Model { /// <summary> /// <#=sqlTableName#>表 /// </summary> public class <#=sqlTableName#> : BaseEntity { public <#=sqlTableName#>() { } <# int columnCount = table.Columns.Count; int i = 0; foreach (Column col in table.Columns) { i++; string propertyType = GetNetDataType(col.DataType.Name); if (string.IsNullOrWhiteSpace(propertyType)) { continue; } if (col.Name == "CreateTime" || col.Name == "ModifyTime" || col.Name == "IsDeleted" || col.Name == "Version" || col.Name == "TenantID") { continue; } if (col.Nullable && propertyType != "string" && propertyType != "String") { propertyType += "?"; } #> <# if (col.InPrimaryKey) { #> [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] <# } #> public virtual <#= propertyType #> <#= col.Name #> { get; set; } <# if (col.InPrimaryKey) { #> <# } #> <# if (i != columnCount) { #> <# } #> <# } #> } } <# // SaveOutput(table.Name + ".cs", destinationFolder); } #> <#+ public static string GetNetDataType(string sqlDataTypeName) { switch (sqlDataTypeName.ToLower()) { case "bigint": return "Int64"; case "binary": case "image": case "varbinary": return "byte[]"; case "bit": return "Boolean"; case "char": return "char"; case "datetime": case "smalldatetime": return "DateTime"; case "decimal": case "money": case "numeric": return "Decimal"; case "float": return "Double"; case "time": return "TimeSpan"; case "int": return "Int32"; case "nchar": case "nvarchar": case "text": case "varchar": case "xml": return "String"; case "real": return "single"; case "smallint": return "Int16"; case "tinyint": return "byte"; case "uniqueidentifier": return "Guid"; default: return null; } } void SaveOutput(string outputFileName, string destinationFolder) { // Write to destination folder // string templateDirectory = Path.Combine(Path.GetDirectoryName(Host.TemplateFile), destinationFolder); // string outputFilePath = Path.Combine(templateDirectory, outputFileName); // File.Delete(outputFilePath); // File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString()); // Flush generation // this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length); } #>
四、保存模板文件的时候就会自动生成模板了
比如我保存后生成的代码如下
using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Randy.Fang.Model { /// <summary> /// TBPosition表 /// </summary> public class TBPosition : BaseEntity { public TBPosition() { } [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public virtual Int32 PositionID { get; set; } public virtual String PositionName { get; set; } public virtual Int32 PositionOrder { get; set; } public virtual String PositionDescription { get; set; } public virtual String PositionPinyin { get; set; } public virtual Int32 RefGroupID { get; set; } public virtual Boolean IsEnable { get; set; } public virtual Boolean IsBusiness { get; set; } public virtual Boolean IsWeekend { get; set; } public virtual TimeSpan OnworkTime { get; set; } public virtual TimeSpan OffworkTime { get; set; } public virtual Int32 BasicSalary { get; set; } } }
(本文完)
标签:set,get,T4,virtual,管理软件,using,public,模板 From: https://www.cnblogs.com/randytech/p/16668722.html