Goglides Dev 🌱

Cover image for Plugin Architecture in C# for Improved Software Design
Dev Leader
Dev Leader

Posted on • Originally published at devleader.ca

Plugin Architecture in C# for Improved Software Design

The post Plugin Architecture in C# for Improved Software Design appeared first on Dev Leader.

My obsession with creating applications based on plugin architectures probably stems from my history playing role-playing games. These games generally required systems that could have mechanics extended via plugins or new content that could be added to the game via plugins. Since much of my experience early on when learning C# as a kid was making games, a plugin architecture in C# applications easily became something I learned about regularly.

In this article, I’ll introduce you to the concept of plugin architectures and specifically look at plugin architecture in C# as well as how we can explore loading plugin information. We’ll also look at some high-level examples of situations where plugins would be valuable — but implementing these in greater detail is an exercise you can do for homework! Let’s dive into it!

Dev Leader Weekly | Substack

My weekly newsletter that simplifies software engineering for you, along with C# code examples. Join thousands of software engineers from companies like Microsoft and Amazon who are already reading! Click to read Dev Leader Weekly, a Substack publication with thousands of subscribers.

favicon weekly.devleader.ca

Understanding Plugin Architecture

So what exactly is a plugin architecture with respect to how software is built? A plugin architecture is a design pattern that allows you to extend the functionality of an existing application by dynamically loading and executing external modules or plugins. These plugins can be developed separately and added to the application without modifying the core codebase. That means we can add new functionality and never even need to rebuild the code of the original core application!

Using a plugin architecture in C# applications provides several benefits and some of them include:

  1. Modularity: With a plugin architecture, different components of an application can be developed separately as plugins. This modularity allows you to focus on specific functionalities without impacting the overall structure of the application. It also enables easier maintenance and updates, as plugins can be added or replaced independently.

  2. Flexibility: By using a plugin architecture, you can easily introduce new features or functionalities to an application without modifying the core code. This flexibility allows for rapid development and iteration, as new plugins can be created and integrated with less effort than modifying core shared code.

  3. Code Reusability: Plugins can be developed as reusable components that can be used across multiple projects or applications. This reusability not only saves development time but also promotes code consistency and reduces the chances of introducing bugs.

  4. Customization: Plugin architectures allow users or clients to customize and extend the functionality of an application based on their specific needs. This customization can be achieved by simply adding or removing plugins without requiring changes to the core codebase.

Overall, plugin architectures in C# are something that I use heavily in my development to provide a modular and flexible approach to building software. Check out this video for how I use plugin systems in my application based on vertical slices:


Loading Plugins in CSharp

In C#, there are various approaches and techniques for loading plugins into your software solution. Loading plugins can provide enhanced functionality and flexibility to your application. Let’s explore some of these approaches and techniques with code examples. For the following examples, assume we have an IPlugin interface that simply maps to the following code:

public IPlugin
{
    void Execute();
}
Enter fullscreen mode Exit fullscreen mode

Dynamic Assembly Loading with Reflection in CSharp

One approach for loading plugins in C# is by dynamically loading assemblies. This allows you to load external DLL files at runtime, which can contain the necessary code for your plugins. Here’s an example of how you can achieve this:

// Get the path to the plugin DLL
string pluginPath = "path/to/plugin.dll";

// Load the plugin assembly
Assembly assembly = Assembly.LoadFrom(pluginPath);

// Instantiate the plugin types
IEnumerable<Type> pluginTypes = assembly
    .GetTypes()
    .Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract);

// Create instances of the plugins
List<IPlugin> plugins = new();
foreach (Type pluginType in pluginTypes)
{
    IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);
    plugins.Add(plugin);
}

// Use the plugins
foreach (IPlugin plugin in plugins)
{
    plugin.Execute();
}
Enter fullscreen mode Exit fullscreen mode

In the above code, we first load the plugin assembly using Assembly.LoadFrom. We then use reflection to find all types that implement the IPlugin interface and are not abstract. We create instances of these plugin types using reflection — specifically, using Activator.CreateInstance — and store them in a list. Finally, we can use the plugins by calling their Execute method.

Autofac for Dynamically Loading C# Plugins

One of my favorite dependency injection frameworks in C# is Autofac. We can use Autofac to scan a directory for assemblies that contain types implementing the IPlugin interface and register them. Here’s an example of how to set up Autofac in C# for this purpose:

