您的位置:首页 > 编程语言

更改动软代码生成器模板 验证Model数据合法性

2015-12-17 18:38 363 查看
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())
{
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: