Glowworm27
Programmer
Hello all,
I am having to do a code review on one of our contractors and ran across an interesting piece of code.
The contractor created a datalayer and used sub routines instead of functions to return values. And my question is why would he do this and how does it differ from best practices
I have included a sample of his code below...
However what I am used to seeing and how I code is as follows
George Oakes
CEO & President
COPS Software, Inc.
Programmer & Developer
.Net, WSS 3.0, SQL DBA
Check out this awsome .Net Resource!
I am having to do a code review on one of our contractors and ran across an interesting piece of code.
The contractor created a datalayer and used sub routines instead of functions to return values. And my question is why would he do this and how does it differ from best practices
I have included a sample of his code below...
Code:
'''''''''''''''''''''''''''''''''''''''''''''''
Partial Class SomePage
Inherits System.Web.UI.Page
Sub LoadModelsTable(ByVal Model_id As String)
Dim oDt As New Data.DataTable,
sExec As String = "",
dl As New DataLayer
sExec = "exec DataSQL..spGetVehModel '" & Model_id & "'"
dl.GetData(sExec, oDt)
rgModels.DataSource = oDt
rgModels.DataBind()
End Sub
End Class
'''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''
Public Class Datalayer
Public Sub GetData(ByVal sExec As String, ByVal oDt As Data.DataTable)
Dim oCn As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("oCn"))
Dim oDa As New SqlDataAdapter(sExec, oCn)
oDa.SelectCommand.CommandTimeout = 10000
oCn.Open()
oDa.Fill(oDt)
oCn.Close()
End Sub
End Class
'''''''''''''''''''''''''''''''''''''''''''''''
However what I am used to seeing and how I code is as follows
Code:
'''''''''''''''''''''''''''''''''''''''''''''''
Partial Class SomePage
Inherits System.Web.UI.Page
Sub LoadModelsTable(ByVal model_id As String)
Dim oDt As New Data.DataTable
oDt = Datalayer.GetVehicleModel(model_id)
rgModels.DataSource = oDt
rgModels.DataBind()
End Sub
End Class
'''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''
Public Class Datalayer
Public Shared Function GetVehicleModel(ByVal sMake As String) As DataSet
Dim oParams As New ParameterCollection
Try
oParams.Add(New Parameter("@VehMake", DbType.String, sMake))
Return RunExecuteDataset("DataSQL", "spGetVehModel", oParams)
Catch ex As Exception
Throw
Finally
oParams = Nothing
End Try
End Function
Private Shared Function RunExecuteDataset(ByVal sdatabaseName As String, ByVal sProcName As String, ByVal oParams As ParameterCollection) As DataSet
Dim db As Database = DatabaseFactory.CreateDatabase(sdatabaseName)
Dim mycommand As DbCommand = db.GetStoredProcCommand(sProcName)
Dim myParm As Parameter
Try
For Each myParm In oParams
If myParm.Direction = ParameterDirection.Input Then
db.AddInParameter(mycommand, myParm.Name, myParm.DbType, myParm.DefaultValue)
End If
Next
Return db.ExecuteDataSet(mycommand)
Catch ex As Exception
Throw
Finally
db = Nothing
If Not mycommand Is Nothing Then
mycommand.Dispose()
mycommand = Nothing
End If
myParm = Nothing
End Try
End Function
End Class
George Oakes
CEO & President
COPS Software, Inc.
Programmer & Developer
.Net, WSS 3.0, SQL DBA
Check out this awsome .Net Resource!