问题描述
请查看以下代码:
Imports System.Data.SqlClient
Public Class Person
Private id As String
Private name As String
Public Function check(ByVal personid As String) As Boolean
'Do some checks on the person to see if it is ready for deletion
End Function
Public Shared Sub Delete()
Dim v As New Vehicle
Dim p As New Person
End Sub
End Class
Public Class Vehicle
Private vrm As String
Public Function check(ByVal vehicleid As String) As Boolean
'Do some checks on the vehicle to see if it is ready for deletion
End Function
Private Shared Sub Delete()
Dim p As New Person
Dim v As New Vehicle
Dim objCommand As SqlCommand
Dim objCon As SqlConnection
Dim objDR As SqlDataReader
Try
Dim _ConString As String = "Data Source=IANSCOMPUTER;Initial Catalog=Test;Integrated Security=True;MultipleActiveResultSets=true"
objCon = New SqlConnection(_ConString)
objCommand = New SqlCommand("SELECT * FROM Person were startdate < dateadd(year,-6," & Now & ")")
objDR = objCommand.ExecuteReader
Do While objDR.Read
If p.check(objDR("id")) And v.check(objDR("vehicleid")) Then
'Execute delete statement, which deletes the person and vehicle
End If
Loop
objDR.Close()
objCommand.Connection = objCon
objCon.Open()
objCommand.ExecuteNonQuery()
Catch ex As Exception
Throw
Finally
End Try
End Sub
End Class
请注意,共享功能(Person.Delete)包含对Person和Vehicle的引用,并在Person和Vehicle中使用实例变量.基本上,必须先对人员和车辆进行检查,然后才能删除人员和车辆.
Notice that the shared function (Person.Delete) contains references to Person and Vehicle and uses instance variables in Person and Vehicle. Basically a check needs to be done on the Person and vehicle before the person and vehicle can be deleted.
从共享功能中引用人员和车辆是否不明智?在Delete函数中使用实例变量是否不明智?删除功能每天将删除数千人和车辆.
Is it poor practice to reference Person and Vehicle from the Shared function? Is it poor practice to use instance variables in the Delete function. The Delete function will delete thousands of persons and vehicles daily.
推荐答案
使它们共享并不一定是一个坏习惯,但是如果共享它们,则将它们重构为自己的类是一个好习惯.在我看来, Shared
会立即使全局函数不污染类签名.
It's not necessarily a bad practice to make them shared, but if they are shared its a good practice to refactor them into their own classes. Shared
instantly makes then global functions that, in my mind, should not contaminate the class signature.
因此,根据我的估计,这将是更好的做法:
So this would be better practice in my estimation:
Public Class Person
Private id As String
Private name As String
End Class
Public Class Vehicle
Private vrm As String
End Class
Public Class PersonVanisher
Public Shared Sub Delete()
Dim v As New Vehicle
Dim p As New Person
...
End Sub
Public Shared Function Check(personId As String) As Boolean
...
End Function
End Class
Public Class VehicleCrusher
Private Shared Sub Delete()
Dim p As New Person
Dim v As New Vehicle
...
End Sub
Public Shared Function Check(vehicleId As String) As Boolean
...
End Function
End Class
人们对此进行了哲学辩论,但是如果这是我的代码,我将不会分享任何东西.我希望在需要时新建" VehicleCrusher
,并让.NET在完成后立即处置所有内容.
People get into philosophical debates about this, but if it were my code I would not make anything shared. I would prefer to "New up" a VehicleCrusher
when I need it and let .NET dispose everything as soon as I'm done.
这篇关于在共享函数中使用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!