GithubHelp home page GithubHelp logo

mrdave1999 / cplugin.net Goto Github PK

View Code? Open in Web Editor NEW
61.0 1.0 0.0 692 KB

A simple library that helps to implement a plugin-based architecture

Home Page: https://mrdave1999.github.io/CPlugin.Net

License: MIT License

C# 100.00%
addons plug-in plugin plugin-architecture plugin-loader plugins csharp dotnet dotnet-core

cplugin.net's Issues

`GetPluginFiles` method must not throw InvalidOperationException

Implementations:

This exception occurs when the Plugins key does not exist in a configuration file as a json or .env.
This behavior should be changed for two reasons:

  • Violates the Principle of least astonishment. If the consumer does not want to load any plugins, then it should delete the configuration key, i.e. the Plugins key found in a configuration file and that's it.
    The expected behavior for the consumer is that no plugin is loaded, but the opposite happens: an exception is thrown.
    This surprises the consumer.
  • As the consumer has been surprised, then should create the configuration key with an empty value. However, if the consumer uses a .json file as source, an exception will still be thrown if an empty array appears in the Plugins section.
    See issue #6

If the consumer forgets to add the Plugins key in the configuration file, then the expected behavior is that no plugins are loaded. This is not a bug.

Add TypeFinder class to find subtypes that implement the contract

This class allows to search for the subtypes that implement the contract from a plug-in.
It contains only one method:

public static class TypeFinder
{
   static IEnumerable<TSupertype> FindSubtypesOf<TSupertype>();
}

This method will search for classes that implement the contract specified by TSupertype (a generic parameter) and returns the instances that implement the contract.

Add support for dependency injection via constructor

Problem reproduction

Plugin project:

[assembly: Plugin(typeof(TestService))]

namespace MyPluginProject;

public class TestService(ILogger<TestService> logger) : ITestService
{
    public int Print()
    {
        logger.LogInformation("TestService Plugin");
        return 0;
    }
}

Host application

namespace MyHostApplication;

public class PluginStartup
{
    public void ConfigureServices(WebApplicationBuilder builder)
    {
        var envConfiguration = new CPluginEnvConfiguration();
        PluginLoader.Load(envConfiguration);
        var testServices = TypeFinder.FindSubtypesOf<ITestService>();
        foreach (ITestService testService in testServices)
            testService.Print();
    }
}

If the host application is executed, the code will fail with an exception, because the method called FindSubtypesOf uses Activator.CreateInstance and the TestService type is required to have a constructor without parameters in order to create the instance.

Solution proposal

Use the Microsoft.Extensions.DependencyInjection package to create the instances and resolve the dependencies of a service.
This solves the limitation of TypeFinder.FindSubtypesOf.

API proposal

public class PluginStartup
{
    public void ConfigureServices(WebApplicationBuilder builder)
    {
        var envConfiguration = new CPluginEnvConfiguration();
        PluginLoader.Load(envConfiguration);
        builder
          .Services
          .AddSubtypesOf<ITestService>(ServiceLifetime.Transient);
    }

    public void Configure(IApplicationBuilder app)
    {
        var testServices = app
            .Services
            .GetServices<ITestService>();

        foreach (ITestService testService in testServices)
            testService.Print();
    }
}

Why can't I copy assemblies like Example.Contracts.dll and CPlugin.Net.Attributes.dll to the plugin output directory?

This avoids copying the assemblies to the plugin output directory:

<ProjectReference Include="$(ProjectRootDir)/samples/Contracts/Example.Contracts.csproj">
<!-- This tells MSBuild not to copy Example.Contracts.dll to the plug-in output directory. -->
<Private>false</Private>
<!--
This setting has the same effect as <Private>false</Private> but works on package references
that the Example.Contracts project or one of its dependencies may include.
-->
<ExcludeAssets>runtime</ExcludeAssets>
</ProjectReference>

<ProjectReference Include="$(ProjectRootDir)/src/Attributes/CPlugin.Net.Attributes.csproj">
<!-- This tells MSBuild not to copy CPlugin.Net.Attributes.dll to the plug-in output directory. -->
<Private>false</Private>
</ProjectReference>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.