Using attribute and reflection transitions from the data access layer to the business object-II

Source: Internet
Author: User
Tags foreach contains reflection
Introduction to Data

In the previous article I introduced the use of attributes to describe business objects. In this article I will show you how to extract descriptive information from a class. To explain how it works, I'll create a SQL script to generate the table using the attributes I used earlier in the class to write an application.

Tools

This is a console application. The tool will load the assembly and enumerate the attibute generated SQL scripts used by the class to create the table.

Start loading part of code:

public static void Main (string[] args)

{

if (args. Length!= 1)

{

Console.WriteLine ("Usage:scriptgen [assembly Path]");

Return

}





Assembly Assembly = null;



Try

{

Assembly = Assembly.LoadFrom (Args[0]);

}

catch (Exception e)

{

Console.WriteLine ("Failed to load assembly [" + Args[0] + "]");

Console.WriteLine (E.message);

}



}

The code above attempts to load the assembly with the parameter path. Now we have to enumerate all the components containing the DataTable. To complete this work, see the following code:

public void parseassembly (Assembly Assembly)

{

Type[] Types = assembly. GetTypes ();



foreach (type type in types)

{

if (type. IsClass)

{

Datatableattribute[] dataTable = (datatableattribute[])

Type. GetCustomAttributes (typeof (Datatableattribute),

true);



if (Datatable.length > 0)

{

Console.WriteLine ("Found class ' {0} '", type.) ToString ());

}

}

}

}

The following is the code that enumerates the properties.

void Parseclass (Type type, Datatableattribute dataTable)

{

Propertyinfo[] Properties = type. GetProperties ();



Gets the key field

foreach (PropertyInfo property in properties)

{

Keyfieldattribute[] key = (keyfieldattribute[])

Property. GetCustomAttributes (typeof (Keyfieldattribute),

true);



if (key. Length > 0)

{

Console.WriteLine ("Key =" + property. Name + ' type is '

+ property. PropertyType);

Break

}

}



Gets the other fields

foreach (PropertyInfo property in properties)

{

basefieldattribute[] field = (basefieldattribute[])

Property. GetCustomAttributes (typeof (Basefieldattribute),

true);



if (field. Length > 0)

{

if (!) ( Field[0] is Keyfieldattribute))

{

Console.WriteLine (property) + property. Name

+ "[" + property. PropertyType + "]" +

+ "Maps to column [

+ field[0]. ColumnName + "]");

}



}

}

}

Now we have to create the SQL script. Our tools only meet 2 requirements: Key is Int,identify property type only these are allowed: String,int,decimal, and DateTime.

The source file will create the following assembly:

DAL.dll: Contains attributes
Customer.dll: Contains Business objects
ScriptGen.exe: The tool used to generate SQL scripts.
Next

Next, I'll create the entire DAL, which is used to get objects at run time and so on.




Related Article

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.