How to convert a able into a generic set in the vb.net Environment

Source: Internet
Author: User

How to convert a able into a generic set in the vb.net Environment
Background

This problem occurs when the data center charging system is used. a row of data in the database table is taken out and the content in each cell is filled in the form.

. Considering the complexity of using DataReader to obtain results, I used a generic set. However, this is a problem.

Question.

Question 1:

This problem occurs because the field type of the table is inconsistent with the field type of the object when the DataTable is converted to the object type. This can also be used.

In this case, Chinese children all follow the father's surname. This is a tradition. However, there is something wrong with the child's surname.

Problem 2: The field names in the database table are inconsistent with the object attribute field names to be converted. As a result, depart in the converted object is null.

 

Conversion Principle

Analysis: the reason for these problems is that I do not quite understand the principle of converting able to object type. After checking the information and adding your own understanding

The conversion method is mastered, saving a lot of effort in coding. First, let's look at the schematic diagram of the conversion process.

Principle: each row of data in a database table is first converted into an entity class, and then converted into a generic set.

Generic Set application implementation function: load the data in the database table to the corresponding form text box

Code implementation (1) Create an entity conversion process at the Entity Layer
'*************************************** * *********** 'Description: generic set. After the able type is converted to the object class, Author: Wang ju' creation date: 11:08:02 'version: v1.00 '************************************** * ************* Imports System. collections. genericImports System. reflectionPublic Class EntityMoudule converts a able to a generic set Public Shared Function converToList (Of T As {New}) (ByVal dt As datatable) As IList (Of T) dim mylist As New List (Of T) 'defines the final returned set Dim mytype As Type = GetType (T) 'Get object class type name Dim dr As datarow' defines the row set Dim TmpName 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 an object class Object Dim propertys () As PropertyInfo = myT. getType (). getProperties () 'define the property set Dim pr As propertyinfo' to traverse all properties of this object For Each pr In propertys TmpName = pr. name' assign the attribute Name to the global variable 'and check whether the datatable contains this column. The column Name = The property Name of the object If (dt. columns. contains (TmpName) Then 'compares this attribute with the column name of the datatable, and checks whether the datatable Contains this attribute' to determine whether the property has a setter class 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 (TmpName) appears) 'define an object column to save the column value If (value. toString <> DBNull. value. toString () Then 'determines whether it is null. If it is not null, it is assigned to the property pr of the object. setValue (myT, value, Nothing) 'dynamically accesses a dynamic attribute End If Next mylist through reflection during runtime. add (myT) 'add the myT object to the set Next Return mylist' and Return the object set End FunctionEnd Class.

(2) Implement code in layer D
Public Class BasicDataBLL        Public Function BasicDataList() As IList(Of Entity.BasicDataEntity)        Dim Ibasicdata As IDAL.IBasicData        Dim factory As New Factory.BasicDataFac        Ibasicdata = factory.CreateIbasicdata()        Dim mylist As IList(Of Entity.BasicDataEntity)        mylist = Ibasicdata.SetDataList()        Return mylist    End FunctionEnd Class

 

(3) define an interface at the Interface Layer

Public Interface IBasicData    Function SetDataList() As IList(Of Entity.BasicDataEntity)End Interface

 

(4) create an interface at the factory Layer
Public Class BasicDataFac    Dim AssemblyName As String = "DAL"    Dim db As String = ConfigurationSettings.AppSettings("DB")    Function CreateIbasicdata() As IBasicData        Dim Ibasicdata As IBasicData        Ibasicdata = CType(Assembly.Load(AssemblyName).CreateInstance(AssemblyName + "." + db + "BasicDataDAL"), IBasicData)        Return Ibasicdata    End FunctionEnd Class

 

(5) access layer D interface methods in layer B
Public Class BasicDataBLL    Public Function BasicDataList() As IList(Of Entity.BasicDataEntity)        Dim Ibasicdata As IDAL.IBasicData        Dim factory As New Factory.BasicDataFac        Ibasicdata = factory.CreateIbasicdata()        Dim mylist As IList(Of Entity.BasicDataEntity)        mylist = Ibasicdata.SetDataList()        Return mylist    End FunctionEnd Class

(6) The appearance layer receives the generic set in layer B.
Public Class BasicDataFacade    Public Function BasicDatalist() As IList(Of Entity.BasicDataEntity)        Dim mylist As IList(Of Entity.BasicDataEntity)        Dim setdata As New BLL.BasicDataBLL        mylist = setdata.BasicDataList()        Return mylist    End FunctionEnd Class
(7) U layer: fill data in the generic set into the form
Private Sub frmSetBasic_Load (sender As Object, e As EventArgs) Handles MyBase. load Balance commit. enabled = false' makes sure that the confirm button cannot use GroupBox1.Enabled = false' data cannot be modified into cancle. enabled = false' cancel unavailability 'dim mylist As IList (Of Entity. basicDataEntity) Dim loaddata As New Facade. basicDataFacade mylist = loaddata. basicDatalist () Dim num As Integer num = mylist. count-1 'num indicates the last row of txtFixed in the table. text = mylist (num ). fixedUser txtCasual. text = mylist (num ). casualUser txtIncrease. text = mylist (num ). incrementOfTime txtAtLeast. text = mylist (num ). atLeastTime txtPrepare. text = mylist (num ). prepareTime txtLeast. text = mylist (num ). minCharge End Sub
(8) object type attributes
Public Class BasicDataEntity 'General user attribute Private _ userId As String Public Property UserId As String Get Return _ userId End Get Set (value As String) _ userId = value End Set End Property 'minimum amount attribute Private _ minCharge As Decimal Public Property MinCharge As Decimal Get Return _ minCharge End Get Set (value As Decimal) _ minCharge = value End Set End Property 'Preparation time Property Private _ prepareTime As Integer Public Property PrepareTime As Integer Get Return _ prepareTime End Get Set (value As Integer) _ prepareTime = value End Set End Property 'least machine time Property Private _ atleastTime As Integer Public Property AtLeastTime As Integer Get Return _ atleastTime End Get Set (value As Integer) _ atleastTime = value End Set End Property 'unit increment time attribute Private _ incrementOfTime As Integer Public Property IncrementOfTime As Integer Get Return _ incrementOfTime End Get Set (value As Integer) _ incrementOfTime = value End Set End Property 'fixed user Property Private _ fixedUser As Decimal Public Property FixedUser As Decimal Get Return _ fixedUser End Get Set (value As Decimal) _ fixedUser = value End Set End Property 'temporary user Property Private _ casualUser As Decimal Public Property CasualUser As Decimal Get Return _ casualUser End Get Set (value As Decimal) _ casualUser = value End Set End PropertyEnd Class

 

 

Summary

Compared with DataReader, generic conversion is easier to process data and more operable. That is, after conversion at Layer D

It can read data and optimize the code structure to simplify the business logic.

 

 

 

Related Article

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.