Tuesday, May 1, 2012

Common Developer Principles

There are two main principles that I usually adhere to.  SOLID and DRY are the two that I preach about the most.  They make things logical and simple.

As I was looking through some source code I came upon this section: (I altered the connection strings.)

Code Snippet
  1. Sub LoadConnection()
  2.         g_intUserID = 0
  3.         ADOModule.g_Connection = New SqlClient.SqlConnection
  4.         Select Case True
  5.             Case Demo
  6.                 ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
  7.             Case Live
  8.                 If g_ConnectUsingIPAddress Then
  9.                     ADOModule.SetSQLServerContentText("ipaddress", "database", "password", "username", "display", g_intTimeout)
  10.                 Else
  11.                     ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
  12.                 End If
  13.             Case BackUp
  14.                 If g_ConnectUsingIPAddress Then
  15.                     ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
  16.                 Else
  17.                     ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
  18.                 End If
  19.             Case TEST
  20.                 ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
  21.             Case Else
  22.                 End
  23.         End Select
  24.         If Not ADOModule.ConnectToSQLServer(ADOModule.g_Connection) Then
  25.             End
  26.         End If
  27.     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
  1. Public Interface IDbConnection
  2.     Inherits ICloneable
  3.  
  4.     Enum ConnectionTo As Integer
  5.         Live = 0
  6.         Backup = 1
  7.         Test = 2
  8.         Demo = 3
  9.     End Enum
  10.  
  11.     ReadOnly Property IPAddress() As String
  12.     ReadOnly Property Server() As String
  13.     ReadOnly Property Database() As String
  14.     ReadOnly Property Username() As String
  15.     ReadOnly Property Password() As String
  16.     ReadOnly Property Timeout() As Integer
  17.     ReadOnly Property Subject() As String
  18.     ReadOnly Property Display() As String
  19.  
  20.     Function ConnectToSQLServer() As Boolean
  21.     Function GetSqlConnection() As SqlClient.SqlConnection
  22. 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
  1.  
  2. connections = New Dictionary(Of IDbConnection.ConnectionTo, Lazy(Of IDbConnection))
  3.  
  4. connections.Add(IDbConnection.ConnectionTo.Live, New Lazy(Of IDbConnection)(Function() New LiveConnection))
  5. connections.Add(IDbConnection.ConnectionTo.Test, New Lazy(Of IDbConnection)(Function() New TestConnection))
  6. connections.Add(IDbConnection.ConnectionTo.Demo, New Lazy(Of IDbConnection)(Function() New DemoConnection))
  7. 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
  1.  
  2. 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