using Autofac;
using System.Reflection;

public class PluginLoader
{
    public IContainer LoadPlugins(string pluginsPath)
    {
        var builder = new ContainerBuilder();

        // Scan the plugins directory for assemblies
        // and register types that implement IPlugin
        var types = Directory
            .GetFiles(pluginsPath, "*.dll")
            .Select(Assembly.LoadFrom)
            .ToArray();
        builder
            .RegisterAssemblyTypes(types)
            .AssignableTo<IPlugin>()
            .As<IPlugin>();

        return builder.Build();
    }
}
Enter fullscreen mode Exit fullscreen mode

The above code will ask use reflection to get the types from the assemblies in the plugins directory. In this case, it will be trying to load any file with a DLL extension from that folder location. From there, we’re able to ask Autofac to register those types, but we use the AssignableTo<T>() method to filter the types to only be of IPlugin. Finally, we indicate that they can be resolved as IPlugin instances using the As<T>() method.

The following code example builds the container, registers all of the plugins, and then mimics the behavior in the previous code example where we call Execute() on each plugin:

// Assuming the plugins are located in a 'plugins' folder in the application directory
var pluginFolder = Path.Combine(
    Directory.GetCurrentDirectory(),
    "plugins");
var pluginLoader = new PluginLoader();
var container = pluginLoader.LoadPlugins(pluginFolder);

// Resolve all implementations of IPlugin and execute them
foreach (var plugin in container.Resolve<IEnumerable<IPlugin>>())
{
    plugin.Execute();
}
Enter fullscreen mode Exit fullscreen mode

I’ve made video tutorials as well on how you can leverage Autofac for loading plugins:


Examples of Practical Plugin Architectures in CSharp

Now that we’ve seen how we could go about implementing how to load plugins, it’s time to look at some examples of possible plugin architectures in C# applications. In these examples, we won’t be going through building out a full-on application, but we can go into some high-level details of the use case, what a sample plugin API might look like, and why plugins could be useful.

Dynamic Data Visualization Plugins

One interesting use case of plugin architecture in C# is for dynamic data visualization. This can be particularly useful when working with large datasets or real-time data streams. By creating a plugin system, you can develop separate visualization modules that can be dynamically loaded and unloaded at runtime, depending on the type and format of the data. Instead of building the core of an application around a visualization or two, we could instead treat them as plugins to allow more rich visualizations to be added in later as the application evolves.

Here’s some example code that we could consider:

// Interface for the visualization plugin
public interface IVisualizationPlugin : IDisposable
{
    void Initialize();

    void RenderData(
        DataSet data,
        IVisualizationContext context);
}

// Example plugin implementation
public class BarChartPlugin : IVisualizationPlugin
{
    public void Initialize()
    {
        // Initialize the bar chart visualization
    }

    public void RenderData(
        DataSet data,
        IVisualizationContext context)
    {
        // Render the bar chart with the provided data
    }

