|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
The ProblemIt was decided by the powers that we take an existing in-house application and release it externally. This application used a proprietary class library, which could not to be included. However, the application would need the same functionality in a new class library when released externally. Furthermore, the existing in-house class library was scheduled to be re-architected in the near future. The challenge was to maintain a single code base for the application to support both the existing and the new DLLs, and be flexible enough to support the yet-to-be-designed rewrite of the existing DLL. This seemed a perfect scenario to use the The SolutionThe plan was to create an interface to describe the properties, methods, and events that will be exposed from the external class library. Any external dynamic linked library must implement this interface. We will then create a helper class library implementing the same interface. The helper class library will contain all the logic needed to load an external DLL implementing the interface. The main application will simply link to the helper class library, and remain oblivious to the logic needed to link specific external class libraries. We will use VB.NET 2005 to create a solution of several fairly generic projects. These projects will serve no useful purpose other than to demonstrate this concept. The first thing we need to do is create an interface that describes our swappable library. We will create a class library project named InterfaceDLL. This project contains a single interface named Public Interface ILibrary
Function GetName() As String
End Interface
This interface states that any DLL to which our application intends to link must expose a Next, we will create the first class library to use this interface. This will be the RedDLL project. It will contain the following code. Imports InterfaceDLL
Public Class clsMe
Implements ILibrary
Public Function GetName() As String Implements ILibrary.GetName
Return "RedDLL"
End Function
End Class
This code imports the Next, we will create the helper class library project, in this case, ColorDLL. This project contains all the .NET magic for this example. First, just like the previous two class libraries, this one imports the Imports InterfaceDLL
Imports System.Reflection
Public Class clsMe
Implements ILibrary
' Grab the name from main application's app.config
Private strColorDLL As String = _
System.Configuration.ConfigurationSettings.AppSettings("ColorDLL")
Public Function GetName() As String Implements ILibrary.GetName
Dim objAssembly As Reflection.Assembly
Dim clsColor As ILibrary = Nothing
' We're assuming the library is in the bin directory for this application
Dim strColorDLLPath As String = _
System.IO.Path.Combine(My.Application.Info.DirectoryPath, strColorDLL)
Dim objTypes() As Type
Dim objFound As Type
Dim strRV As String = String.Empty
' Load the assembly from the DLL
objAssembly = Reflection.Assembly.LoadFrom(strColorDLLPath)
' March through the types in the assembly
objTypes = objAssembly.GetTypes
For Each objItem As Type In objTypes
' Looking for our Interface
objFound = objItem.GetInterface("ILibrary")
If objFound IsNot Nothing Then
' We wouldn't be here if it wasn't the right type, so we can DirectCast
clsColor = DirectCast(objAssembly.CreateInstance(objItem.FullName), _
ILibrary)
Exit For
End If
Next
' If we've got it, call our function
If clsColor IsNot Nothing Then
strRV = clsColor.GetName
End If
' Let 'em have it
GetName = strRV
End Function
End Class
For each project containing a class importing the Finally, we will create the main application project, in this case, creatively named MainExe. For this example, we will just drop the code into the Public Class Form1
Dim clsColor As New ColorDLL.clsMe
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.lblLibrary.Text = clsColor.GetName
End Sub
End Class
Now, a couple things are happening behind the scenes here. First, when the developer builds the MainExe project, a reference will have to be added to the appropriate RedDLL or BlueDLL project or DLL, as well as a reference to the helper class. Additionally, we will add a line to the app.config to specify which DLL will be loaded by the helper class library. Remember, the app.config key is referenced in the helper class library, ColorDLL. It is not referenced in the main application. <appSettings>
<!--It is assumed that the library is in the bin directory,
we only need its name-->
<add key="ColorDLL" value="BlueDLL.dll"/>
</appSettings>
ConclusionNow, using this method, you can create a system for plugging in interchangeable libraries, without having multiple versions of your main application. You are also ready for future libraries that implement the same interface. Finally, you do not have to stray from your type safe development.
|
||||||||||||||||||||||