In the process of refactoring to see a lot of people are using generics, and then feel that they have to try to use generics or no experience how will know good use!
The concept of generics is actually seen in the appendix to the book "Big Talk Design mode", but it didn't seem to be a serious understanding of the term.
Generics are methods of classes, structs, and interfaces that have placeholders (type parameters), which are placeholders for one or more types that are stored or used by classes, structs, interfaces, and methods. (excerpt from the book)
Through your own practice in the refactoring process, you feel some of the benefits of generics versus DataTable.
Generics it is very good to maintain the seven-tier structure we are involved in, we do not need to be too clear about the design of the database, but when the DataTable returns to the U-Layer pass value when the database is not aware of the error will be passed the wrong value, so that is not a generic security higher.
Of course, in the process of refactoring the use of generics is only a little fur, because we use a DataTable, so we need a transformation of the public class, like SqlHelper, so you can put it on the D layer:
The code is as follows:
' ********************************************** ' text name: Converthelper ' namespaces: DAL ' inside capacity: ' work can: ' file relationship: ' make : Statement Cream ' Small Group: statement Cream ' Build Date: 2016-04-04 20:48:33 ' edition Ben :v1.0.0.0 ' Modify log: ' Copyright Note: ' ********************************************** imports System.Collections.Generic ' increasing the namespace of generics imports System.Reflection ' introducing reflection: Public Class Converthelper ' transforms a DataTable into a generic collection Public Shared Function Converttolist (Of T as {New}) (ByVal DT as DataTable) as IList (Of T) ' Note: converttolist (Of T A s {new}) here new is used to constrain T, must have, otherwise new T will appear error Dim myList As New List (Of T) ' defines the final returned collection Dim myType As Type = Get Type(T) ' Get the type name of the entity class Dim dr As DataRow ' define rowset Dim tempname as String = String.Empty ' Define a temporary variable ' traverse the DataTable All data rows for each Dr in Dt. Rows Dim MyT As New T ' defines an object for an entity class Dim Propertys () as PropertyInfo = Myt.gettype (). GetProperties () ' Defines the property collection Dim Pr as PropertyInfo ' to traverse all properties of the object for every Pr in Propertys Tempname = Pr.name ' assigns a property name to a temporary variable ' checks if the DataTable contains this column (the column name = = Property name of the object) if (dt. Columns.contains (tempname) Then ' Compare this attribute to the column name in the DataTable to see if the DataTable contains this property ' to determine if this property has a setter if (Pr.canwrite = False) Then ' determines whether this property is writable, if not writable, jumps out of this loop Continue for End I F Dim Value As Object = DR (Tempname) ' defines an object type variable to hold the value of the column If (value. ToString <> DBNull.Value.ToString () Then ' if not NULL, the property assigned to the object Pr.setvalue (MyT, Value, nothing) ' in Dynamic access to an object's properties during run time by reflection End If End If Next Mylist.add (MyT) ' Add to collection Next return myList ' return to Entity Collection End FunctionEnd ClassLayer D:
Public Class sqlserverbasicdatadal:implements Idal. Ibasicdata Private SqlHelper as New sqlhelper.sqlhelper public Function selectbd () as List (of Basicdataentity) Implements ibasicdata.selectbd Dim sql As String Dim dt As DataTable Dim list As New list (of Entity.basicdatae ntity) sql = "SELECT * from T_basicdatainfo" dt = SqlHelper. Execselectno (SQL, CommandType.Text) If dt. Rows.Count > 0 Then list = DAL. Converthelper.converttolist (of Entity.basicdataentity) (DT) return list Else return Nothing End If End Function
Summary: Although generics have various benefits but in the use of the process to pay attention to the data type of the field, such as date, time and other errors in the use of the process, in addition to the field name of the entity in the database is consistent with the field name!
Machine Room reconstruction of the DataTable to the generic type