C # Generic Analysis of Data Center reconstruction

Source: Internet
Author: User

C # Generic Analysis of Data Center reconstruction
Preface

We all know that in the reconstruction of the data center, the type of the returned value of the Three-layer video that we first watched is entity. Later, with SqlHelper, the returned value type is changed to Datatable, what is the relationship with the generic type that we want to talk about today? In other words, why do we need to use generic products for a long time?

Principle

The entity class is the ing of the database. Therefore, the attributes in the Entity class correspond to fields in the database table. Treat each row of record in the DataTable as an entity class, read the fields from the record, store the fields in the attributes of the entity class, and store all object classes in a generic set. Therefore, the number of records in the DataTable and the number of entity classes in the generic set correspond to the attributes of each object class and the fields in the DataTable.

Advantages

1. Writing B-layer personnel do not need to manually fill in the required fields. Simply click the button and all the prompts are displayed. If you want to use the field, no errors will occur.

2. Do not understand the database structure.

3. Conforming to the object-oriented idea.

4. The attributes of the object class are strongly typed. The type of each field is known.

Notes

1. the attribute name of the object class must be the same as the field name in the database table.

2. to use this method to convert a able to an object class, you must have a known object class that corresponds to the data in the DataTable. That is to say, you must specify the object class type you want to convert, otherwise, you cannot specify the type of the generic set, and you cannot call it.

3. Try to differentiate the fields of each table. For example, table A has A name field and table B also has A name field, which is unreasonable.

Reference Code

After talking about this, how can we convert Datatable to generics? Let's look at the code first.

 

Using System; using System. collections. generic; using System. linq; using System. text; using System. data; using System. collections; using System. reflection; namespace SqlDAL {public class ConvertHelper {public List
 
  
ConvertToList
  
   
(DataTable dt) where T: new () {// defines the List of Sets
   
    
Ts = new List
    
     
(); // Obtain the model Type type = typeof (T); // define a temporary variable string tempName = string. empty; // traverses all data rows in the DataTable foreach (DataRow dr in dt. rows) {T t = new T (); // obtain the public attributes of this model. PropertyInfo [] propertys = t. getType (). getProperties (); // traverses all attributes of the object. foreach (PropertyInfo pi in propertys) {tempName = pi. name; // assign the attribute Name to the Temporary Variable // check whether the DataTable contains this column (column Name = Object attribute Name) if (dt. columns. contains (tempName) {// determines whether this property has a Setter if (! Pi. canWrite) continue; // This attribute cannot be written, jump out directly // value object value = dr [tempName]; // if it is not empty, the property if (value! = DBNull. value) pi. setValue (t, value, null) ;}// Add the object to ts in the generic set. add (t) ;}return ts ;}}}
    
   
  
 

This is the conversion from datatable to generic ConvertHelper. This is common and some friends have asked, So how should I write it on layer D?

 

 

DataTable table = SqlDAL. SQLHelper. getDataTable (SQL, CommandType. text, sqlParams); // call the method if (table. rows. count> 0) {ConvertHelper ctl = new ConvertHelper (); List
     
      
List = new List
      
       
(); List = ctl. convertToList
       
        
(Table); return list;} else {return null ;}
       
      
     

Now, the generics have been converted successfully. In the U layer, we need to call out the data in the generic set.

 

 

txtRate.Text = list[0].Rate.ToString();txtTmpRate.Text = list[0].TmpRate.ToString();txtPrepare.Text = list[0].PrepareTime.ToString();txtLimit.Text = list[0].LimitCash.ToString();lblModUser.Text = list[0].Head.ToString();

This is the code for extracting data from the U layer.

 

Summary

There are many ways to develop a function. Our task is not only to master these methods, but also to analyze these methods, distinguish these methods, and find their respective advantages and disadvantages, in this way, these methods can be used better.

 

 

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.