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. 解决 php-cgi 启动时提示缺少 msvcr110.dll 的问题

    问题描述: 启动 php-cgi 时如果提示缺少 msvcr110.dll 问题原因: 缺少 vc 2012 运行库   问题解决: 需要安装 vcredist_x64.exe 或 vcredist_ ...

  2. IC系统组成概论

    IC系统是什么? 对算法工程师来说,IC系统是完成特定功能的硬件.对架构设计师来说,IC系统包括控制,运算,存储部分.电路设计工程师来说,IC系统是加法器,乘法器,与非门,运算放大器,开关电容等的搭配 ...

  3. C语言 百炼成钢1

    //题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> ...

  4. 多个相同jar存在时的引用顺序

    起因:今天一个aar包在测试环境中正常运行,使用soapui测试正常返回,在本地环境中运行则老是报数据库连接异常,经检查,是因为在运行时环境中缺少ojdbc相关的jar包引起的. 重新打了一个aar包 ...

  5. Memcached通用类(基于enyim.com Memcached Client)

    一.如果用官方提供的方法,在web.config里面配置好了各个参数和服务器IP.如下图: <?xml version="1.0"?> <configuratio ...

  6. 基于IHttpAsyncHandler的实时大文件传送器

    在日常工作中,有时候需要到远程服务器上部署新版本的系统,由于远程服务器出于外网,所以每次都要开QQ连接,非常麻烦.索性就研究了下IHttpasyncHandler,并结合Juqery Progress ...

  7. 面试准备(三) Java 异常类层次结构

    在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中.这类容易出选择题 考试你是否掌握了异常类并清楚哪些异常类必须捕获 下面的图展示了Java异常类的继承关系. 图1 粉红色的 ...

  8. Linux内核分析——第八周学习笔记20135308

    第八周 进程的切换和系统的一般执行过程 一.进程切换的关键代码switch_to分析 1.进程调度与进程调度的时机分析 (1)进程分类 第一种分类 I/O-bound:等待I/O CPU-bound: ...

  9. 创建Maven工程

    一.Maven工程创建 File->New->Other,进入: 点击Next,进入: 勾选上Create a simple project(不使用骨架) 点击Next,进入: 输入项目名 ...

  10. 再次遇到\r\n转\r问题

    帮助小伙伴做jenkins的环境搭建.以为5分钟的事情,但是发现了一个诡异的问题.总是提示SVN的url不合法“URL '%s' is not properly URI-encoded”. 由于选择了 ...