As I was looking through some source code I came upon this section: (I altered the connection strings.)
Code Snippet
- Sub LoadConnection()
- g_intUserID = 0
- ADOModule.g_Connection = New SqlClient.SqlConnection
- Select Case True
- Case Demo
- ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
- Case Live
- If g_ConnectUsingIPAddress Then
- ADOModule.SetSQLServerContentText("ipaddress", "database", "password", "username", "display", g_intTimeout)
- Else
- ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
- End If
- Case BackUp
- If g_ConnectUsingIPAddress Then
- ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
- Else
- ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
- End If
- Case TEST
- ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
- Case Else
- End
- End Select
- If Not ADOModule.ConnectToSQLServer(ADOModule.g_Connection) Then
- End
- End If
- End Sub
This is in two different locations on the application. There's no reason for this. At all! When you add a connection you now have to add this in two different locations. This can be easily fixed by using dependency inversion which is part of SOLID. It first starts off with needing an interface.
Code Snippet
- Public Interface IDbConnection
- Inherits ICloneable
- Enum ConnectionTo As Integer
- Live = 0
- Backup = 1
- Test = 2
- Demo = 3
- End Enum
- ReadOnly Property IPAddress() As String
- ReadOnly Property Server() As String
- ReadOnly Property Database() As String
- ReadOnly Property Username() As String
- ReadOnly Property Password() As String
- ReadOnly Property Timeout() As Integer
- ReadOnly Property Subject() As String
- ReadOnly Property Display() As String
- Function ConnectToSQLServer() As Boolean
- Function GetSqlConnection() As SqlClient.SqlConnection
- End Interface
This interface inherits the ICloneable and IDisposable interfaces. The reason behind this is now any class that implements my IDbConnection interface (Different namespace than Microsofts) is that they must now implement an ICloneable and IDisposable. The ICloneable is used to create a new instance that will have the exact same connection string so I can take advantage of connection pooling. It uses IDispose to dispose of the actual connection. I also like putting in the enumeration inside the interface as it's easier to manage since if you need to add a connection you would more then likely be looking up this interface and so you would add it here instead of having to search a main module or a main form to add to the enumeration.
Now that we have the connection interface you will need to create 4 new classes that implement this new IDbConnection interface. A test, backup, demo, and live classes will be created. You will then need to create a private Dictionary(Of IDbConnection.ConnectionTo, Lazy(Of IDbConnection)). In the form load event add the following.
Code Snippet
- connections = New Dictionary(Of IDbConnection.ConnectionTo, Lazy(Of IDbConnection))
- connections.Add(IDbConnection.ConnectionTo.Live, New Lazy(Of IDbConnection)(Function() New LiveConnection))
- connections.Add(IDbConnection.ConnectionTo.Test, New Lazy(Of IDbConnection)(Function() New TestConnection))
- connections.Add(IDbConnection.ConnectionTo.Demo, New Lazy(Of IDbConnection)(Function() New DemoConnection))
- connections.Add(IDbConnection.ConnectionTo.Backup, New Lazy(Of IDbConnection)(Function() New BackupConnection))
Now you can assign the g_connection and the rest of the connections don't take up your memory.
Code Snippet
- Dim g_connection As IDbConnection = connections(IDbConnection.ConnectionTo.Live).Value
If there is a change to anything now you just have to change it in one location. The rest of the connections take up no extra memory like before and then entire thing is easier to maintain.
No comments:
Post a Comment