NOTE : read the rest of the series, or check out the source code.
If you enjoy reading these exercises then please buy Crista’s book to support her work.
Following on from the last post, we will look at the Plugins style today.
Style 19 – Plugins
- The problem is decomposed using some form of abstraction (procedures, functions, objects, etc.).
- All or some of the abstractions are physically encapsulated into their own, usually pre-compiled, packages. Main program and each of the packages are compiled independently. These packages are loaded dynamically by the main program, usually in the beginning (but not necessarily).
- Main program uses functions/objects from the dynamically-loaded packages, without knowing which exact implementation will be used. New implementations can be used without having to adapt or recompile the main program.
- External specification of which packages to load. This can be done by a configuration file, path conventions, user input or other mechanisms for external specification of code to be linked at run time.
We can derive a few design decisions from the constraints above:
- the plugins and the main program are to be in two separate assemblies
- the main program will load the plugins dynamically
- the main program will rely on external specification to determine which plugins to load
So, I added two new projects to the solution.
Style19Main will be the “main program” in this case, and the plugins will be defined in Style19Plugins.
First, we’ll define the contracts that these plugins must adhere to in Style19Main/PluginInterface.fs.
Notice that I have included a marker interface IPlugin, we’ll see how this comes in handy later on.
I have followed Crista’s example by splitting up the term frequency program into two stages – extract words from an input file, and finding the top 25 most frequent words.
Next, we’ll define the plugins that will implement these contracts.
I have neglected the supporting functions as they’re lifted right out of previous styles. The key point to take away here is that we have created two “plugins” that implement the aforementioned contracts.
Now we need to enable the “main program” to load these plugins dynamically at runtime. Back in the Style19Main project, let’s take a peek at MainProgram.fs.
Here, we’ll define the configuration that can be passed into the main program.
In the Config type above, we declared a PluginsDir field which tells us where to load plugins from. In the real world, this might be by convention – e.g. many popular apps have a plugins folder in its root folder.
Given an instance of this Config type, we will do the following:
- find all .DLL files inside the plugins directory
- dynamically load them
- for each assembly, find all the publicly visible types that:
- are concrete, non-abstract types
- implement the IPlugin marker interface
- construct an instance of each type (there’s an implicit assumption here that the type has a parameterless constructor)
- return the constructed plugin as a map (keyed to the names of the assembly and type)
Once we have loaded all the available plugins from DLLs in the specified folder, we can pick out the ones we need based on the configuration and execute them in turn.
The last thing we need is the “external specification”, which in this case will come from our Style19.fsx script file.
Here, we’ll simply construct the configuration and pass it along to the “main program”.
In this example I have included only one set of plugins, but the infrastructure is in place for you to add support for more plugins without having to recompile Style19Main (as per the constraints).
I find the plugins style a very useful tool in your toolbox and have used it with relative success in the past. Before you point it out to me, yes I’m aware of MEF, but I’m also not a fan of the amount of wiring required for it to work.
In general I prefer the reflection-based approach and find MEF introduces another layer of abstraction that adds more complexity than it hides.
You can find the source code for this exercise here.
1 thought on “Exercises in Programming Style–Plugins”
Pingback: F# Weekly #7-#8, 2016 | Sergey Tihon's Blog