c#基于NVelocity實(shí)現(xiàn)代碼生成
在框架開發(fā)過程中,通用代碼生成是一項(xiàng)必不可少的功能,c#在這后端模板引擎這方面第三方組件較少,我這里選擇的是NVelocity,現(xiàn)在升級到了NetStandard2.0,可以用于NetCore項(xiàng)目
添加引用

初始化模板引擎及設(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 來進(jìn)行循環(huán)渲染
使用 #if #end 來進(jìn)行分支判斷渲染
完整工具類代碼
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;
}
}
以上就是c#基于NVelocity實(shí)現(xiàn)代碼生成的詳細(xì)內(nèi)容,更多關(guān)于NVelocity實(shí)現(xiàn)代碼生成的資料請關(guān)注腳本之家其它相關(guān)文章!
- 詳解使用Mybatis-plus + velocity模板生成自定義的代碼
- SiteMesh如何結(jié)合Freemarker及velocity使用
- Vue中JS動畫與Velocity.js的結(jié)合使用
- 如何解決SpringBoot2.x版本對Velocity模板不支持的方案
- SpringBoot與velocity的結(jié)合的示例代碼
- 聊聊JS動畫庫 Velocity.js的使用
- springMVC+velocity實(shí)現(xiàn)仿Datatables局部刷新分頁方法
- 詳解velocity模板使javaWeb的html+js實(shí)現(xiàn)模塊化
- Mybatis velocity腳本的使用教程詳解(推薦)
- JAVA velocity模板引擎使用實(shí)例
- html文件中jquery與velocity變量中的$沖突的解決方法
- Java 如何使用Velocity引擎生成代碼
相關(guān)文章
c#之利用API函數(shù)實(shí)現(xiàn)動畫窗體的方法詳解
本篇文章是對c#中利用API函數(shù)實(shí)現(xiàn)動畫窗體的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
C# Winform實(shí)現(xiàn)圓角無鋸齒按鈕
這篇文章主要為大家詳細(xì)介紹了C# Winform實(shí)現(xiàn)圓角無鋸齒按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07
c# Invoke和BeginInvoke 區(qū)別分析
這篇文章主要介紹了c# Invoke和BeginInvoke 區(qū)別分析,需要的朋友可以參考下2014-10-10
詳解如何使用BenchmarkDotNet對.NET代碼進(jìn)行性能基準(zhǔn)測試
BenchmarkDotNet是一個基于.NET開源、功能全面、易于使用的性能基準(zhǔn)測試框架,這篇文章就來和小編一起學(xué)習(xí)一下如何使用BenchmarkDotNet對.NET代碼進(jìn)行性能基準(zhǔn)測試吧2024-12-12
C# DataGridView中實(shí)現(xiàn)勾選存儲數(shù)據(jù)和右鍵刪除數(shù)據(jù)(示例代碼)
這篇文章主要介紹了C# DataGridView中實(shí)現(xiàn)勾選存儲數(shù)據(jù)和右鍵刪除數(shù)據(jù)的示例代碼,通過示例代碼給大家展示運(yùn)行效果圖,需要的朋友可以參考下2021-07-07

