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. [1]Telerik Extensions for ASP.NET MVC 中文教程(转)

    http://demos.telerik.com/aspnet-mvc/ Telerik Extensions for ASP.NET MVC 是Telerik 公司专门针对Asp.net MVC 开 ...

  2. usb驱动开发10之usb_device_match

    在第五节我们说过会专门分析函数usb_device_match,以体现模型的重要性.同时,我们还是要守信用的. 再贴一遍代码,看代码就要不厌其烦. static int usb_device_matc ...

  3. SRTM数据介绍与说明

    一.SRTM 的背景引言 美国利用航天飞机搭载成像雷达对地进行观测始于20 世纪80 年代初.1982 年11 月和1985 年10 月, 美国分别进行了两次称为S IR2A 与S IR2B 的航天飞 ...

  4. [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

    3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...

  5. Jsp内置对象及EL表达式的使用

    一.JSP的内置对象(9个JSP内置对象) JSP的内置对象引用名称 对应的类型 request HttpServletRequest response HttpServletResponse ses ...

  6. HTC Vive 体验的折腾经历

    HTC Vive 是个什么东西, 想必我就不用介绍了, 不知道自己百度吧 HTC Vive发布已经有一段时间了, 一直很纠结买还是不买, 这玩意太贵(官网6888),买了还不能直接用, 还要配太高性能 ...

  7. js中的运动

    缓慢隐藏与出现 效果: 鼠标移至分享上黄色区域自动向左隐藏. <!DOCTYPE html> <html> <head> <title></tit ...

  8. 编写高质量代码改善C#程序的157个建议[避免finaly内的无效代码、避免嵌套异常、避免吃掉异常、注意循环异常处理]

    前言 本文已同步到http://www.cnblogs.com/aehyok/p/3624579.html.本文主要来学习以下几点建议 建议61.避免在finally内撰写无效代码 建议62.避免嵌套 ...

  9. EntityFramework_MVC4中EF5 新手入门教程之七 ---7.通过 Entity Framework 处理并发

    在以前的两个教程你对关联数据进行了操作.本教程展示如何处理并发性.您将创建工作与各Department实体的 web 页和页,编辑和删除Department实体将处理并发错误.下面的插图显示索引和删除 ...

  10. javascript继承(七)—用继承的方式实现照片墙功能

    照片墙DEMO下载 注意:图片有四种类型:1可放大:2可拖动:3既可放大也可拖动:4都不行.由于每个图片的构造函数不同而不同(目前在火狐上调试的,其它的浏览器可能不行,请见谅,主要讲继承的思想.以后会 ...