Custom Plugins

Custom plugins are the most flexble of all, they allow developers to extend a host interface, with minimum of ease.

There are a multitude of uses for extending an interface, for instance you could have a menu system which is dynamically populated depending on the plugins that are loaded.

Another use would be to display carousel items depending on active marketing campaigns, the list is literally as open as the website being developed.

How it Works

A custom plugin is individual to the host application, a single requirement is a shared library of Interfaces and abstract classes, that can be loaded by the host application. Plugins provide the implementation to the interface or abstract methods and the host application is responsible for extending the interface.

Example

In this example, the host application exposes an abstract MenuItem class, which is overridden in plugins.


namespace SharedPluginFeatures
{
    public abstract class MainMenuItem
    {
        public abstract string Area();

        public abstract string Controller();

        public abstract string Action();

        public abstract string Name();
    }
}

A plugin can create as many MenuItems as it requires, by overriding the MainMenuItem class


public class MiddlewareMenuItem : SharedPluginFeatures.MainMenuItem
{
    public override string Area()
    {
        return String.Empty;
    }

    public override string Controller()
    {
        return "Services";
    }

    public override string Action()
    {
        return "Middleware";
    }

    public override string Name()
    {
        return "Middleware";
    }
}

When loading the layout page, the host application can query all plugins for MainMenuItem classes, and receive instantiated classes.


    <div class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
            @{
                // dynamically add menu items from plugins
                ISharedPluginHelper sharedPluginHelper = Startup.GetServiceProvider.GetRequiredService<ISharedPluginHelper>();
                foreach (MainMenuItem menuItem in sharedPluginHelper.BuildMainMenu())
                {
                    <li><a asp-area="@menuItem.Area()" asp-controller="@menuItem.Controller()" asp-action="@menuItem.Action()">@menuItem.Name()</a></li>
                }
            }

            <li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
        </ul>
    </div>