patterncsharpMinor
Executing a list of tasks from a database
Viewed 0 times
executingdatabasetaskslistfrom
Problem
I have a list of tasks I get from my database (as strings) and then execute. However I'm uncertain on how to implement this correctly.
Currently I have implemented this in the following way:
```
Private Sub Monitoring(installationCode As String, drive As String, syncFolder As String)
InitializeDatabaseConnection(installationCode)
TraceInitialize(drive, "Monitoring")
Trace.TraceInformation("Monitoring started for installation: " & installationCode)
For Each param In JdnParamDAO.GetJdnParams(General.AmosRemoteTask)
If param.Value Then
RemoteTask.Deptid = Department.GetDepartmentsNotInOne().Where(Function(x) x.Code = "001").FirstOrDefault.DepartmentID
Dim monitorTasks = TaskDefinitionDAO.GetActiveTaskDefinitionsByCategory(TaskDefinitionCategoryEnum.Monitor)
Trace.TraceInformation(monitorTasks.Count() & " have been found.")
'Perform each monitor task and save the results to the database
For Each monitorTask In monitorTasks
Try
Trace.TraceInformation("MonitorTask: " & monitorTask.Description.ToLowerInvariant() & " found.")
Dim result = String.Empty
Select Case monitorTask.Description.ToLowerInvariant()
Case "getlastbackup"
result = RemoteTaskHelper.GetLastBackup(drive, installationCode)
Case "getamosrtversion"
result = General.AmosRTVersion
Case "getstockondummypercentage"
result = RemoteTaskHelper.GetStockOnDummyPercentage()
Case "getnumberofhistoryrecords"
result = RemoteTaskHelper.GetNumerOfHistoryRecords()
Case "getnumberofsparepartlogrecords"
result = Re
Currently I have implemented this in the following way:
```
Private Sub Monitoring(installationCode As String, drive As String, syncFolder As String)
InitializeDatabaseConnection(installationCode)
TraceInitialize(drive, "Monitoring")
Trace.TraceInformation("Monitoring started for installation: " & installationCode)
For Each param In JdnParamDAO.GetJdnParams(General.AmosRemoteTask)
If param.Value Then
RemoteTask.Deptid = Department.GetDepartmentsNotInOne().Where(Function(x) x.Code = "001").FirstOrDefault.DepartmentID
Dim monitorTasks = TaskDefinitionDAO.GetActiveTaskDefinitionsByCategory(TaskDefinitionCategoryEnum.Monitor)
Trace.TraceInformation(monitorTasks.Count() & " have been found.")
'Perform each monitor task and save the results to the database
For Each monitorTask In monitorTasks
Try
Trace.TraceInformation("MonitorTask: " & monitorTask.Description.ToLowerInvariant() & " found.")
Dim result = String.Empty
Select Case monitorTask.Description.ToLowerInvariant()
Case "getlastbackup"
result = RemoteTaskHelper.GetLastBackup(drive, installationCode)
Case "getamosrtversion"
result = General.AmosRTVersion
Case "getstockondummypercentage"
result = RemoteTaskHelper.GetStockOnDummyPercentage()
Case "getnumberofhistoryrecords"
result = RemoteTaskHelper.GetNumerOfHistoryRecords()
Case "getnumberofsparepartlogrecords"
result = Re
Solution
Important: Always set Option Explicit On
From what's an option strict and explicit?
Option Strict "restricts implicit data type conversions to only widening conversions". See [here][2]. With this option enabled, you can't accidentally convert one data type to another that is less precise (e.g. from an Integer to a Byte). Again, an option that should be turned on by default.
General
-
by using guard conditions you can save horizontal spacing which improves readability. A guard condition is a condition which guards the code from beeing executed if one ore more certain conditions are fullfilled. This is done by either returning at this point or by throwing an exception depending on which is suitable in this case.
-
extract the processing of
-
extract the processing of
-
extract the processing of the
-
comments should describe why something is done. What is done should be described by the code itself.
-
calling
this would lead for your
```
Private Sub Monitoring(installationCode As String, drive As String, syncFolder As String)
InitializeDatabaseConnection(installationCode)
TraceInitialize(drive, "Monitoring")
Trace.TraceInformation("Monitoring started for installation: " & installationCode)
For Each param In JdnParamDAO.GetJdnParams(General.AmosRemoteTask)
If Not param.Value Then continue For
ProcessParameter(param)
Next
End Sub
Private Sub ProcessParameter(param As someType, installationCode As String, drive As String, syncFolder As String)
RemoteTask.Deptid = Department.GetDepartmentsNotInOne().Where(Function(x) x.Code = "001").FirstOrDefault.DepartmentID
Dim monitorTasks = TaskDefinitionDAO.GetActiveTaskDefinitionsByCategory(TaskDefinitionCategoryEnum.Monitor)
Trace.TraceInformation(monitorTasks.Count() & " have been found.")
For Each monitorTask In monitorTasks
Try
Dim result as String = ProcessMonitorTasks(monitorTask.Description, installationCode, drive, syncFolder)
ProcessMonitorResult(result, monitorTask, installationCode)
Catch ex As Exception
Trace.TraceInformation("Error " + ex.ToString())
ExceptionMonitor.LogExceptionEvent(ex, ProblemSeverity.Error)
End Try
Next
End Sub
Private Function ProcessMonitorTasks(monitorTaskDescription As String, installationCode As String, drive As String, syncFolder As String) As String
Select Case monitorTaskDescription.ToLowerInvariant()
Case "getlastbackup"
return RemoteTaskHelper.GetLastBackup(drive, installationCode)
Case "getamosrtversion"
return General.AmosRTVersion
Case "getstockondummypercentage"
return RemoteTaskHelper.GetStockOnDummyPercentage()
Case "getnumberofhistoryrecords"
return RemoteTaskHelper.GetNumerOfHistoryRecords()
Case "getnumberofsparepartlogrecords"
return RemoteTaskHelper.GetNumberOfSparePartLogRecords()
Case "getinconsistenciesbetweencompjobnextdueandlastdone"
return RemoteTaskHelper.GetInconsistenciesBetweenCompJobNextDueAndLastDone()
Case "getdifferencesofcompjobnextdueandworkorderdue"
return RemoteTaskHelper.GetDifferencesOfCompJobNextDueAndWorkOrderDue()
Case "getinconsistenciesbetweencompjobsprioandworkordersprio"
return RemoteTaskHelper.GetWorkOrderCompJobPriority()
Case "getactivecompjobswithnextdue"
return RemoteTaskHelper.GetActiveCompJobsWithNextDue()
Case "getactivecompjobswithoutnextdue"
return RemoteTaskHelper.GetActiveCompJobsWithoutNextDue()
Case "getopenworkorders"
return RemoteTaskHelper.GetOpenWorkOrders()
Case "getamosguiversion"
return RemoteTaskHelper.GetAmosGuiVersion(syncFolder)
Case "checkaddinsactivation"
return RemoteTaskHelper.CheckAddinsActivation()
Case "checkamosmobileactivation"
return RemoteTaskHelper.CheckAmosMobileActivation()
Case "checkchangelogactivation"
return RemoteTaskHelper.CheckChangelogActivation()
Case "retrievejdnmodules"
return RemoteTaskHelper.RetrieveJdnModules(installationCode, syncFolder)
Case "retrieveamosparameters"
return RemoteTaskHelper.RetrieveAmosParameters(installationCode, syncFolder)
Case "retrievestocksituation"
return RemoteTaskHelper.RetrieveStockSituation(installationCode, syncFolder)
Case "executesql"
return RemoteTaskHelper.ExecuteSQL(drive, installationCode)
Case "getfreespace"
return RemoteTaskHelper.GetFreeSpace(drive)
Case Else
Trace.TraceInformation("Non excisting monitoring task: " + monitorTask.Descript
From what's an option strict and explicit?
Option Strict "restricts implicit data type conversions to only widening conversions". See [here][2]. With this option enabled, you can't accidentally convert one data type to another that is less precise (e.g. from an Integer to a Byte). Again, an option that should be turned on by default.
General
-
by using guard conditions you can save horizontal spacing which improves readability. A guard condition is a condition which guards the code from beeing executed if one ore more certain conditions are fullfilled. This is done by either returning at this point or by throwing an exception depending on which is suitable in this case.
-
extract the processing of
param to a separate method.-
extract the processing of
monitorTask to a separate method.-
extract the processing of the
result to a separate method.-
comments should describe why something is done. What is done should be described by the code itself.
-
calling
ToString() on a String is not necessary.this would lead for your
Monitoringmethod to look like```
Private Sub Monitoring(installationCode As String, drive As String, syncFolder As String)
InitializeDatabaseConnection(installationCode)
TraceInitialize(drive, "Monitoring")
Trace.TraceInformation("Monitoring started for installation: " & installationCode)
For Each param In JdnParamDAO.GetJdnParams(General.AmosRemoteTask)
If Not param.Value Then continue For
ProcessParameter(param)
Next
End Sub
Private Sub ProcessParameter(param As someType, installationCode As String, drive As String, syncFolder As String)
RemoteTask.Deptid = Department.GetDepartmentsNotInOne().Where(Function(x) x.Code = "001").FirstOrDefault.DepartmentID
Dim monitorTasks = TaskDefinitionDAO.GetActiveTaskDefinitionsByCategory(TaskDefinitionCategoryEnum.Monitor)
Trace.TraceInformation(monitorTasks.Count() & " have been found.")
For Each monitorTask In monitorTasks
Try
Dim result as String = ProcessMonitorTasks(monitorTask.Description, installationCode, drive, syncFolder)
ProcessMonitorResult(result, monitorTask, installationCode)
Catch ex As Exception
Trace.TraceInformation("Error " + ex.ToString())
ExceptionMonitor.LogExceptionEvent(ex, ProblemSeverity.Error)
End Try
Next
End Sub
Private Function ProcessMonitorTasks(monitorTaskDescription As String, installationCode As String, drive As String, syncFolder As String) As String
Select Case monitorTaskDescription.ToLowerInvariant()
Case "getlastbackup"
return RemoteTaskHelper.GetLastBackup(drive, installationCode)
Case "getamosrtversion"
return General.AmosRTVersion
Case "getstockondummypercentage"
return RemoteTaskHelper.GetStockOnDummyPercentage()
Case "getnumberofhistoryrecords"
return RemoteTaskHelper.GetNumerOfHistoryRecords()
Case "getnumberofsparepartlogrecords"
return RemoteTaskHelper.GetNumberOfSparePartLogRecords()
Case "getinconsistenciesbetweencompjobnextdueandlastdone"
return RemoteTaskHelper.GetInconsistenciesBetweenCompJobNextDueAndLastDone()
Case "getdifferencesofcompjobnextdueandworkorderdue"
return RemoteTaskHelper.GetDifferencesOfCompJobNextDueAndWorkOrderDue()
Case "getinconsistenciesbetweencompjobsprioandworkordersprio"
return RemoteTaskHelper.GetWorkOrderCompJobPriority()
Case "getactivecompjobswithnextdue"
return RemoteTaskHelper.GetActiveCompJobsWithNextDue()
Case "getactivecompjobswithoutnextdue"
return RemoteTaskHelper.GetActiveCompJobsWithoutNextDue()
Case "getopenworkorders"
return RemoteTaskHelper.GetOpenWorkOrders()
Case "getamosguiversion"
return RemoteTaskHelper.GetAmosGuiVersion(syncFolder)
Case "checkaddinsactivation"
return RemoteTaskHelper.CheckAddinsActivation()
Case "checkamosmobileactivation"
return RemoteTaskHelper.CheckAmosMobileActivation()
Case "checkchangelogactivation"
return RemoteTaskHelper.CheckChangelogActivation()
Case "retrievejdnmodules"
return RemoteTaskHelper.RetrieveJdnModules(installationCode, syncFolder)
Case "retrieveamosparameters"
return RemoteTaskHelper.RetrieveAmosParameters(installationCode, syncFolder)
Case "retrievestocksituation"
return RemoteTaskHelper.RetrieveStockSituation(installationCode, syncFolder)
Case "executesql"
return RemoteTaskHelper.ExecuteSQL(drive, installationCode)
Case "getfreespace"
return RemoteTaskHelper.GetFreeSpace(drive)
Case Else
Trace.TraceInformation("Non excisting monitoring task: " + monitorTask.Descript
Code Snippets
Private Sub Monitoring(installationCode As String, drive As String, syncFolder As String)
InitializeDatabaseConnection(installationCode)
TraceInitialize(drive, "Monitoring")
Trace.TraceInformation("Monitoring started for installation: " & installationCode)
For Each param In JdnParamDAO.GetJdnParams(General.AmosRemoteTask)
If Not param.Value Then continue For
ProcessParameter(param)
Next
End Sub
Private Sub ProcessParameter(param As someType, installationCode As String, drive As String, syncFolder As String)
RemoteTask.Deptid = Department.GetDepartmentsNotInOne().Where(Function(x) x.Code = "001").FirstOrDefault.DepartmentID
Dim monitorTasks = TaskDefinitionDAO.GetActiveTaskDefinitionsByCategory(TaskDefinitionCategoryEnum.Monitor)
Trace.TraceInformation(monitorTasks.Count() & " have been found.")
For Each monitorTask In monitorTasks
Try
Dim result as String = ProcessMonitorTasks(monitorTask.Description, installationCode, drive, syncFolder)
ProcessMonitorResult(result, monitorTask, installationCode)
Catch ex As Exception
Trace.TraceInformation("Error " + ex.ToString())
ExceptionMonitor.LogExceptionEvent(ex, ProblemSeverity.Error)
End Try
Next
End Sub
Private Function ProcessMonitorTasks(monitorTaskDescription As String, installationCode As String, drive As String, syncFolder As String) As String
Select Case monitorTaskDescription.ToLowerInvariant()
Case "getlastbackup"
return RemoteTaskHelper.GetLastBackup(drive, installationCode)
Case "getamosrtversion"
return General.AmosRTVersion
Case "getstockondummypercentage"
return RemoteTaskHelper.GetStockOnDummyPercentage()
Case "getnumberofhistoryrecords"
return RemoteTaskHelper.GetNumerOfHistoryRecords()
Case "getnumberofsparepartlogrecords"
return RemoteTaskHelper.GetNumberOfSparePartLogRecords()
Case "getinconsistenciesbetweencompjobnextdueandlastdone"
return RemoteTaskHelper.GetInconsistenciesBetweenCompJobNextDueAndLastDone()
Case "getdifferencesofcompjobnextdueandworkorderdue"
return RemoteTaskHelper.GetDifferencesOfCompJobNextDueAndWorkOrderDue()
Case "getinconsistenciesbetweencompjobsprioandworkordersprio"
return RemoteTaskHelper.GetWorkOrderCompJobPriority()
Case "getactivecompjobswithnextdue"
return RemoteTaskHelper.GetActiveCompJobsWithNextDue()
Case "getactivecompjobswithoutnextdue"
return RemoteTaskHelper.GetActiveCompJobsWithoutNextDue()
Case "getopenworkorders"
return RemoteTaskHelper.GetOpenWorkOrders()
Case "getamosguiversion"
return RemoteTaskHelper.GetAmosGuiVersion(syncFolder)
Case "checkaddinsactivation"
return RemoteTaskHelper.CheckAddinsActivation()
Case Context
StackExchange Code Review Q#77723, answer score: 5
Revisions (0)
No revisions yet.