请看一下下面的类和类型:
Imports System.Data.SqlClient
Public Class c1
Dim PersonID As Integer
Public Sub New(ByVal Person As Person)
PersonID = Person.PersonID
End Sub
Public Sub New()
End Sub
Private Sub SingleDelete()
Dim objCommand As SqlCommand
Dim objCon As SqlConnection
Try
Dim _ConString As String = "Data Source=IANSCOMPUTER;Initial Catalog=Test;Integrated Security=True;MultipleActiveResultSets=true"
objCon = New SqlConnection(_ConString)
objCommand = New SqlCommand("Delete FROM Person were id=" & PersonID)
objCommand.Connection = objCon
objCon.Open()
objCommand.ExecuteNonQuery()
Catch ex As Exception
Throw
Finally
End Try
End Sub
Private Sub MassDelete(ByVal listPerson As List(Of Person))
Dim objCommand As SqlCommand
Dim objCon As SqlConnection
Try
Dim _ConString As String = "Data Source=IANSCOMPUTER;Initial Catalog=Test;Integrated Security=True;MultipleActiveResultSets=true"
objCon = New SqlConnection(_ConString)
For Each Person In listPerson
objCommand = New SqlCommand("Delete FROM Person were id=" & Person.PersonID)
objCommand.Connection = objCon
objCon.Open()
objCommand.ExecuteNonQuery()
Next
Catch ex As Exception
Throw
Finally
End Try
End Sub
End Class
Public Class Person
Public PersonID As String
End Class
这个类有两个用途:
1) Call the constructor with a Person argument. Then call SingleDelete i.e. delete one person.
2) Call the zero argument constructor. Then call multiple delete i.e. delete many persons (all persons in the list are deleted)
这是否违反了 SOLID 原则?在我看来,这门课有两个目的。如果使用零参数构造函数,则不使用 PersonID 实例变量。
如果我想太多,我会徘徊。