NVelocity實現代碼生成

在框架開發過程中,通用代碼生成是一項必不可少的功能,c#在這後端模板引擎這方面第三方組件較少,我這裏選擇的是NVelocity,現在升級到了NetStandard2.0,可以用於NetCore項目

添加引用


初始化模板引擎及設置模板讀取路徑

            vltEngine = new VelocityEngine();
            vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CloudUtil.GetContentPath() + "/" + "Template");
            vltEngine.Init();

讀取模板渲染結果

 VelocityContext vltContext = new VelocityContext();
            foreach (var item in RenderDataDic)
            {
                vltContext.Put(item.Key, item.Value);
            }
            Template vltTemplate = vltEngine.GetTemplate(TemplateFileName);
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltTemplate.Merge(vltContext, vltWriter);
            string CodeContent = vltWriter.GetStringBuilder().ToString();

模板語法

示例Entity模板

using FastORM.Attribute;
using FastORM.Entity;
using System;
using System.Collections.Generic;
using System.Text;

namespace ${NameSpace}.Entity
{
    [Table(Name = "${TablePhysicalNameLowCase}")]
    public class ${TablePhysicalName} : BaseEntity
    {
        [Key]
        public string RowGuid { set; get; }
        #foreach( $Column in $ColumnList)
        #if (($Column.ColumnType == 10 || $Column.ColumnType ==  50) && $Column.PhysicalColumnName!="RowGuid")
        public string $Column.PhysicalColumnName { set; get; }
        #end
        #if ($Column.ColumnType == 20 && $Column.PhysicalColumnName!="RowGuid")
        public int $Column.PhysicalColumnName { set; get; }
        #end
        #if ($Column.ColumnType == 30 && $Column.PhysicalColumnName!="RowGuid")
        public decimal $Column.PhysicalColumnName { set; get; }
        #end
        #if ($Column.ColumnType == 40 && $Column.PhysicalColumnName!="RowGuid")
        public DateTime? $Column.PhysicalColumnName { set; get; }
        #end
        #end
    }
}

常用語法

使用${xxx}佔位替換具體字符串內容

使用 #foreach( $Itemin $ItemList)  #end 來進行循環渲染

使用 #if #end 來進行分支判斷渲染

完整工具類代碼

public class TemplateUtil
    {
        private static VelocityEngine vltEngine;
        public static string CodeTempPath;

        private static void InitTemplateSetting()
        {
            CodeTempPath = AppConfigUtil.Configuration["Frame:GenerateCodeTemplatePath"];
            DirectoryInfo CodePath = new DirectoryInfo(CloudUtil.GetContentStaticFilePath() + CodeTempPath);
            if (!CodePath.Exists)
            {
                CodePath.Create();
            }
            vltEngine = new VelocityEngine();
            vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CloudUtil.GetContentPath() + "/" + "Template");
            vltEngine.Init();
        }

        public static string GeneratemeplateFile(string FileID, string TableName, string TemplateFileName, string CodeFileName, Dictionary<string, object> RenderDataDic)
        {
            InitTemplateSetting();
            DirectoryInfo CodePath = new DirectoryInfo(CloudUtil.GetContentStaticFilePath() + CodeTempPath + "/" + FileID);
            if (!CodePath.Exists)
            {
                CodePath.Create();
            }
            CodePath = new DirectoryInfo(CloudUtil.GetContentStaticFilePath() + CodeTempPath + "/" + FileID + "/" + TableName);
            if (!CodePath.Exists)
            {
                CodePath.Create();
            }
            VelocityContext vltContext = new VelocityContext();
            foreach (var item in RenderDataDic)
            {
                vltContext.Put(item.Key, item.Value);
            }
            Template vltTemplate = vltEngine.GetTemplate(TemplateFileName);
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltTemplate.Merge(vltContext, vltWriter);
            string CodeContent = vltWriter.GetStringBuilder().ToString();
            string CodeFilePath = CloudUtil.GetContentStaticFilePath() + CodeTempPath + "/" + FileID + "/" + TableName + "/" + CodeFileName;
            //保存生成後的代碼內容到文件
            FileUtil.SaveStringToFile(CodeFilePath, CodeContent);
            return CodeFilePath;
        }

        public static string GenerateTemplateContent(string TemplateFileName, Dictionary<string, object> RenderDataDic)
        {
            InitTemplateSetting();
            VelocityContext vltContext = new VelocityContext();
            foreach (var item in RenderDataDic)
            {
                vltContext.Put(item.Key, item.Value);
            }
            Template vltTemplate = vltEngine.GetTemplate(TemplateFileName);
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltTemplate.Merge(vltContext, vltWriter);
            string CodeContent = vltWriter.GetStringBuilder().ToString();
            return CodeContent;
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章