以我的经验,应用层应该引用业务层,业务层应该引用数据层。我想对应用程序进行更改,以便应用程序层直接引用数据层,如下所示:
Imports System.Data.SqlClient
Public Class ApplicationLayerClass
Public Function ProcessAllPersons()
Dim data As New DataLayerClass
Dim list As List(Of Person) = data.getAllPersons()
For Each person In list
'Call this function from the application client. Do some complex work on the person here.
Next
End Function
End Class
Public Class DataLayerClass
Public Function getAllPersons() As List(Of Person)
Dim list As List(Of Person) = New List(Of Person)
Dim p As New Person
Dim objCommand As New SqlCommand
Dim strConString As String = "Data Source=IANSCOMPUTER;Initial Catalog=Test;Integrated Security=True"
Dim objCon As New SqlConnection
objCon.ConnectionString = strConString
objCon.Open()
objCommand.Connection = objCon
objCommand.CommandText = "select * from person "
Dim objDR As SqlDataReader = objCommand.ExecuteReader
If objDR.HasRows Then
Using objCon
Do While objDR.Read
p.Name = objDR("Name")
p.age = objDR("Age")
list.Add(p)
Loop
End Using
End If
Return list
End Function
End Class
Public Class Person
Public Name As String
Public age As String
End Class
或者,我可以在业务层中创建一个使用适配器模式http://en.wikipedia.org/wiki/Adapter_pattern
(哪个选项更合适?我想这在某种程度上取决于问题域。