IDataRowPersistable

接口定义:

1 Public Interface IDataRowPersistable
2     ReadOnly Property PrimaryKey() As Object
3     Sub Save(ByVal row As DataRow)
4     Sub Load(ByVal row As DataRow)
5 End Interface
6 

实现接口的类:

代码
 1 Public Class Student
 2     Implements IDataRowPersistable
 3 
 4     Property FirstName As String
 5     Property LastName As String
 6     Property BirthDate As Date
 7 
 8     Private ID As Guid = Guid.Empty
 9 
10     Public Sub New()
11     End Sub
12 
13     Public Sub New(ByVal firstName As StringByVal lastName As StringByVal birthDate As Date)
14         Me.FirstName = firstName
15         Me.LastName = lastName
16         Me.BirthDate = birthDate
17     End Sub
18 
19     Public ReadOnly Property PrimaryKey() As Object Implements IDataRowPersistable.PrimaryKey
20         Get
21             If Me.ID.Equals(Guid.Empty) Then Me.ID = Guid.NewGuid()
22             Return Me.ID
23         End Get
24     End Property
25 
26     Public Sub Load(ByVal row As DataRow) Implements IDataRowPersistable.Load
27         Me.ID = CType(row("ID"), Guid)
28         Me.FirstName = CStr(row("FirstName"))
29         Me.LastName = CStr(row("LastName"))
30         Me.BirthDate = CDate(row("BirthDate"))
31     End Sub
32 
33     Public Sub Save(ByVal row As DataRow) Implements IDataRowPersistable.Save
34         If DBNull.Value.Equals(row("ID")) Then row("ID"= Me.ID
35         row("FirstName"= Me.FirstName
36         row("LastName"= Me.LastName
37         row("BirthDate"= Me.BirthDate
38     End Sub
39 End Class
40 

用法:

代码
 1 Module Module1
 2     Sub InterfacesAndPolymorphism()
 3         Dim table As New DataTable()
 4 
 5         Dim idCol As DataColumn = table.Columns.Add("ID"GetType(Guid))
 6         table.PrimaryKey = New DataColumn() {idCol}
 7 
 8         table.Columns.Add("FirstName"GetType(String))
 9         table.Columns.Add("LastName"GetType(String))
10         table.Columns.Add("BirthDate"GetType(Date))
11 
12         Dim students() As Student = {
13             New Student("John""Doe", #1/2/1965#),
14             New Student("Ann""Doe", #8/17/1972#),
15             New Student("Robert""Smith", #11/1/1973#)}
16 
17         SaveObjects(table, students)
18 
19         Dim studArray(table.Rows.Count - 1As Student
20         For i As Integer = 0 To studArray.Count - 1
21             studArray(i) = New Student()
22         Next
23 
24         LoadObjects(table, studArray)
25     End Sub
26 
27     Sub SaveObjects(ByVal table As DataTable, ByVal array() As IDataRowPersistable)
28         ' Retrieve the primary key name. (Multiple column keys aren't supported.)
29         Dim pkName As String = table.PrimaryKey(0).ColumnName
30 
31         Dim dataView As New DataView(table)
32         dataView.Sort = pkName
33 
34         For Each obj As IDataRowPersistable In array
35             Dim row As DataRow
36             Dim rowIndex As Integer = dataView.Find(obj.PrimaryKey)
37 
38             If rowIndex >= 0 Then
39                 row = table.Rows(rowIndex)
40             Else
41                 row = table.NewRow()
42             End If
43             obj.Save(row)
44             If rowIndex < 0 Then table.Rows.Add(row)
45         Next
46     End Sub
47 
48     Sub LoadObjects(ByVal table As DataTable, ByVal array() As IDataRowPersistable)
49         For i As Integer = 0 To table.Rows.Count - 1
50             Dim row As DataRow = table.Rows(i)
51             array(i).Load(row)
52         Next
53     End Sub
54 End Module
55 
原文地址:https://www.cnblogs.com/cuishengli/p/1719797.html