您的位置:首页 > 其它

模版引擎之简单实现

2012-07-18 11:45 302 查看
模版,大家肯定都比较熟悉的一个概念,刚学C#(Java)那会老师就告诉我们,类是对象的模版。今天写这个模版其实是我用于生成js代码的,当然不限于生成js,其实跟codesmith有着差不多的功能,只是没那么强大,下面我写一些思路

我这个模版目前用于生成Ext的Grid和添加,编辑 表单,所以主角的又(怎么会是又呢,呵呵)是数据库表,不过这些信息被我存到前一篇博客里提到的Model里去了(通过特性的方式)。

出于方便,我还是把Model的代码贴一下:

MathTag

using System;
using XDbFramework;

namespace TemplateEngine
{
public class MathTag
{
private string _sourceString;
private TagIndex pkTag;
private TagIndex fkTag;
private TagIndex textTag;
private TagIndex dateTag;
private TagIndex numberTag;
private TagIndex moneyTag;
private TagIndex boolTag;
private TagIndex everyTag;

public MathTag(string sourceString)
{
_sourceString = sourceString;
pkTag = TagIndex.GetTag(sourceString, Generator.PkColumnTagName);
fkTag = TagIndex.GetTag(sourceString, Generator.FkColumnTagName);
textTag = TagIndex.GetTag(sourceString, Generator.TextColumnTagName);
dateTag = TagIndex.GetTag(sourceString, Generator.DateColumnTagName);
numberTag = TagIndex.GetTag(sourceString, Generator.NumberColumnTagName);
moneyTag = TagIndex.GetTag(sourceString, Generator.MoneyColumnTagName);
boolTag = TagIndex.GetTag(sourceString, Generator.BoolColumnTagName);
everyTag = TagIndex.GetTag(sourceString, Generator.EveryColumnTagName);
}

public TagIndex GetTagIndexByColumn(ColumnAttribute m)
{
if (m.KeyType == KeyTypeEnum.PrimaryKey && pkTag.FindThisTag)
{
return pkTag;
}
if ((m.CType == typeof(DateTime) || m.CType == typeof(DateTime?)) && dateTag.FindThisTag)
{
return dateTag;
}
if ((m.CType == typeof(bool) || m.CType == typeof(bool?)) && boolTag.FindThisTag)
{
return boolTag;
}
if ((m.CType == typeof(float) || m.CType == typeof(decimal) || m.CType == typeof(double) || m.CType == typeof(float?) || m.CType == typeof(decimal?) || m.CType == typeof(double?)) && moneyTag.FindThisTag)
{
return moneyTag;
}
if ((m.CType == typeof(int) || m.CType == typeof(int?) || m.CType == typeof(long) || m.CType == typeof(long?)) && numberTag.FindThisTag)
{
return numberTag;
}
if (m.CType == typeof(string) && textTag.FindThisTag)
{
return textTag;
}
if (m.KeyType == KeyTypeEnum.ForeignKey && fkTag.FindThisTag)
{
return fkTag;
} if (everyTag.FindThisTag)
{
return everyTag;
}
return null;
}
}
}

好,这里整个过程也就差不多结束了(不知道怎么替换?当然是Replace)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: