在框架开发过程中,通用代码生成是一项必不可少的功能,c#在这后端模板引擎这方面第三方组件较少,我这里选择的是NVelocity,现在升级到了NetStandard2.0,可以用于NetCore项目
添加引用
初始化模板引擎及设置模板读取路径
复制代码
1
2
3
4vltEngine = new VelocityEngine(); vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file"); vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CloudUtil.GetContentPath() + "/" + "Template"); vltEngine.Init();
读取模板渲染结果
复制代码
1
2
3
4
5
6
7
8
9VelocityContext 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模板
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29using 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 来进行分支判断渲染
完整工具类代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62public 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; } }
以上就是c#基于NVelocity实现代码生成的详细内容,更多关于NVelocity实现代码生成的资料请关注靠谱客其它相关文章!
最后
以上就是受伤小蚂蚁最近收集整理的关于c#基于NVelocity实现代码生成的全部内容,更多相关c#基于NVelocity实现代码生成内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复