What's new in Downlink 0.2: Plugins

This is part of a series of posts on the new features added in Downlink 0.2! Check out this post for more or hit the docs.

So all week we've been looking at all these cool new features available in Downlink like all the new extension points or the flexible routing behaviour, but a lot of it has required getting your hands dirty in your app's startup code and handling things like dependency injection in multiple places.

If that all seems a bit messy to you, you're quite right. To that end, Downlink has it's own powerful plugin system!

What's in a plugin?

So a plugin, in Downlink-land, is just a type (often in it's own assembly) that can add new features or behaviours to Downlink in one easy motion. In fact, plugins in Downlink are essentially just a package/wrapper around all the cool new features we've been discussing this week.

Plugins allow you to ship, for example, support for a whole new storage service, including it's own storage backend, scheme client, and supported pattern matchers, all in one easy-to-consume package.

Plugins are so handy that most of the moving parts included in Downlink out of the box are implemented as plugins!

Using plugins in your app

Since plugins are a package of extension points, they need just one change to add to your app:

// in Startup.cs
public void ConfigureServices(IServiceCollection services)  
{
    services.AddMvc().AddDownlink(d => d.UsePlugin<MyPlugin>());
}

The MyPlugin plugin will then be loaded, added to your app, and any services it provides will be available in the app.

Plugins in the Docker image

If you want to use plugins without running your own host, you can also take advantage of highly experimental support for loading plugins from the local folder!

You can see how to get started with local plugin discovery in the online docs.

Building plugins

If you want to build a plugin for Downlink, it's pretty simple:

  • Optionally, create and reference a class library
  • Add a class that implements IDownlinkPlugin
  • Add it to your startup code

That's it! Restart the app and your plugin will get invoked, your services will be added to the app container and you're good to go!

The technical details

Downlink's plugin system is essentially a wrapper over the existing DI-powered extension points. The IDownlinkPlugin interface just provides a single "hook" method to add and configure services with direct access to the DI container (via the IDownlinkBuilder) that makes registering services easier.

Since they're much simpler, quite a few of Downlink's existing moving parts like the routing, context and all of the default services are implemented as plugins that get loaded first.

Since plugins themselves are resolved out of DI, you can also use your plugin's constructor to import required services (example). It's the IPluginLoader/PluginLoader that does the actual loading.


Summary

That's it for this week's journey through Downlink 0.2's new features! Hopefully you'll find some cool new uses for Downlink and get a chance to try out all it's new features.

As always, you can find help in the docs, on GitHub, or on Gitter.

Comments

comments powered by Disqus