网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

NVelocity中怎么实现代码生成功能

本篇文章给大家分享的是有关NVelocity中怎么实现代码生成功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联建站专注于建邺网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供建邺营销型网站建设,建邺网站制作、建邺网页设计、建邺网站官网定制、微信平台小程序开发服务,打造建邺网络公司原创品牌,更为您提供建邺网站排名全网营销落地服务。

添加引用

NVelocity中怎么实现代码生成功能

初始化模板引擎及设置模板读取路径

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 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 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中怎么实现代码生成功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


分享名称:NVelocity中怎么实现代码生成功能
分享链接:http://bjjierui.cn/article/popdoh.html

其他资讯