这篇文章主要介绍了c#基于NVelocity实现代码生成的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
在框架开发过程中,通用代码生成是一项必不可少的功能,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;
    }
  }
以上就是c#基于NVelocity实现代码生成的详细内容,更多关于NVelocity实现代码生成的资料请关注得得之家其它相关文章!
				 沃梦达教程
				
			本文标题为:c#基于NVelocity实现代码生成
				
        
 
            
        
             猜你喜欢
        
	     - Unity Shader实现模糊效果 2023-04-27
 - user32.dll 函数说明小结 2022-12-26
 - 如何使用C# 捕获进程输出 2023-03-10
 - WPF使用DrawingContext实现绘制刻度条 2023-07-04
 - c# 模拟线性回归的示例 2023-03-14
 - 在C# 8中如何使用默认接口方法详解 2023-03-29
 - Oracle中for循环的使用方法 2023-07-04
 - .NET CORE DI 依赖注入 2023-09-27
 - C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
 - Unity3D实现渐变颜色效果 2023-01-16
 
						
						
						
						
						
				
				
				
				