Just started in the machine room reconstruction with the most is the DataTable, before the beginning of the time to listen to everyone has been talking about generics, very curious about this thing, also want to know clearly with a DataTable can, why do not want to turn into a generic type? What good does it do? So, the study began ...
A DataTable is a grid of temporary data that is stored as a virtual table. When we use a DataTable, we select the data in the database directly from the D layer, return to the B-layer and U-layer, and when we return to the form, we need to match the U-layer text box with the field one by one in the table. If you're not careful, you'll write it wrong, and the data will be wrong. So the coupling of the database is too large, can not guarantee the security of the database.
For example, our students check the balance for this function, if using a DataTable:
<span style= "FONT-SIZE:18PX;" >if card. Rows.Count then txtclass.text = card. Rows (0) (5). ToString () txtName.Text = card. Rows (0) (3). ToString () txtdepartment.text = card. Rows (0) (8). ToString () txtgrass.text = card. Rows (0) (4). ToString () txtsex.text = card. Rows (0) (2). ToString () txtstate.text = card. Rows (0) (7). ToString () txtcash.text = card. Rows (0) (6). ToString () txtstuno.text = card. Rows (0) (1). ToString () End If </span>
This functionality can be implemented, but there are some imperfections:
1, must understand the structure of the database, the security of the database is destroyed.
2, in the code writing process is easy to write wrong, because we want to use the number of each field in the virtual table to correspond, and this error compiler does not prompt.
3, in the process of data transmission is a DataTable, no longer an entity, contrary to the three-layer thinking.
A DataTable is a weak type, and it is not possible to visually see the data type of a field. When there is less data, we can do it all, but if there is a lot of data, the DataTable will have a lot of unexpected trouble. So we convert the DataTable to generics.
Generics are classes, structs, interfaces, and methods that have placeholders (type parameters), which are placeholders for one or more types that are stored or used by classes, structs, interfaces, and methods. A generic collection class can use a type parameter as a placeholder for the type of formation it stores; the type parameter appears as the type of its field and the parameter type of its method.
So what are the benefits of generics?
1, reduce the input, transfer only need to pass an instance T can get any of its properties, convenient, take is a single object.
2, the correct construction of the generic class can really reduce the security problems in the code.
3. The use of generic classes can also improve performance.
A diagram of the core idea of a DataTable converted to a generic type:
An entity is a mapping of a table in a database, because each attribute in an entity corresponds to a field in a database table. Each row of records in a DataTable is treated as an entity class, the fields are read out, stored in the attributes of the entity class, and all the entity classes are stored in the generic collection, so how many records are in the DataTable, and how many entities are in the generic collection.
How does a specific DataTable turn into a generic type?
<span style= "FONT-SIZE:18PX;" >imports System.Collections.Generic ' Add a generic collection namespace Imports system.reflection ' Add reflection '/****************************** ' Class name: Modelhelper ' namespace: Jfdal ' creation time: 2015/7/13 11:31:37 ' Author: Shanyu ' Group: ' Modified: ' Modified by: ' Version number: v1.0.0 ' ************* ' The ability to convert a DataTable to generics public Class modelhelper public Shared function converttolist (of T as {New}) (ByVal DT as DataTable) As IList (Of T) ' gets the type of T dim type As Type = GetType (t) Dim ts as New List (Of T) ' defines a temporary variable Dim strtemp as String = String.Empty ' Iterates through all the rows in the table for each Dr as DataRow in DT. Rows ' Define the type variable act gets the type of the dynamic creation object T, Dim act as T = If (Nothing was nothing), activator.createinstance (Of T) () , nothing) ' reference reflection represents a collection of all the properties of the object that can be obtained Dim propertys as PropertyInfo () = Act. [GetType] (). GetProperties () ' defines the array variable, receives the attribute contained in the Propertys, and provides access to the property Propertys metadata to the Dim array as PropertyInfo () = Prop Ertys Dim intcount as Integer = 0 ' Traverse all object properties While intcount < array. Length ' length ' represents the sum of elements in all dimensions ' PR represents the attributes contained in the element and provides access to the data Dim PR as PropertyInfo = Array (intcount) strtemp = pr. Name ' column Name = object's property name If dt. Columns.contains (strtemp) Then ' determines whether this property sets the function if Pr. CanWrite then ' whether the property is writable Dim value As Object = DR (strtemp) ' If not NULL, the property assigned to the object If value IsNot DBNull.Value Then ' Set the property value of this object to PR. SetValue (Act, value, nothing) End If End If End if intcount + = 1 Continue when End while ' adds an object to the generic collection in TS. ADD (ACT) Next Return ts End functionend class</span>
Then add a code that calls this method after the D layer returns to the DataTable.
<span style= "FONT-SIZE:18PX;" > table = sqlhelper.execselect (sql, CommandType.Text, sqlparam) list = Jfdal. Modelhelper.converttolist (of jfentity.cardentity) (table) </span>
Summary: The first time you use a DataTable can also be satisfied with us, but in the back can not satisfy us, we will do new exploration, and then understand and use generics. That's the spirit we don't have!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Computer room toll System reconstruction--datatable turn generic type