Plugin Framework IPlugin#
Title: Plugin Framework IPlugin
URL Source: https://newlifex.com/core/plugin
Markdown Content:
The plugin architecture is commonly used in business development, especially for plugins in the form of external DLLs. We have encapsulated the common code for plugin management.
Source code: https://github.com/NewLifeX/X/blob/master/NewLife.Core/Model/IPlugin.cs
Quick Start#
StarDust Agent supports loading plugins to achieve extended functionality. Below is the code for loading and initializing plugins in the StarDust Agent.
The management class PluginManager is responsible for loading plugins and supports scanning external DLLs. Next, Init notifies the plugin to perform initialization work. Finally, you can execute your business operations, which in the case of the StarDust Agent is Start.
A plugin is essentially an implementation of the IPlugin interface or has the PluginAttribute attribute, as follows:
Core Principle#
General plugin implementation is usually through interfaces or attributes.
The plugin interface is very simple, with only one Init. Since there may be multiple sets of plugin systems, it is necessary to specify an Identity to distinguish different types of plugins.
The service provider IServiceProvider is also very important, facilitating the plugin's internal access to various service implementations.
As you can see, our IPlugin interface only has the Init initialization method and does not have a destruction interface. Generally, we use IDispose to indicate plugin destruction, so IPlugin does not need to define similar methods again.
To manage plugins, we introduce the management class PluginManager.
- Identity. Specifies the current plugin identifier, loading only plugins with that identifier. For example, the StarAgent in the previous introductory code.
- Provider. The service provider of the host; plugins generally need to interact with other objects, and this serves as the link.
- Plugins. A collection of plugins that holds the loaded plugins.
- LoadPlugins. Scans for plugins and can quickly obtain the collection of plugins.
- Load. Loads plugins, scans, and modifies the Plugins collection.
- Init. Initializes plugins, sequentially calling the Init interface of the plugins.