Use a generic set instead of datatable as the return value to implement object-oriented

Source: Internet
Author: User

During the meeting, master said that during data center reconstruction, we should try not to use datatable as the return value. Instead, we should use the generic set method to implement real object orientation. The problem was solved by checking the information and communicating with the students. I have some knowledge about generic collections.

I. Understanding of generic Sets

1. It exists in the namespace of system. Collection and generic.

2. generics are classes, structures, interfaces, and methods with placeholders (type parameters. That is to say, the data added to the generic set must conform to the specified type <t>. Otherwise, an error will be reported during compilation.

The above is the content of Design Model Training interns.

What are the advantages of generic sets compared with datatable?

1. When using datatable, you need to understand the structure of the database. For example, to obtain the value of a field, you must know the fields in the data table. Datatable. rows (1 ). item (2). However, you do not need to use a generic set. For example, if you want to obtain the user name, you can directly write mylist. item (0 ). username. here, item refers to the entity you want to obtain.

2. If there are thousands of data entries, datatable may encounter unexpected troubles. However, with generics, you can use cyclic statements to automatically obtain data. For I = 0 to mylist. Count-1, which greatly facilitates programmers

3. Generics conform to the object-oriented idea. Each piece of data is treated as an object in a generic set, and each field is treated as a different attribute of the object.

Therefore, we usually use a generic set as the return value, which is more convenient.


2. How should I apply it in actual programming? Here, I would like to write my own notes.

Here, we only convert the original returned value datatable type into a generic collection class, because each conversion step is the same.

Because data is often displayed on the display layer, we also need to use the returned entities in the display layer, that is, in the U, B, the return value may be used for layer D. Therefore, we abstract the conversion method into a class to put it on the entity layer (which can be established according to our habits) for each layer to call at any time.


Imports system. collections. genericimports system. reflectionpublic class converthelper 'of T as new is used to constrain T. It must contain an empty non-argument constructor public shared function converttolist (of T as {New}) (byval dT as datatable) as list (of T) 'defines a generic set and is used to store the returned set dim ds as new list (of T)' to define a variable, to get the returned set of entity type dim type as type = GetType (t) 'defines a temporary variable, used to store the attribute dim proname as string = string for each entity in the datatable. during the initial call of empty, the empty string defines the row entity dim Dr as datarow to traverse all data rows in the datatable, that is, all records for each DR in DT. rows instantiate an object in a generic set, that is, a set that records dim record as new t' and defines an attribute information. 'propertyinfo Property) and provides metadata (metadata) Access dim propertys () as propertyinfo = record. getType (). getproperties () 'Get all attributes of the object' traverse all attributes dim pro as propertyinfo for each pro in propertys' assign the attribute name to the Temporary Variable proname = pro at a time in each loop. name' uses the attribute name in the temporary traversal to check whether the record contains this column if DT. columns. when contains (proname) then 'contains this column, you must ensure that this column can be written to If pro. canwrite = false then continue for 'jump out directly, continue executing the loop' value dim value as object = DR (proname) 'Save the column name value if (value. tostring <> dbnull. value. tostring () then 'if it is not empty, the attribute assigned to the object' sets the attribute value of the specified object with the optional index value of the indexed attribute pro. setvalue (record, value, nothing) 'dynamically accesses the attribute end if next DS of an object through reflection during runtime. add (record) next return DS 'returns the object's generic set end functionend class

In the above Code, you also need to pay attention to a method: setvalue, which exists in the namespace system. reflection. Therefore, you need to reference it in advance.

When creating the conversion function, you must <t> restrict the type. If the client code tries to use a class that is not allowed by a certain constraint if the client code tries to use a type that is not allowed by a certain constraint to instantiate the class, a compilation error occurs. Here, we use an empty constructor new to constrain it.

Iii. return values

As a type of return value for our declared method, the return type can be a separate entity, that is, containing only one record, or returning a set (list) containing multiple records)

Example: As T

Or as List (of T)

4. Connect to the control to display data

When we connect to the datagridview control to display data, we only need to change our original data source to a declared list. At the same time, note that we should change the autogeneratecolumns attribute to true, otherwise, data cannot be displayed in the control.

There is a question: when a datatable is used, the name of the first row of the control can be directly displayed in the control by using the alias in the D-layer query statement, for example, if SQL = "select username as username where [email protected]" is set to list, data cannot be displayed and data cannot be loaded. After a long period of time, we still failed to find a reasonable reason. At last, we had to directly edit headertext on the u layer.


The above is the general set of learned knowledge. Through Data Center reconstruction and the use of three layers, combined with the design model, we found that we can learn a lot of new knowledge, increase a lot of knowledge, and gradually feel that programming is still very interesting. Continue to work!

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.