Winterborn.Library.PluginArchitecture
3.1.0
dotnet add package Winterborn.Library.PluginArchitecture --version 3.1.0
NuGet\Install-Package Winterborn.Library.PluginArchitecture -Version 3.1.0
<PackageReference Include="Winterborn.Library.PluginArchitecture" Version="3.1.0" />
<PackageVersion Include="Winterborn.Library.PluginArchitecture" Version="3.1.0" />
<PackageReference Include="Winterborn.Library.PluginArchitecture" />
paket add Winterborn.Library.PluginArchitecture --version 3.1.0
#r "nuget: Winterborn.Library.PluginArchitecture, 3.1.0"
#:package Winterborn.Library.PluginArchitecture@3.1.0
#addin nuget:?package=Winterborn.Library.PluginArchitecture&version=3.1.0
#tool nuget:?package=Winterborn.Library.PluginArchitecture&version=3.1.0
Plugin Architecture
This library provides a simple way of implementing a plug-in architecture. To use this plugin, there are a small number of simple steps.
TL;DR
You can use this library to dynamically load types that implement any interface you define. Those types can be scattered across multiple assemblies. This makes it easy to implement a plug-in architecture with many options about how to distribute and organize the code.
var plugins = Plugins.Load();
var instances = plugins.GetInstances<interface>();
foreach(var instance in instances)
{
instance.DoSomething();
}
Step 0 - Download this library
I'm going to go ahead and assume you know how to do that or have already done so.
Step 1 - Identify a Use Case
Where you want to implement a Plug-in architecture, define an interface.
Provide one or more concrete implementations of the interface.
Step 2 - Load the Plugin Engine
Load the plugin engine
var plugins = Plugins.Load();
You may optionally provide a specific directory as a load parameter parameter. If you do not, the current context's environment directory will be used (most likely the directory where the current executable was invoked from).
var plugins = Plugins.Load("Absolute or Relative directory");
Alternatively, you can pass the exact assemblies from which you want to load your plugins to the load method.
var plugins = Plugins.Load(Assembly1, Assembly2, Assembly3);
The scope of all subsequent methods invoked on the plugin engine will interact with all types, in all dlls, in the provided directory and (recursively) all sub-directories.
Step 3 - Load the Plugin Engine
There are two ways of interacting with the plugin engine.
Step 3.1 - Load the Plugin Types
You may load all types that implement a given interface.
Use this method if you just need to get metadata about the type or use custom constructors.
var types = plugins.GetTypes<interface>();
Step 3.2 - Load the Plugin Implementations
You may load an instance of every type that implements a given interface.
Use this method if you want to actually interact with an instance of type plug-in types.
An important disclaimer is that this only works if the plug-in types have a parameterless constructor.
var instances = plugins.GetInstances<interface>();
Using the Factory
This library provides a super simple implementation of a basic factory pattern. The factory allows for users to specify a concrete type to use when an interface is requested. Any future code requesting an instance of the interface will have a new instance of the concrete type created and sent over.
The example below highlights the full lifecycle of this simple factory implement.
var factory = new Factory();
factory.SetImplementation<interface,class>();
var instance = factory.Make<interface>();
Notes
- The concrete types provided must have a parameterless constructor (it's not enough to provide default values)
- The factory type itself is not a singleton, so to make best possible use of it you should create it at start-up and then pass it around (or make your own singleton)
- It's configuration is done with code (not app settings) to aide in refactoring and testing
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.