溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

NVelocity中怎么實現(xiàn)代碼生成功能

發(fā)布時間:2021-08-03 15:22:37 來源:億速云 閱讀:94 作者:Leah 欄目:編程語言

本篇文章給大家分享的是有關(guān)NVelocity中怎么實現(xiàn)代碼生成功能,小編覺得挺實用的,因此分享給大家學(xué)習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

添加引用

NVelocity中怎么實現(xiàn)代碼生成功能

初始化模板引擎及設(shè)置模板讀取路徑

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

 

讀取模板渲染結(jié)果

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}占位替換具體字符串內(nèi)容

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

使用 #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;//保存生成后的代碼內(nèi)容到文件            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;
        }
    }

以上就是NVelocity中怎么實現(xiàn)代碼生成功能,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI