A small open-source class library is used to flexibly copy attributes between objects and convert idatareader to object classes.

Source: Internet
Author: User

Function 1:

Background:

This requirement often exists in programming. In two very similar entity classes, copy the value of the attribute with the same name (for example, copying in the WCF entity and EF entity ...)

In the past, there were generally two solutions

1. Hard coding: The execution efficiency is very high, but it requires a lot of repetition.Code,

2. Reflection: flexible, but inefficient

 

Here provides a solution https://github.com/xwj90/Clover.Copyer with no less flexibility than reflection

The code is very simple, as shown below, there is only one sentence

 //Example 1 directly copy attributes on two objects
Classa target =NewClassa ();
Classb source =NewClassb () {A ="AA", B =2, C = datetime. Now };

//Copy code
Copyhelper <classb, classa>. Copy (source, target );//This is the call method.

Console. writeline (target. );
Console. writeline (target. B );
Console. writeline (target. C );

The two types are defined as follows:

    Public   Sealed   Class Classa
{

Public String A { Get ; Set ;}
Public Int B { Get ; Set ;}
Public String Title {Get ; Set ;}
Public Int Id { Get ; Set ;}
Public Datetime c { Get ; Set ;}

}
Public Sealed Class Classb
{
Public String A { Get ; Set ;}
Public Int B { Get ; Set ;}
Public Datetime c { Get ; Set ;}
}

Note: All types of public instance attributes are supported.

The indexer is not supported.

 

 

Function 2:

Read the corresponding value from the idatareader provided by the database and convert it to the corresponding type and assign it to the object class,

Saving the dataset process in the middle, and saving a lot of work on type conversion and hard coding.

First look at the Code:

             //  Example 2 read attributes from idatareader  
String Connstring = " Data Source =.; initial catalog = northwind; persist Security info = true; " ;
For ( Int I = 0 ; I < 1 ; I ++)
{

Using (Sqlconnection conn = New Sqlconnection (connstring ))
{
Sqlcommand command = New Sqlcommand ();
Command. Connection = conn;
Command. commandtext = " Select Top 1000 * From clog " ;
Conn. open ();
Idatareader reader = command. executereader ();
VaR List = copyhelper <testclassa>. Copy (Reader ); // This is the call method.
Foreach ( VaR Item In List)
{
Console. Write (item. logid + " --- " );
Console. Write (item. logtime + " --- " );
Console. Write (item. appname + " --- " );
Console. Write (item. SERVER + " --- " );
Console. Write (item. IPaddress + " " );
Console. writeline ();
}
}
}

Note: All basic types are supported. The order of the columns returned each time is the same. Do not select a, B from table for a while and then select B, A from table for a while (Automatic Detection consumes a lot of performance,)

 

Implementation principle:
Use the Reflection Analysis type for the first time, and then use ilemit to generate a dynamic method

Cache the dynamic method. This method can be used directly for the second time.

 

Performance:

Basically, the efficiency ratio is about

Hard coding: dynamic method: Reflection

--1000 (about 1-30 attributes in the class during actual testing)

 

 

Please also provide comments

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.