更改动软代码生成器模板 验证Model数据合法性
1.第一个模板 判断字段是否为空 类
IsNullableType.cmt
static public partial class CommonType
{
public static bool IsNullableType(Type theType)
{
return (theType.IsGenericType && theType.
GetGenericTypeDefinition().Equals
(typeof(Nullable<>)));
}
}
2.第二个模板 定义字段类型及长度
DBFiledTypeAndSizeAttribute.cmt
[AttributeUsage( AttributeTargets.Property)]
public class DBFiledTypeAndSizeAttribute : Attribute
{
public System.Data.SqlDbType DbType { get; set; }
public int Size { get; set; }
public DBFiledTypeAndSizeAttribute(System.Data.SqlDbType DbType, int Size)
{
this.DbType = DbType;
this.Size = Size;
}
}
3.第三个模板 扩展属性
CustomAttribute.cmt
/// <summary>
/// 对Attribute类扩展方法
/// </summary>
public static class CustomAttribute
{
/// <summary>
/// 判断是否存在相应的特性
/// </summary>
/// <typeparam name="T">特性类</typeparam>
/// <param name="type"></param>
/// <returns></returns>
static public bool HasAttribute<T>(this Type type ) where T:class
{
object[] attributes = type.GetCustomAttributes(false);
foreach (Attribute attr in attributes)
{
//判断Attribute 中是否 为 UniqueColumnAttribute
if (attr is T)
{
return true;
}
}
return false;
}
/// <summary>
/// 获取相应的Attribute对象 如 var attr=typeof(Person).GetAttribute<DBTableNameAttribute>();
/// </summary>
/// <typeparam name="T">Attribute类</typeparam>
/// <param name="type">实体类</param>
/// <returns>Attribute对象</returns>
static public T GetAttribute<T>(this Type type) where T:class
{
//var info = typeof(MyCodeClass);
//var classAttribute = (VersionAttribute)Attribute.GetCustomAttribute(info, typeof(VersionAttribute));
//Console.WriteLine(classAttribute.Name);
//Console.WriteLine(classAttribute.Date);
//Console.WriteLine(classAttribute.Describtion);
// var info = typeof(MyCode);
Attribute classAttribute = Attribute.GetCustomAttribute(type, typeof(T));
//var info = typeof(T);
//var classAttribute = (T)Attribute.GetCustomAttribute(info, typeof(T));
return classAttribute as T;
}
/// <summary>
/// 判断是否存在相应的特性
/// </summary>
/// <typeparam name="T">特性类</typeparam>
/// <param name="type"></param>
/// <returns></returns>
static public bool HasAttribute<T>(this System.Reflection.MemberInfo type) where T : class
{
object[] attributes = type.GetCustomAttributes(false);
foreach (Attribute attr in attributes)
{
//判断Attribute 中是否 为 UniqueColumnAttribute
if (attr is T)
{
return true;
}
}
return false;
}
/// <summary>
/// 获取相应的Attribute对象 如 var attr=typeof(Person).GetAttribute<DBTableNameAttribute>();
/// </summary>
/// <typeparam name="T">Attribute类</typeparam>
/// <param name="type">实体类</param>
/// <returns>Attribute对象</returns>
static public T GetAttribute<T>(this System.Reflection.MemberInfo type) where T : class
{
//var info = typeof(MyCodeClass);
//var classAttribute = (VersionAttribute)Attribute.GetCustomAttribute(info, typeof(VersionAttribute));
//Console.WriteLine(classAttribute.Name);
//Console.WriteLine(classAttribute.Date);
//Console.WriteLine(classAttribute.Describtion);
// var info = typeof(MyCode);
Attribute classAttribute = Attribute.GetCustomAttribute(type, typeof(T));
//var info = typeof(T);
//var classAttribute = (T)Attribute.GetCustomAttribute(info, typeof(T));
return classAttribute as T;
}
}
4.第四个模板 生成Model
<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".cs" #>
<#
TableHost host = (TableHost)(Host);
host.Fieldlist.Sort(CodeCommon.CompareByintOrder);
#>
using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
namespace <#= host.NameSpace #>.Model<# if( host.Folder.Length > 0) {#>.<#= host.Folder #><# } #>
{
<# if( host.TableDescription.Length > 0) {#>
//<#= host.TableDescription #>
<# } #>
public class <#= host.GetModelClass(host.TableName) #>
{
<# foreach (ColumnInfo c in host.Fieldlist)
{ #>/// <summary>
/// <#= string.IsNullOrEmpty(c.Description) ? c.ColumnName : c.Description #>
/// </summary>
private <#= CodeCommon.DbTypeToCS(c.TypeName) #> _<#= c.ColumnName.ToString().ToLower() #>;
[DBFiledTypeAndSize(SqlDbType.<#=CodeCommon.DbTypeLength(host.DbType, c.TypeName, c.Length)#>)]
public <#= CodeCommon.DbTypeToCS(c.TypeName) #> <#= c.ColumnName #>
{
get{ return _<#= c.ColumnName.ToString().ToLower()#>; }
set{ _<#= c.ColumnName.ToString().ToLower() #> = value; }
}
<# } #>
public bool AllIsValid()
{
System.Type type = this.GetType();
System.Reflection.PropertyInfo[] Props = type.GetProperties();
foreach(var prop in Props) //(int i = 0; i < Props.Length; i++)
{
System.Data.SqlDbType dbType;
int size = 0;
object propValue;
propValue = prop.GetValue(this, null);
Type propType = prop.PropertyType;
bool isAllowNull = CommonType.IsNullableType(propType);
if (prop.HasAttribute<DBFiledTypeAndSize>())
{
if (isAllowNull && propValue == null)
continue;
DBFiledTypeAndSize attr = prop.GetAttribute<DBFiledTypeAndSize>();
dbType = attr.DbType;
size = attr.Size;
if (dbType == System.Data.SqlDbType.NVarChar || dbType == System.Data.SqlDbType.NChar)
{
string propValue2 = propValue.ToString();
if (propValue2.Length > size)
return false;
}
else if (dbType == System.Data.SqlDbType.VarChar || dbType == System.Data.SqlDbType.Char)
{
string propValue2 = propValue.ToString();
byte[] bytes = Encoding.Default.GetBytes(propValue2);
if (bytes.Length > size)
return false;
}
else if (dbType == System.Data.SqlDbType.Bit )
{
string propValue2 = propValue.ToString();
int v = 0;
if (!int.TryParse(propValue2, out v))
{
return false;
}
if (propValue2 == "0" || propValue2 == "1")
{ }
else
return false;
}
}
}
return true;
}
}
}
效果如下:
public class Order
{
[DBFiledTypeAndSize(System.Data.SqlDbType.Bit ,1)]
public int? Flag{get;set;}
[DBFiledTypeAndSize(System.Data.SqlDbType.VarChar, 10)]
public string OrderNo { get; set; }
[DBFiledTypeAndSize(System.Data.SqlDbType.VarChar, 10)]
public string ClientName { get; set; }
public bool AllIsValid()
{
System.Type type = this.GetType();
System.Reflection.PropertyInfo[] Props = type.GetProperties();
foreach(var prop in Props) //(int i = 0; i < Props.Length; i++)
{
System.Data.SqlDbType dbType;
int size = 0;
object propValue;
propValue = prop.GetValue(this, null);
Type propType = prop.PropertyType;
bool isAllowNull = CommonType.IsNullableType(propType);
if (prop.HasAttribute<DBFiledTypeAndSize>())
{
if (isAllowNull && propValue == null)
continue;
DBFiledTypeAndSize attr = prop.GetAttribute<DBFiledTypeAndSize>();
dbType = attr.DbType;
size = attr.Size;
//下面的代码可以 用工厂方法实现,这里只是提供一下思路,希望大家不要介意
if (dbType == System.Data.SqlDbType.NVarChar || dbType == System.Data.SqlDbType.NChar)
{
string propValue2 = propValue.ToString();
if (propValue2.Length > size)
return false;
}
else if (dbType == System.Data.SqlDbType.VarChar || dbType == System.Data.SqlDbType.Char)
{
string propValue2 = propValue.ToString();
byte[] bytes = Encoding.Default.GetBytes(propValue2);
if (bytes.Length > size)
return false;
}
else if (dbType == System.Data.SqlDbType.Bit )
{
string propValue2 = propValue.ToString();
int v = 0;
if (!int.TryParse(propValue2, out v))
{
return false;
}
if (v== 0 || v == 1)
{ }
else
return false;
}
}
}
return true;
}
}
调用方法如下:
CommonClass.Order order111 = new CommonClass.Order()
{
Flag =3,
OrderNo="asdfq1q324",
ClientName ="张 模压工asdf"
};
if (order111.AllIsValid())
{
}
更改动软代码生成器模板 验证Model数据合法性的更多相关文章
- MVC 3 数据验证 Model Validation 详解
在MVC 3中 数据验证,已经应用的非常普遍,我们在web form时代需要在View端通过js来验证每个需要验证的控件值,并且这种验证的可用性很低.但是来到了MVC 新时代,我们可以通过MVC提供的 ...
- (转)MVC 3 数据验证 Model Validation 详解
继续我们前面所说的知识点进行下一个知识点的分析,这一次我们来说明一下数据验证.其实这是个很容易理解并掌握的地方,但是这会浪费大家狠多的时间,所以我来总结整理一下,节约一下大家宝贵的时间. 在MVC 3 ...
- <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解
MVC 3 数据验证 Model Validation 详解 再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...
- MVC Model数据验证
概述 上节我们学习了Model的数据在界面之间的传递,但是很多时候,我们在数据传递的时候为了确保数据的有效性,不得不给Model的相关属性做基本的数据验证. 本节我们就学习如何使用 System.Co ...
- .NET MVC model数据验证
MVC提供了很方便的数据验证,只需要在model里加入相关的正则等,那么就会在前台里生成相关的验证脚本.需要引用两个js文件: jquery.validate.min.js jquery.valida ...
- 动软代码生成器 可用于生成Entity层,可更改模板 /codesmith 也可以
动软代码生成器官方下载地址:http://www.maticsoft.com/download.aspx 教程:http://jingyan.baidu.com/article/219f4bf7dfd ...
- Ruby Rails学习中:User 模型,验证用户数据
用户建模 一. User 模型 实现用户注册功能的第一步是,创建一个数据结构,用于存取用户的信息. 在 Rails 中,数据模型的默认数据结构叫模型(model,MVC 中的 M).Rails 为解决 ...
- C# 嵌入dll 动软代码生成器基础使用 系统缓存全解析 .NET开发中的事务处理大比拼 C#之数据类型学习 【基于EF Core的Code First模式的DotNetCore快速开发框架】完成对DB First代码生成的支持 基于EF Core的Code First模式的DotNetCore快速开发框架 【懒人有道】在asp.net core中实现程序集注入
C# 嵌入dll 在很多时候我们在生成C#exe文件时,如果在工程里调用了dll文件时,那么如果不加以处理的话在生成的exe文件运行时需要连同这个dll一起转移,相比于一个单独干净的exe,这种形 ...
- Java快速开发平台,JEECG 3.7.7闪电版本发布,增加多套主流UI代码生成器模板
JEECG 3.7.7 闪电版本发布,提供5套主流UI代码生成器模板 导读 ⊙平台性能优化,速度闪电般提升 ⊙提供5套新的主流UI代码生成器模板(Bootstrap表单+Boots ...
随机推荐
- HttpServletRequest 中 getRequestURL和getRequestURI的区别
比如说有这样的一个页面 test1.jsp======================= <a href ="test.jsp?name=wf">跳转到test2.js ...
- 整理MAC下Eclipse的常用快捷键
整理Eclipse常用快捷键 开发环境切换到Mac下后原来Window下的快捷键很大一部分是不相容的,习惯了快捷键的生活忽然哪天快捷键不起作用了,跟着的就是开发效率明显降低,频繁录入错误的快捷键让Ec ...
- 我的WCF摸爬滚打之路(1)
等了好久终于等到今天!盼了好久终于把梦实现……哈哈,仅以此歌词来庆祝我为期3天的wcf学习之路圆满结束. 今天写这个文章的目的在于记录一下我自己在学习WCF的时候碰到的一些问题,俗话说,好记心不如烂笔 ...
- [转]redis 五种数据类型的使用场景
FROM : http://blog.csdn.net/gaogaoshan/article/details/41039581#t5 String 1.String 常用命令: 除了get.set.i ...
- 待整理-coredump
Linux下如何产生coredump(gdb调试用) 任务发生异常,需要记录遗言信息,利用gdb调试,因此需要记录coredump文件.设置查看:在root用户下执行sysctl -a | grep ...
- Bootstrap 表格
Bootstrap 提供了一个清晰的创建表格的布局.下表列出了 Bootstrap 支持的一些表格元素: 标签 描述 <table> 为表格添加基础样式. <thead> 表格 ...
- Android--保持加速度传感器在屏幕关闭后运行
由于写论文需要,需要用手机加速度采集数据,关于android加速度传感器的介绍网上一抓一大把,但大多都是大同小异,跟官网文档差不多.自己写了个取加速度传感器的APK,发现数据有点不对劲,原理屏幕一关后 ...
- IT男的”幸福”生活"续9
世界上最容易失去的便是时间了,我们总是蓦然回首,而时间早已流去. 曾经的种种,时时刻刻在我们脑中出现,让我们感到开心,快乐,幸福等. 有时好想有一种动冲,回到过去,再感受一下心中的那份触动. 又一年过 ...
- 分布式人工智能标记语言(DAIML)示例
DAIML(Distributed Artificial Intelligence Markup Language)是用于分布式人工智能系统中智能语言的标记库.DAIML主要分为Patte ...
- jQuery问题:$XXX is not a function
用火狐浏览器打开,js代码一段不执行,F12以后看见下面的错误: 网上查看说是jQuery文件引用的问题,把jQuery.js引入语句修改了一下,果然没有错了. 我原来的引用语句是:<scrip ...