首页 > 其他分享 >房产中介管理软件第7课:T4模板的使用

房产中介管理软件第7课:T4模板的使用

时间:2022-09-22 15:33:05浏览次数:55  
标签:set get T4 virtual 管理软件 using public 模板

因为创建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

相关文章

  • 如何创建service的时候使用template模板?
    什么模板 模板?什么鬼,其实非常的简单! 就是在创建service的时候,直接引用变量,获取变量的值,然后将这些值变成具体的参数值。 可以设置的参数 --hostname--mount......
  • sbatch命令在集群递交任务模板
     001、脚本模板#!/bin/bash#SBATCH-JTEST_NAME#本次作业的名称#SBATCH-pxhacnormala#指定作业队列名#SBATCH-o......
  • 30套各行业可视化模板,比Excel好看千倍!
    可视化大屏的目的是通过将数据在屏幕上可视化,帮助用户熟悉业务数据,以便于高效地处理信息、快速做出应答。 以下行业案例模板均来源于:山海鲸可视化  1. 医疗行业进......
  • 房产中介管理软件错误记录一
    一、Core3.1添加Session后报错:Someservicesarenotabletobeconstructed在使用“services.AddSession();”和“app.UseSession();”之后,启动项目报错:System.Aggrega......
  • 房产中介管理软件第6课:全局配置文件读取和使用
    思路:通过appsetting.json来配置所有的配置信息,比如数据库连接、日志配置、Redis配置等等。一、appsettings.json配置{"Logging":{"LogLevel":{"Defaul......
  • C++ 模板类继承
    template<classT>classA{protected:voidTest(){printf("%f",0.1f);}};template<classT>classB:publicA<T>{public:voidTest2()......
  • redis基础系列~监控模板
    {"annotations":{"list":[{"builtIn":1,"datasource":"--Grafana--","enable":true,"hide":true,......
  • UEC++ 资源加载(四)模板资源拾取类
    TSoftObjectPtr和TSoftClassPtr模板类帮助我们在进行资源操作时增加了类型安全检查,我们可以在细节面板中根据给定的模版类型拾取对应的资源,以获得更加高效的操作!同样的,TS......
  • 房产中介管理软件第5课:框架整体分层
    整体架构设计如下 1、Model层:所有实体类2、Common层:数据库访问、配置文件读取、帮助类、实用工具类等3、Repository层:仓储层,用于数据库访问,尽量不做逻辑判断4、Serv......
  • 房产中介管理软件第4课:ORM框架SqlSugar配置
    本文只做SqlSugar的配置,具体功能还要到实际使用。 1、Nuget安装SqlSugarCore到项目2、假如我们有个表,TBAction3、建立对应的类文件,SqlSugar支持DBFirst和CodeFir......