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数据合法性的更多相关文章

  1. MVC 3 数据验证 Model Validation 详解

    在MVC 3中 数据验证,已经应用的非常普遍,我们在web form时代需要在View端通过js来验证每个需要验证的控件值,并且这种验证的可用性很低.但是来到了MVC 新时代,我们可以通过MVC提供的 ...

  2. (转)MVC 3 数据验证 Model Validation 详解

    继续我们前面所说的知识点进行下一个知识点的分析,这一次我们来说明一下数据验证.其实这是个很容易理解并掌握的地方,但是这会浪费大家狠多的时间,所以我来总结整理一下,节约一下大家宝贵的时间. 在MVC 3 ...

  3. <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解

    MVC 3 数据验证 Model Validation 详解  再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...

  4. MVC Model数据验证

    概述 上节我们学习了Model的数据在界面之间的传递,但是很多时候,我们在数据传递的时候为了确保数据的有效性,不得不给Model的相关属性做基本的数据验证. 本节我们就学习如何使用 System.Co ...

  5. .NET MVC model数据验证

    MVC提供了很方便的数据验证,只需要在model里加入相关的正则等,那么就会在前台里生成相关的验证脚本.需要引用两个js文件: jquery.validate.min.js jquery.valida ...

  6. 动软代码生成器 可用于生成Entity层,可更改模板 /codesmith 也可以

    动软代码生成器官方下载地址:http://www.maticsoft.com/download.aspx 教程:http://jingyan.baidu.com/article/219f4bf7dfd ...

  7. Ruby Rails学习中:User 模型,验证用户数据

    用户建模 一. User 模型 实现用户注册功能的第一步是,创建一个数据结构,用于存取用户的信息. 在 Rails 中,数据模型的默认数据结构叫模型(model,MVC 中的 M).Rails 为解决 ...

  8. 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,这种形 ...

  9. Java快速开发平台,JEECG 3.7.7闪电版本发布,增加多套主流UI代码生成器模板

    JEECG 3.7.7 闪电版本发布,提供5套主流UI代码生成器模板 导读 ⊙平台性能优化,速度闪电般提升           ⊙提供5套新的主流UI代码生成器模板(Bootstrap表单+Boots ...

随机推荐

  1. f2fs解析(六)

    f2fs中有对一个bitmap进行操作的函数,感觉很巧妙,和大家分享一下: 1333 static inline void f2fs_change_bit(unsigned int nr, char ...

  2. 记录使用gogs,drone搭建自动部署测试环境

    使用gogs,drone,docker搭建自动部署测试环境 Gogs是一个使用go语言开发的自助git服务,支持所有平台 Docker是使用go开发的开源容器引擎 Drone是一个基于容器技术的持续集 ...

  3. An Introduction to Interactive Programming in Python (Part 1) -- Week 2_3 练习

    Mini-project description - Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that ...

  4. 20145314郑凯杰《信息安全系统设计基础》GDB调试32位汇编堆栈分析

    20145314郑凯杰<信息安全系统设计基础>GDB调试32位汇编堆栈分析 本篇博客将对第五周博客中的GDB调试32位汇编堆栈进行分析 首先放上以前环境配置的图: 图1: 测试代码: #i ...

  5. [C/C++基础] C语言常用函数sprintf和snprintf的使用方法

    Sprintf 函数声明:int sprintf(char *buffer, const char *format [, argument1, argument2, …]) 用途:将一段数据写入以地址 ...

  6. Scala之类型参数和对象

    泛型 类型边界 视图界定 逆变和协变 上下文界定 源代码 1.泛型 泛型用于指定方法或类可以接受任意类型参数,参数在实际使用时才被确定,泛型可以有效地增强程序的适用性, 使用泛型可以使得类或方法具有更 ...

  7. hello-weapp 微信小程序最简示例教程

    打开微信小程序官方开发文档,最好全篇看一遍,基本上就会了. 点击文档中 工具 选项卡中 下载工具页面 下载对应系统版本的微信开发者工具 注意:脱离微信开发者工具是不能调试的 好了,安装下工具即可打开, ...

  8. jTemplate —— 基于jQuery的javascript前台模版引擎

    reference: http://blog.csdn.net/lexinquan/article/details/6674102     http://blog.csdn.net/kuyuyingz ...

  9. [wikioi2926][AHOI2002]黑白瓷砖(Polya定理)

    小可可在课余的时候受美术老师的委派从事一项漆绘瓷砖的任务.首先把n(n+1)/2块正六边形瓷砖拼成三角形的形状,右图给出了n=3时拼成的“瓷砖三角形”.然后把每一块瓷砖漆成纯白色或者纯黑色,而且每块瓷 ...

  10. [C#]Hosting Process (vshost.exe)

    写在前面 最近在群里,有朋友问起这个vshost.exe进程到底是什么?当时确实不知道是个什么东东,给人的感觉是,经常看到它,就是在启动一个项目的时候,经常看到它,就是没细研究它是啥玩意儿.既然遇到了 ...