    public void Dispose()
    {
        // Cleanup resources used by the bar chart
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we might want to have some optional entry points for plugins to get things set up. This would be our Initialize() method. We mark each as IDisposable so that we can ensure that each plugin has a proper opportunity to clean up resources when we’re ready to unload them. The RenderData method is where the magic happens, and each plugin would be able to take the incoming data set and a context object for visualization. This context object is contrived, and of course, would be heavily application dependent, but it could be something that allows the plugin to add a UI control into it or a surface that the plugin can paint the visualization onto directly.

Extension-Based Plugins for File Processing

Another example of a possible plugin architecture in C# is for extending file processing capabilities. Imagine you have a file-processing application that supports various file formats, such as images, documents, and videos. By implementing a plugin system, you can allow users to develop and load custom file format handlers dynamically.

In the code below, we’ll see a plugin format that allows us to check if the plugin can support the file extension. You could imagine that we have something like a facade class that would iterate through each plugin to see which supports it, and call the relevant plugin to handle it:

// Interface for the file format plugin
public interface IFileFormatPlugin
{
    bool CanHandleFile(string filePath);

    void ProcessFile(string filePath);
}

// Example plugin implementation
public class PDFPlugin : IFileFormatPlugin
{
    public bool CanHandleFile(string filePath)
    {
        return filePath.EndsWith("pdf", StringComparison.OrdinalIgnoreCase);
    }

    public void ProcessFile(string filePath)
    {
        // Perform PDF-specific file processing
    }
}
Enter fullscreen mode Exit fullscreen mode

Custom Rule Engine Plugins

A plugin architecture can also be beneficial when implementing a custom rule engine for applications that require complex validation or business rules. By using plugins, you can break down the rule engine into separate modules and dynamically load and execute them based on specific conditions or triggers.

In the code example below, we use a TryX pattern that commonly has a boolean return type and an out parameter to get a result. In this case, we can output an Exception instance when the rule evaluation is not met. An alternative way to do this could be using a custom multi-type to have your own return type that could be the result or an error, or using the OneOf NuGet package. Let’s check it out:

// Interface for the rule plugin
public interface IRulePlugin
{
    string RuleName { get; }

    bool TryEvaluateRule(
        object target,
        out Exception error);
}

// Example plugin implementation
public class AgeValidationPlugin : IRulePlugin
{
    public string RuleName { get; } = "AgeValidation";

    public bool TryEvaluateRule(
        object target,
        out Exception error)
    {
        // Check if the target object meets the age validation criteria
    }
}
Enter fullscreen mode Exit fullscreen mode

Plugin-based Authentication Systems

A plugin architecture can also be applied to authentication systems, allowing the integration of various authentication providers such as OAuth, Active Directory, or custom authentication mechanisms. By creating authentication plugins, you can enable flexibility and easily switch between different authentication methods without tightly coupling your application to a specific implementation.

Here’s a highly simplified plugin API that demonstrates the goal, but anyone who’s worked with real authentication systems likely realizes this is very trivialized:

// Interface for the authentication plugin
public interface IAuthenticationPlugin
{
    bool AuthenticateUser(string username, string password);
}

// Example plugin implementation
public class OAuthPlugin : IAuthenticationPlugin
{
    public bool AuthenticateUser(string username, string password)
    {
        // Authenticate the user using OAuth protocol
    }
}
Enter fullscreen mode Exit fullscreen mode

While this example might be a little bit light, the point is calling out the use case — authentication in applications can be a great thing to move into plugins. If you are developing something that may require different authentication integrations, leveraging plugins to interface with each and still provide authentication for your app would be a great opportunity.

Social Media Metrics Platform

A very relevant example of a plugin architecture in C# is for tracking social media metrics. In my newsletter, I was sharing behind the scenes details on how this was being built and created a video series which you can watch on YouTube detailing the entire step-by-step process for making the app. You can see the start of the Blazor build series in this video, which focuses on a plugin architecture in C#:


Wrapping Up Plugin Architecture in CSharp

Using a plugin architecture in C# development is one of my go-to ways to design systems. I admit that I have a strong bias for them! For me, when I am building applications on my own, they align with how I like to think about being able to extend software. Because I am used to structuring code to be loaded dynamically via plugin systems, this shapes a lot of my design. However, like all things, there are pros and cons and often it doesn’t make sense to complicate trivial applications with plugins — especially if there are features that are not plan to be extended.

If you found this useful and you’re looking for more learning opportunities, consider subscribing to my free weekly software engineering newsletter and check out my free videos on YouTube! If you’d like to discuss your software design with me and other software engineers, join the Discord community!

Dev Leader Weekly | Substack

My weekly newsletter that simplifies software engineering for you, along with C# code examples. Join thousands of software engineers from companies like Microsoft and Amazon who are already reading! Click to read Dev Leader Weekly, a Substack publication with thousands of subscribers.

favicon weekly.devleader.ca

Want More Dev Leader Content?

  • Follow along on this platform if you haven’t already!
  • Subscribe to my free weekly software engineering and dotnet-focused newsletter. I include exclusive articles and early access to videos: SUBSCRIBE FOR FREE
  • Looking for courses? Check out my offerings: VIEW COURSES
  • E-Books & other resources: VIEW RESOURCES
  • Watch hundreds of full-length videos on my YouTube channel: VISIT CHANNEL
  • Visit my website for hundreds of articles on various software engineering topics (including code snippets): VISIT WEBSITE
  • Check out the repository with many code examples from my articles and videos on GitHub: VIEW REPOSITORY

Top comments (0)