|
| 1 | +# Solution Design: Plugin Contract and Foundation (Story 004) |
| 2 | + |
| 3 | +## Overview |
| 4 | +This document summarizes the recommended implementation order, rationale, and testing strategies for Story 004: "Define a unified plugin contract and foundation (DI, config, localization, logging) for safe, consistent plugin integration." |
| 5 | + |
| 6 | +## Recommended Task Order |
| 7 | + |
| 8 | +1. **Create Modulus.Plugin.Abstractions Project** |
| 9 | + - Define interfaces: |
| 10 | + - `IPlugin`: Main plugin contract, including metadata, DI, initialization, and UI extension points. |
| 11 | + - `IPluginMeta`: Metadata contract (Name, Version, Description, Author, Dependencies). |
| 12 | + - `ILocalizer`: Localization contract (resource access, language switching). |
| 13 | + - (Optional) `IPluginSettings`: For plugin-specific configuration. |
| 14 | + - Example interface definitions: |
| 15 | + |
| 16 | +```csharp |
| 17 | +public interface IPlugin |
| 18 | +{ |
| 19 | + IPluginMeta Meta { get; } |
| 20 | + void ConfigureServices(IServiceCollection services, IConfiguration configuration); |
| 21 | + void Initialize(IServiceProvider provider); |
| 22 | + object? GetMainView(); // For UI plugins, e.g., Avalonia Control |
| 23 | + object? GetMenu(); // For menu extension |
| 24 | +} |
| 25 | + |
| 26 | +public interface IPluginMeta |
| 27 | +{ |
| 28 | + string Name { get; } |
| 29 | + string Version { get; } |
| 30 | + string Description { get; } |
| 31 | + string Author { get; } |
| 32 | + string[]? Dependencies { get; } |
| 33 | + string ContractVersion { get; } // Required: plugin contract version |
| 34 | +} |
| 35 | + |
| 36 | +public interface ILocalizer |
| 37 | +{ |
| 38 | + string this[string key] { get; } |
| 39 | + string CurrentLanguage { get; } |
| 40 | + void SetLanguage(string lang); |
| 41 | + IEnumerable<string> SupportedLanguages { get; } |
| 42 | +} |
| 43 | + |
| 44 | +public interface IPluginSettings |
| 45 | +{ |
| 46 | + IConfiguration Configuration { get; } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +2. **Update Plugin Template and Main App to Reference SDK** |
| 51 | + - Add project/package reference to `Modulus.Plugin.Abstractions` in both plugin template and main app. |
| 52 | + - Ensure all plugin entry points implement `IPlugin`. |
| 53 | + |
| 54 | +3. **Standardize Plugin Directory Structure and Metadata** |
| 55 | + - Each plugin in its own subdirectory: |
| 56 | + - `PluginA/PluginA.dll` |
| 57 | + - `PluginA/pluginsettings.json` |
| 58 | + - `PluginA/lang.en.json`, `PluginA/lang.zh.json`, ... |
| 59 | + - Example `pluginsettings.json`: |
| 60 | +```json |
| 61 | +{ |
| 62 | + "ContractVersion": "2.0.0", |
| 63 | + "SettingA": "value", |
| 64 | + "SettingB": 123 |
| 65 | +} |
| 66 | +``` |
| 67 | + - Example `lang.en.json`: |
| 68 | +```json |
| 69 | +{ |
| 70 | + "Hello": "Hello", |
| 71 | + "Exit": "Exit" |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +4. **Upgrade PluginLoader to Only Load IPlugin Implementations** |
| 76 | + - Use reflection to ensure only assemblies with `IPlugin` implementations are loaded. |
| 77 | + - Example: |
| 78 | +```csharp |
| 79 | +var asm = context.LoadFromAssemblyPath(pluginPath); |
| 80 | +var pluginType = asm.GetTypes().FirstOrDefault(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract); |
| 81 | +if (pluginType == null) throw new InvalidOperationException("No valid IPlugin implementation found."); |
| 82 | +``` |
| 83 | + |
| 84 | +5. **Implement DI, Config, and Logging Support** |
| 85 | + - `IPlugin.ConfigureServices` for service registration. |
| 86 | + - `IPlugin.Initialize` for runtime setup with `IServiceProvider`. |
| 87 | + - Plugins access `IConfiguration` and `ILogger<T>` via DI. |
| 88 | + - Example usage in plugin: |
| 89 | +```csharp |
| 90 | +public void ConfigureServices(IServiceCollection services, IConfiguration configuration) |
| 91 | +{ |
| 92 | + services.AddSingleton<IMyService, MyService>(); |
| 93 | +} |
| 94 | +public void Initialize(IServiceProvider provider) |
| 95 | +{ |
| 96 | + var logger = provider.GetRequiredService<ILogger<MyPlugin>>(); |
| 97 | + logger.LogInformation("Plugin initialized"); |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +6. **Documentation and Example Plugins** |
| 102 | + - Write English interface docs and sample plugin code. |
| 103 | + - Example README snippet for plugin authors: |
| 104 | +```markdown |
| 105 | +## Plugin Contract |
| 106 | +- Implement `IPlugin` from Modulus.Plugin.Abstractions |
| 107 | +- Provide metadata, DI, and (optionally) UI extension points |
| 108 | +- Place your plugin DLL and config/localization files in a dedicated subdirectory |
| 109 | +``` |
| 110 | + |
| 111 | +## Version Compatibility and Error Handling |
| 112 | + |
| 113 | +### Problem |
| 114 | +When the plugin contract (SDK) version changes, there are two main incompatibility scenarios: |
| 115 | +1. **Plugin is too old**: The plugin's contract version is lower than the minimum required by the host application. |
| 116 | +2. **Host is too old**: The plugin requires a newer contract version than the host supports. |
| 117 | + |
| 118 | +### Solution |
| 119 | +- **Contract Version Declaration**: Both the host and each plugin declare their supported/required contract version (e.g., `ContractVersion` in `IPluginMeta` and a constant in the host). |
| 120 | +- **Compatibility Check**: When loading a plugin, the host compares its supported contract version with the plugin's declared version. |
| 121 | +- **Error Handling**: |
| 122 | + - If the plugin is too old (plugin version < host minimum): |
| 123 | + - Do not load the plugin. |
| 124 | + - Show/log a user-friendly message: `The plugin "{Name}" is not compatible with this version of Modulus. Please contact the plugin developer to update the plugin.` |
| 125 | + - If the host is too old (plugin version > host maximum): |
| 126 | + - Do not load the plugin. |
| 127 | + - Show/log a user-friendly message: `The plugin "{Name}" requires a newer version of Modulus. Please update the application to use this plugin.` |
| 128 | + - For all other contract mismatches, provide a clear error message and do not throw raw exceptions to the user. |
| 129 | + |
| 130 | +#### Example Implementation |
| 131 | +```csharp |
| 132 | +const string HostContractVersion = "2.0.0"; |
| 133 | +const string HostMinSupportedVersion = "1.0.0"; |
| 134 | + |
| 135 | +public object? RunPluginWithContractCheck(string pluginPath) |
| 136 | +{ |
| 137 | + var meta = ReadMeta(pluginPath); |
| 138 | + if (meta == null) |
| 139 | + throw new InvalidOperationException("Plugin metadata not found."); |
| 140 | + |
| 141 | + Version pluginVer = new Version(meta.ContractVersion); |
| 142 | + Version hostVer = new Version(HostContractVersion); |
| 143 | + Version hostMin = new Version(HostMinSupportedVersion); |
| 144 | + |
| 145 | + if (pluginVer < hostMin) |
| 146 | + throw new PluginContractException($"The plugin '{meta.Name}' is too old and not compatible with this version of Modulus. Please contact the plugin developer to update the plugin."); |
| 147 | + if (pluginVer > hostVer) |
| 148 | + throw new PluginContractException($"The plugin '{meta.Name}' requires a newer version of Modulus. Please update the application to use this plugin."); |
| 149 | + |
| 150 | + // ...existing plugin loading logic... |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +- **UI/Log Integration**: Catch `PluginContractException` and display/log the message to the user, not a raw stack trace. |
| 155 | +- **Documentation**: Clearly document the contract versioning policy for both plugin and host developers. |
| 156 | + |
| 157 | +## Testing Strategy |
| 158 | +- **Contract Tests**: Use reflection/mocks to ensure only `IPlugin` implementations are loaded. |
| 159 | +- **DI/Config/Logging**: Integration tests to verify plugins can register/resolve services, read config, and log. |
| 160 | +- **Localization**: Test plugins can load and switch language resources via `ILocalizer`. |
| 161 | +- **End-to-End**: Load multiple plugins, verify isolation, config, and logging. |
| 162 | + |
| 163 | +## File/Project Organization (Recommended) |
| 164 | +- `src/Modulus.Plugin.Abstractions/` (new project, all contracts/interfaces) |
| 165 | +- `src/Modulus.PluginHost/` (host, references Abstractions) |
| 166 | +- `src/Modulus.App/` (main app, references Abstractions) |
| 167 | +- `tools/modulus-plugin/` (plugin template, references Abstractions) |
| 168 | +- Each plugin: its own subdirectory with `dll`, `pluginsettings.json`, `lang.xx.json`, etc. |
| 169 | + |
| 170 | +## Notes |
| 171 | +- All code comments and README files must be in English. |
| 172 | +- Story/requirement docs can remain in Chinese for team communication. |
| 173 | +- This solution is flexible: teams can adjust file/project layout as long as contract and testability are preserved. |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +**This document is intended as a reference for all team members implementing or extending the plugin system foundation.** |
0 commit comments