Skip to content

Instantly share code, notes, and snippets.

@DominicFinn
Last active July 26, 2016 22:24
Show Gist options
  • Save DominicFinn/3db253530ed76555ab43596fb0a73b6d to your computer and use it in GitHub Desktop.
Save DominicFinn/3db253530ed76555ab43596fb0a73b6d to your computer and use it in GitHub Desktop.
How to use Delegates and Events in VB
Imports System.Data.SqlClient
Module Updater
Sub Main()
Console.WriteLine("Welcome to the cool async updater")
dim updater = New DatabaseUpdater(new UpdateScanner())
addHandler updater.ErrorRaised, AddressOf InformUserOfError
AddHandler Updater.Success, AddressOf InformUserOfSuccess
dim updaterRun = Task.Factory.StartNew(Async Sub() Await updater.Run())
updaterRun.Wait()
Console.ReadKey()
End Sub
Private Sub InformUserOfSuccess(eventdetails As ExecutionResult)
Dim color = console.BackgroundColor
If eventdetails.Success then
Console.ForegroundColor = ConsoleColor.Green
Console.WriteLine("You did it! Teh Updates ran")
Console.WriteLine(eventdetails.Message)
Else
Console.ForegroundColor = ConsoleColor.Yellow
Console.WriteLine("Oh man there were problems")
Console.WriteLine(eventdetails.Message)
End If
Console.ForegroundColor = color
End Sub
Private Sub InformUserOfError(eventdetails As ExecutionResult)
dim color = console.ForegroundColor
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("{0}, {1}", eventdetails.Message, eventdetails.Exception.Message)
Console.ForegroundColor = color
End Sub
End Module
public delegate sub SuccessEvent(eventDetails As ExecutionResult)
Public Delegate Sub ErrorEvent(eventDetails as ExecutionREsult)
Public Class DatabaseUpdater
public Event Success as SuccessEvent
public Event ErrorRaised as ErrorEvent
Private ReadOnly _updateScanner As IUpdateScanner
public Sub New (updateScanner As IUpdateScanner)
Me._updateScanner = updateScanner
end Sub
public Async function Run() As Task
Dim updates = Me._updateScanner.Updates().ToArray()
dim failed as Integer
If updates.Any() then
try
Using conn as New SqlConnection("")
await conn.OpenAsync()
for each u in updates
dim result = Await Update(u, conn)
If result = false
failed = failed + 1
End If
Next
conn.Close()
End Using
Catch ex As Exception
RaiseEvent ErrorRaised(New ExecutionResult(false, "Unable to create the connection", ex))
return
End Try
End If
RaiseEvent Success(new ExecutionResult(failed = 0, $"{updates.Count() - failed} of {updates.Count()} Updates completed successfully", nothing))
End function
Async Function Update(updateCommand As string, conn As SqlConnection) as Task(Of boolean)
Try
Using command As New SqlCommand(updateCommand, conn)
command.CommandType = CommandType.Text
Await command.ExecuteNonQueryAsync()
End Using
Catch ex As Exception
RaiseEvent ErrorRaised(new ExecutionResult(False, $"There was an error running the update `{updateCommand}`", ex))
return false
End Try
return true
End Function
End Class
Public Class ExecutionResult
Property Success() As boolean
Property Message() As string
Property Exception() As Exception
Public sub New(success As Boolean, message As String, exception As Exception)
me.Success = success
me.Message = message
me.Exception = exception
End sub
End Class
public Interface IUpdateScanner
function Updates() As IEnumerable(of string)
End Interface
Public Class UpdateScanner
Implements IUpdateScanner
Public Function Updates() As IEnumerable(Of String) Implements IUpdateScanner.Updates
Dim list = New List(Of String)()
list.Add("CREATE TABLE [dbo].[Pet](
[Type] [int] NULL
) ON [PRIMARY]")
list.Add("CREATE TABLE [dbo].[Pie](
[Type] [int] NULL
) ON [PRIMARY]")
Return list
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment