Plugin Architecture - Make an MDI Parent Form Aware of Children in DLLs
I'm experimenting with a plugin architecture for my company's internal business system. I have managed to read all .DLLs in a Plugin folder which implement a specific interface. What I am trying to figure out is the best method of communication between the "host" MDI parent application and forms that will be in the .DLLs which I intend to make MDI children.
Currently, I am returning just ToolStripMenuItem objects from the .DLLs to add to the MDI Parent. I've also tested that events wired in the .DLLs to the ToolStripMenuItems do propagate to the code in the .DLLs. I've also managed to return a Form object via the interface and open that form, as the Plugin folder is being "scanned".
However, I am not clear how I would make these forms MDI children. Also, any other forms living in the .DLLs will also have to be MDI children. I created a VS 2008 Addin project just to see what's going on, and it seems that the Addin accepts an Application object on which it adds to the ToolStripMenuItems and 开发者_如何学Godoes other operations. The code to build the menu inside the .DLL. This is opposite of what I have done so far, where the MDI requests a ToolStripMenuItem from each .DLL and adds the returned object to its own menu.
Would designing my plugin architecture to accept an application object in the same manner the only way I could get forms to open as an MDI child? Am I asking for other, currently unknown to me, headaches by NOT passing in the application object to the .DLL?
A number of years ago we did something similar. How we handled it was by creating a couple of interfaces that were implemented by a PluginManager and the Plugins.
The Plugin Manager implemented an interface similar to this:
''' <summary>
'''The IPluginManager interface is implemented by whatever component manages your gui
'''It provides a means for plugins to access GUI elements of the application
''' </summary>
Public Interface IPluginManager
''' <summary>
'''The MDIForm property allows the plugin to display itself
'''inside of the application's main MDI form (ie. plugin.form.mdiparent=mdiform)
''' </summary>
ReadOnly Property MDIForm() As Form
ReadOnly Property Toolbar() As ToolBar
''' <summary>
'''Allows the plugin to request that the application's main menu be updated
''' </summary>
''' <param name="Menu">The menu to add to the main menu</param>
Sub UpdateMainMenu(ByVal Menu As Menu)
''' <summary>
'''Allows the plugin to request that the application's workspace toolbar be updated
''' </summary>
''' <param name="Buttons">the collection of toolbar buttons to add to the toolbar</param>
Sub UpdateToolbarButtons(ByVal Buttons As ToolBar.ToolBarButtonCollection)
End Interface
The Plugins implemented this interface:
''' <summary>
'''The IPlugin interface is implemented by all plugins
'''It provides a standardized means for the pluginmanager
'''to communicate with a plugin, without knowing the plugin explicitly
''' </summary>
Public Interface IPlugin
''' <summary>
'''Allows the plugin to be intialized first before it asked to run
''' </summary>
Sub Initialize()
''' <summary>
'''Allows the pluginmanager to register itself with the plugin
'''so the plugin can listen to events from the pluginmanager
''' </summary>
''' <param name="PluginManager">A plugin manager that implements the IPluginManager interface</param>
Sub RegisterPluginManager(ByVal PluginManager As IPluginManager)
''' <summary>
'''Requests the plugin to run its functionality
''' </summary>
Sub Run()
End Interface
Once the app starts up, the PluginManager finds all of the available plugins (sounds like you have this working already) and then instantiates each plugin, registering itself with each plugin. The PluginManager then Intializes and then Runs the plugin(s).
On the plugin side, when the Plugin is Initialized or Runs (depending on your needs) you simply set the plugin form's MDIParent property to the MDIForm provided in the IPluginManager interface. And Viola!
It's a pretty simple contract between the two that you can easily change or expand upon.
精彩评论