Index: ActiviteitenOpvolging/ActiviteitenOpvolging/mappers/EmployeesDatabaseMapper.vb =================================================================== diff -u --- ActiviteitenOpvolging/ActiviteitenOpvolging/mappers/EmployeesDatabaseMapper.vb (revision 0) +++ ActiviteitenOpvolging/ActiviteitenOpvolging/mappers/EmployeesDatabaseMapper.vb (revision 1709) @@ -0,0 +1,131 @@ +Imports System.Data.SqlClient + +Module EmployeesDatabaseMapper + Private _sqlConnection As SqlConnection + +#Region "Help functions" + ''' + ''' Helpfunction to open SQL connection to database. + ''' + Private Sub ConnectToDatabase() + Try + If _sqlConnection Is Nothing Then + 'Build connection string. Looks something like this "Server=...;database=...;user=...;Pwd=..." + Dim connectionString = $"Server={My.Resources.EmployeesDatabaseServerName}; database={My.Resources.EmployeesDatabase}; + user={My.Resources.EmployeesDatabaseUsername}; Pwd={My.Resources.EmployeesDatabasePassword}" + 'Initialise connection. + _sqlConnection = New SqlConnection(connectionString) + End If + 'Check if connection state is already open. + If Not _sqlConnection.State = ConnectionState.Open Then + 'If not, open the connection. + _sqlConnection.Open() + End If + Catch ex As Exception + Throw New Exception($"Can't reach the Employees database: {vbNewLine}{ex.Message}") + End Try + End Sub + + ''' + ''' Execution of a query into a data reader. + ''' + ''' The query + ''' The SQL data reader + Private Function ExecuteIntoDataReader(query As String) As SqlDataReader + Try + ConnectToDatabase() + Return New SqlCommand(query, _sqlConnection).ExecuteReader() + Catch ex As Exception + Throw New Exception($"Could not get data reader from the employees database.{vbNewLine}{ex.Message}") + End Try + End Function + + ''' + ''' Tries to extract a value from a datareader with a key. + ''' If the value is not present, but not required, a default value van be specified. + ''' If the value is not present, but required, an exception will be thrown. + ''' The value will be converted to the specifief type. + ''' + ''' The target type of the value + ''' The datareader + ''' The key, identification of the value + ''' Whether or not the value is required + ''' Optional default value of the value. Standard 'Nothing' or the default value of the type + ''' The value converted to the type of + Private Function ExtractItemFromDataReader(Of T)(ByRef dr As SqlDataReader, key As String, isRequired As Boolean, Optional defaultValue As T = Nothing) As T + Dim value As Object + Try + 'Extract the value with the key. Column will always be present. + value = dr.Item(key) + 'Check if the value is DBNull or Nothing. + If IsDBNull(value) Or value Is Nothing Then + 'If so, check if value was required. + If isRequired Then + 'Value was required. + Throw New Exception($"A required value with key '{key}' was empty.") + End If + 'Value was not required, return default value. + Return defaultValue + End If + Catch ex As Exception + Throw New Exception($"A value for a work item can't be fetched from the data reader. The key is '{key}'.") + End Try + Try + 'Do the conversion explicitly, so exceptions can be caught. + Return CType(value, T) + Catch ex As Exception + Throw New Exception($"A value for a work item can't be converted to the specified type. The key is '{key}', the type is {GetType(T).Name}, the value is {value}.") + End Try + End Function + +#End Region + + ''' + ''' Fetches the employees. + ''' + ''' A collection of employees + Public Function FetchEmployees() As ICollection(Of IEmployee) + 'Define the query. + Dim query = "SELECT [PersNr] + ,[Foto] + ,[Achternaam] + ,[Voornaam] + ,Initialen + FROM [WieIsWie].[dbo].[tmpWieisWie] + WHERE Bedrijf='Halux nv'" + + 'Initialise the list with employees. + Dim employees As New List(Of IEmployee) + + 'Declare SQL data reader. + Dim dr As SqlDataReader = Nothing + + Try + 'Fetch the data reader with the query. + dr = ExecuteIntoDataReader(query) + + While dr.Read() + 'Initialise new Employee. + Dim employee As New Employee + With employee + .EmployeeNumber = ExtractItemFromDataReader(Of Integer)(dr, "PersNr", True) + .FirstName = ExtractItemFromDataReader(Of String)(dr, "Voornaam", False, String.Empty) + .LastName = ExtractItemFromDataReader(Of String)(dr, "Achternaam", False, String.Empty) + .Initials = ExtractItemFromDataReader(Of String)(dr, "Achternaam", True) + End With + + 'Add new employee to list. + employees.Add(employee) + End While + 'Return the employees. + Return employees + Catch ex As Exception + Throw New Exception($"An error occured while fetching the employees.{vbNewLine}{ex.Message}") + Finally + 'Close connection if datareader is not nothing. + If Not IsNothing(dr) Then + dr.Close() + End If + End Try + End Function +End Module Index: ActiviteitenOpvolging/ActiviteitenOpvolging/domain/classes/IEmployee.vb =================================================================== diff -u --- ActiviteitenOpvolging/ActiviteitenOpvolging/domain/classes/IEmployee.vb (revision 0) +++ ActiviteitenOpvolging/ActiviteitenOpvolging/domain/classes/IEmployee.vb (revision 1709) @@ -0,0 +1,7 @@ +Public Interface IEmployee + Property EmployeeNumber As Integer + 'Property Picture As Image + Property FirstName As String + Property LastName As String + Property Initials As String +End Interface Index: ActiviteitenOpvolging/ActiviteitenOpvolging/ActiviteitenOpvolging.vbproj =================================================================== diff -u -r1701 -r1709 --- ActiviteitenOpvolging/ActiviteitenOpvolging/ActiviteitenOpvolging.vbproj (.../ActiviteitenOpvolging.vbproj) (revision 1701) +++ ActiviteitenOpvolging/ActiviteitenOpvolging/ActiviteitenOpvolging.vbproj (.../ActiviteitenOpvolging.vbproj) (revision 1709) @@ -75,6 +75,7 @@ + @@ -96,6 +97,7 @@ + True Index: ActiviteitenOpvolging/ActiviteitenOpvolging/domain/classes/implementations/Employee.vb =================================================================== diff -u --- ActiviteitenOpvolging/ActiviteitenOpvolging/domain/classes/implementations/Employee.vb (revision 0) +++ ActiviteitenOpvolging/ActiviteitenOpvolging/domain/classes/implementations/Employee.vb (revision 1709) @@ -0,0 +1,8 @@ +Public Class Employee + Implements IEmployee + + Public Property EmployeeNumber As Integer Implements IEmployee.EmployeeNumber + Public Property FirstName As String Implements IEmployee.FirstName + Public Property LastName As String Implements IEmployee.LastName + Public Property Initials As String Implements IEmployee.Initials +End Class