Change soft code generator template to verify model data legitimacy

Source: Internet
Author: User

1. The first template determines whether a field is an empty class

Isnullabletype.cmt

Static public partial class Commontype
{
public static bool Isnullabletype (Type thetype)
{

Return (Thetype.isgenerictype && thetype.

GetGenericTypeDefinition (). Equals

(typeof (Nullable<>)));

}
}

2. The second template defines the type and length of a field

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. Third template Extended Properties

Customattribute.cmt

<summary>
Extension methods for attribute classes
</summary>
public static class CustomAttribute
{
<summary>
Determine if there is a corresponding feature
</summary>
<typeparam name= "T" > Feature class </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)
{

Determine if the attribute is Uniquecolumnattribute

if (attr is T)
{
return true;

}

}
return false;
}
<summary>
Gets the corresponding attribute object, such as Var attr=typeof (person). Getattribute<dbtablenameattribute> ();
</summary>
<typeparam name= "T" >attribute class </typeparam>
<param name= "Type" > entity class </param>
<returns>attribute Object </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>
Determine if there is a corresponding feature
</summary>
<typeparam name= "T" > Feature class </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)
{

Determine if the attribute is Uniquecolumnattribute

if (attr is T)
{
return true;

}

}
return false;
}
<summary>
Gets the corresponding attribute object, such as Var attr=typeof (person). Getattribute<dbtablenameattribute> ();
</summary>
<typeparam name= "T" >attribute class </typeparam>
<param name= "Type" > entity class </param>
<returns>attribute Object </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. The fourth template generates a 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;
}


}
}

The effect is as follows:

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;

The following code can be implemented by the factory method, here is just to provide ideas, I hope you don't mind
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;
}
}

The calling method is as follows:

Commonclass.order order111 = new Commonclass.order ()
{
Flag = 3,
Orderno= "asdfq1q324",
ClientName = "Zhang Molded asdf"
};
if (order111. Allisvalid ())
{
}

Change soft code generator template to verify model data legitimacy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.