I have an windows application that is tightly coupled with a third party COM Object. And through the instance of this com object, all i am doing is create, delete, save and export. Now as new requirement arised, I have to add another similar third party component but from different vendor.
I dont want to write separate application that is similar to the first one except third party dependency changed.
How should i change my windows application so that
- I can easily switch these third party dependencies. (like loosely coupled kind of thing).
- I dont need to maintenance separate code.
I need some directions.
One option (which may not be suitable for you) is to write an interface that all plugins will inherit, which contains the abstracted-out methods of what you need to achieve from each COM .dll. Then, each plugin is a C# Class Library which implements the interface, and passes off all of the calls to a Backend COM object.
This means that you would ship 3 new DLLs, the one that contains your interface (and is referenced by the main application and the .NET Class Library), the .NET Class Library as the main ‘Plugin’, and then the backend COM .dll also.
Like I say it’s just one way to do it, but would get you up and running pretty quick with little overhead IMO.
Simplistic example:
IPlugin.cs [IPlugin.dll]
YourPlugin.cs [YourPlugin.dll, references IPlugin.dll]
PluginHost.cs [yourapp.exe, references IPlugin.dll]
PluginUser.cs [yourapp.exe, references IPlugin.dll]
Of course, that relies on the class being called “YourPlugin” within the “YourPlugin.dll”. In a production environment to support multiple .dlls you would enumerate the .dlls, load each assembly, and use Reflection to find classes within that Assembly which implement the
IPlugininterface. If you need two way communications between the plugin and the host, you can make another interface (ie.IPluginHost) and add a method toIPluginwhich accepts an instance ofIPluginHost. The implementing classes can then store that instance and call methods to have the two way comms in place.