c#基于NVelocity实现代码生成
作者:code2roc 时间:2023-06-03 14:46:18
在框架开发过程中,通用代码生成是一项必不可少的功能,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;
}
}
来源:https://www.cnblogs.com/yanpeng19940119/p/14320593.html
标签:c#,NVelocity,代码生成
0
投稿
猜你喜欢
详解Spring Boot中MyBatis的使用方法
2023-08-22 17:25:01
java基础--JDK SPI概述
2021-09-19 11:50:16
Java非静态成员变量之死循环(详解)
2022-04-15 09:18:20
IntelliJ IDEA 2019.1.1 for MAC 下载和注册码激活教程图解
2023-07-23 08:01:50
java 解决异常 2 字节的 UTF-8 序列的字节2 无效的问题
2021-06-01 10:29:00
Android getevent用法实例详解
2021-06-27 10:03:35
java中对list分页并显示数据到页面实例代码
2023-05-22 00:39:10
C#实现控制台飞行棋小游戏
2023-01-01 15:43:12
Android实现简单的分批加载ListView
2023-10-28 14:49:21
Java调用第三方接口示范的实现
2023-06-27 20:32:10
详解SpringBoot定时任务说明
2022-08-11 05:03:11
Java导出Execl疑难点处理的实现
2021-09-18 12:55:11
Java虚拟机内存区域划分详解
2023-06-20 22:04:24
MapStruct对象映射转换解决Bean属性拷贝性能问题
2023-05-14 04:10:55
浅谈java+内存分配及变量存储位置的区别
2022-07-09 00:46:47
Java基础知识之ByteArrayInputStream流的使用
2023-10-27 14:37:53
SpringBoot对Druid配置SQL监控功能失效问题及解决方法
2023-06-10 21:31:24
C# String常用函数的使用详解
2022-04-05 05:29:02
JAVA常用API总结与说明
2023-05-19 08:17:17
Android自定义View新年烟花、祝福语横幅动画
2022-01-24 21:31:27