Datatable data to entity

Source: Internet
Author: User

When writing software with a three-tier architecture, we often encounter the following problems: parameter transmission between three layers: If we query data in layer D as a datatable type, when we use this data in layer B or even the U layer, we need to use the able type to transmit it back and forth. In any case, we will inevitably enter the read field. For example, we need to use a field of the first record, and the code needs to write: MRC. Count (*) rows (*). There are many disadvantages in writing this:

1. It is easy to write errors, and the compiler cannot check the errors;

2. We need to have a detailed understanding of the database structure;

3. It does not conform to the object-oriented programming idea.

After studying this problem for a long time, I found countless materials and finally found a solution. Converts datatable data into a single entity class, and puts these entity classes in a generic set. The result is as follows:

 

The entity class is the ing of the database. Each record corresponds to an entity. The attributes of the entity correspond to the fields of each record and correspond to each other one by one. Here we extract each piece of data queried as an entity and store these entities in a generic set. In this way, we only need to know the attribute when using data. The Code is as follows: List. Items (1). attribute name. In this way, the code is simplified, the workload is reduced, and the error rate is also reduced.

So how is Code implemented? The first is the entity class. Here, we assume that the database has only two fields: user and password:

Public Class UserPublic UserName As StringPublic PassWord As StringPublic Property _username() As String        Get            Return UserName        End Get        Set(value As String)            UserName = value        End Set    End Property    Public Property _password() As String        Get            Return PassWord        End Get        Set(value As String)            PassWord = value        End Set    End PropertyEnd Class
Here, I use a modelhelper class to implement this function, because this is a parameter class and this class is placed at the model layer. The Code is as follows:
Imports system. collections. generic 'namespace imports system. reflection 'introduce reflection: it is easy to use propertyinfo ''' <summary> ''' entity conversion class, this class is used to convert a data table to an object set ''' </Summary> ''' <remarks> </remarks> public class modehelper public function converttolist (of T as {New }) (byval dT as datatable) as ilist (of T) 'converts a able to a generic set '1converttolist (of T as {New}) where new is used to constrain parameter T, otherwise, the parameter following '2list is always of the (of +) type dim mylist as new list (of T) 'type returned value set dim mytype as type = GetType (t) 'define the object class type name dim Dr as datarow' defines the row set dim tempname as string = string. empty defines a temporary variable used to store a 'data table is always a two-dimensional table. Arrays: DR and PR are required. dt indicates that sqlhelper returns the result for each DR in DT. rows 'traverse all records of the datatable dim MYT as new t' defines an object dim propertys () as propertyinfo = MYT. getType (). getproperties () 'defines the property set and obtains the Public Property dim PR as propertyinfo for each PR in propertys' to traverse all the fields of the datatable tempname = Pr. name' assign the attribute name to the Temporary Variable 'and check whether the datatable contains this column (column name = Object attribute name) if (DT. columns. contains (tempname) then' compares this attribute with the column name of the datatable to check whether the datatable contains this column if (PR. canwrite = false) then' determines whether this property has setter continue for 'continue to execute end if Dim value as object = DR (tempname) 'define an object variable to save the column value if (value. tostring <> dbnull. value. tostring () then' if it is not null, the property PR of the object is paid. setvalue (MYT, value, nothing) 'during runtime, through reflection, dynamic access to an object's attribute end if next mylist. add (MYT) 'add to collection next return mylist' return result end functionend class
The following is the calling code of layer D:
Public Function selectusers1 (User) as List (of charge. model. user) dim MRC as able 'assume that MRC is a able data table dim mylist as List (of charge. model. user) 'defines a set to return the converted entity set dim mhelper as new charge. model. modehelper instantiate an object conversion class mylist = mhelper. converttolist (of charge. model. user) (MRC) 'calls the method of object conversion class and converts data return mylist' to return the result end Function
Here, we will only discuss the problem of converting the datatable data type. We will not discuss other issues. We will take assumptions as the premise. We should be cautious when referring to the code.
So far, these codes have solved the problem I encountered, but think carefully, here is a record of an object corresponding to the database, that is, each table corresponds to an object class or a generic set. But if multiple tables are used for joint queries, what should we do? I have not solved this problem yet. I will leave it for future solutions.






Datatable data to entity

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.