Use of generic sets, use of generic Sets

Source: Internet
Author: User

Use of generic sets, use of generic Sets

The restructuring of the data room charging system is nothing more than adding, deleting, modifying, and querying four database operations. In addition, we use a three-tier architecture for reconstruction, and layer D is used to deal with databases. To perform these four operations, we need to return values. addition, deletion, and modification are not mentioned here, but when we perform queries, layer d needs data to be sent to layer B, so what form should we return the best?

When I first came into contact with Layer 3, I returned a datatable, dt. rows [0] ["xxx"] is very powerful when it is realized. Later, I communicated with you and found that there is actually a better way to use it, is to use the object class to fill the generic set.

The problem arises:

1. What is a generic set?
2. Why is it better to use it? 1. What is it?

Generic, as the name implies, is a generic type. That is, there is no definite type. How to Use it if there is no definite type? In fact, you can define the type when using it.

A set is a class that processes multiple data types. Generally, you can use different forms of the same set in multiple applications. Instead of creating a set based on the draft, you need to create a prototype using the generic type ).

In use, replace T in List <T> Angle brackets with the corresponding type based on the data type to be processed, and create the corresponding instance.

2. Why?

First, you need to add the following Imports statement to the Code:

Imports System. Collections. Generic


After the Imports statement is added, you can create a generic class. Basic classes look very common. You can use attributes, functions, subprograms, fields, or anything else in the class.

Public Class ConvertHelperEntity Public Shared Function convertToList (Of T As {New}) (ByVal dt As DataTable) As IList (Of T) 'convert able to generic set Dim myList As New List (Of T) 'to define the final returned set Dim myType As Type = GetType (T) 'Get object class type name Dim dr As datarow' defines the row set Dim tempName As String = String. empty 'defines a temporary variable 'to traverse all data rows In the datatable For Each dr In dt. rows Dim myT As New t' defines the object Dim propertys () As PropertyInfo = myT of an object class. getType. getProperties () 'define the property set Dim pr As propertyinfo' to traverse all properties of this object For Each pr In propertys tempName = pr. the name' assigns the property Name to the Temporary Variable 'and checks whether the datatable contains this column (column Name = object property Name) If (dt. columns. contains (tempName) Then 'compares this attribute with the column name in the datatable to check whether the datatable Contains this attribute' to determine whether the property has a setter If (pr. canWrite = False) then' determines whether this attribute can be written. If it cannot be written, the loop Continue For End If Dim value As Object = dr (tempName) appears) 'define an object-type variable to save the column value If (value. toString <> DBNull. value. toString () then' if it is not null, the property pr is assigned to the object. setValue (myT, value, Nothing) 'dynamically accesses the attribute End If Next myList of an object through reflection during runtime. add (myT) 'add to the set Next Return mylist' returns the object set End FunctionEnd Class
We need to add an object class in the object Layer, because the basis for creating a set is to have a class before we can put the object into the set.

So why do we say it is better than datatable? With a generic set, we convert DataTable into a single object class at Layer D, and then fill the object class in the generic set.
Its core concept diagram:


Comparison of its advantages and disadvantages can be found:

Datatable

List <T>

It is very easy to write errors, and the compiler does not check

Click it and click it.

Must understand the database structure

No need to understand the database structure

Not in line with Object-oriented Thinking

In line with Object-oriented Thinking

The data type of the field cannot be intuitively seen because the DataTable is weak.

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

By learning generic sets, you can really understand that a program may not be the most important to implement functions. How to Make the Code the simplest, easier to expand, and how to think for others the most, this is what we are going to do.


Java generic set application and Method

Define a generic class
Import java. util .*;
/**
* Description:
* <Br/> Copyright (C), 2005-2008, Yeeku. H. Lee
* <Br/> This program is protected by copyright laws.
* <Br/> Program Name:
* <Br/> Date:
* @ Author Yeeku. H. Lee kongyeeku@163.com
* @ Version 1.0
*/
// The generic declaration is used to define the Apple category.
Public class Apple <T>
{
// Use a T-type parameter to define attributes
Private T info;

Public Apple (){}
// Use the T-type parameter to define the method in the following method:
Public Apple (T info)
{
This.info = info;
}
Public void setInfo (T info)
{
This.info = info;
}
Public T getInfo ()
{
Return this.info;
}
Public static void main (String [] args)
{
// Because the actual String type is passed to the T-shaped parameter, the constructor parameter can only be String
Apple <String> a1 = new Apple <String> ("Apple ");
System. out. println (a1.getInfo ());
// Because the actual type of the parameter passed to the T-shaped parameter is Double, the constructor parameter can only be Double or double.
Apple <Double> a2 = new Apple <Double> (5.67 );
System. out. println (a2.getInfo ());
}
}

The role of generics:
For example, in the List collection class, List can be put into various types of data. How can we let the system know what type of data is stored in List? In this case, you need to use a generic type, such as List <String> listString = new ArrayList <String> (); in this way, you can specify that listString stores String-type data.

There are a lot of generic content. I recommend Li Gang's crazy java handout for a book. I also use it to learn it myself.

C # What is a set of generics and generics?

Generic sounds like a deep term, but in fact its function is very simple, it is to improve the performance of the c # program.
For example, some data structures, such as queues and linked lists, are often used in computers, and the elements are previously defined as follows: object a = new object ();
In this way, there is a serious problem. It is no logical problem to use objects to indicate elements. However, each unpacking and sealing occupy a large amount of computer resources, resulting in low program performance, this part of content is generally the core part of the program. If the object is used, the program performance is worse.
The use of generics solves this problem well. In essence, the compiler tells the compiler about the types of elements in the data structure at the compilation stage. Since the compiler knows the types of elements, naturally, the unpacking and sealing operations are avoided, which significantly improves the performance of the c # program.
For example, List <string> directly uses the string object as the List element, and avoids the binning and unboxing operations caused by the object, thus improving program performance.
You can compile a small example to see how the data structure is used.

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.