Raghu
I've reverted to using reflection but am not really happy about it. Do
let me know if you've come up with something
As I stated use the Separated Interface pattern!
http://www.martinfowler.com/eaaCatalog/separatedInterface.html
Remember that your ClassFactory will effectively be implementing the Plugin
pattern to dynamically load types.
http://www.martinfowler.com/eaaCatalog/plugin.html
Note you may need to read the entire book "Patterns Of Enterprise
Application Architecture" by Martin Fowler from Addison-Wesley rather then
the above pages to get the full "gist" of what the above patterns are doing
for you. As the book goes into further details & examples.
http://www.martinfowler.com/books.html#eaa
Thanks for the response, but that does not solve the problem (atleast
without using reflection)
Yes! it does solve the problem & reflection is not involved per se as you
are early binding to the interface. Some may consider reflection is involved
as you are dynamically loading the class, however you are only dynamically
loading the class, you are still early binding to the interface. Hence no
reflection is involved in execution.
You see, Project1 will still need to refer to Project2 because even
though it returns reference to the interface, it still needs to call
teh ctor of the concrete classes that are defined in Project2
The Activator.CreateInstance will call the ctor of the concrete class, the
name of the concrete class will be in the app.config. You will early bind to
the interface!
' app.config file
<configuration>
<appSettings>
<add key="IDBWrapper" value="Project2.DBWrapper, Project2" />
</appSettings>
</configuration>
' various files in the ClassFactory project
Public Interface IDBWrapper
Sub Method1()
Sub Method2()
End Interface
public shared function createDBWrapper as IDBWrapper
Dim typeName As String
typeName =
Configuration.ConfigurationSettings.AppSettings("IDBWrapper")
Dim t As Type
t = Type.GetType(typeName)
Dim value As Object
value = Activator.CreateInstance(t)
Return DirectCast(value, IDBWrapper)
' Then in some project that references the Class Factory Project
Option Strict On
Dim dbw As IDBWrapper
dbw = ClassFactory.CreateDBWrapper()
dbw.Method1()
dbw.Method2()
As the name Separated Interface suggests, the interface to your DBWrapper is
separated from the implementation, two major benefits include (but are not
limited to):
1. You can easily change the implementation without recompiling your app.
You could include any of the following lines you your app config & your app
is suddenly using SQL Server, Odbc, Ole DB, or Oracle as the database. (The
plug in pattern)
<add key="IDBWrapper" value="Project2.SqlDBWrapper, Project2" />
<add key="IDBWrapper" value="Project2.ObdcDBWrapper, Project2" />
<add key="IDBWrapper" value="Project2.OleDbDBWrapper, Project2" />
<add key="IDBWrapper" value="Project3.OracleDBWrapper, Project3" />
2. You can have circular references between assemblies without resorting to
Reflection or Late Binding.
The key is that your app only knows about the Interface & uses the Interface
for early binding, meanwhile back in the liar (I mean Class Factory) you use
Activator.CreateInstance to dynamically load the concrete class that offers
the implementation for the interface.
Remember that using an Interface (Public Interface MyInterface) allows you
to have Early binding while keeping the Implementation (Public Class
MyClass) hidden out of the way.
Hope this helps
Jay