Skip to content

Dependency Injection

The Imprevis plugin framework uses the concept of Dependency Injection to allow you to write better code. Each plugin runner is resolved from the dependency injection container, which means it will automatically resolve any of the dependencies in the constructor.

By default, the standard Dataverse services are registered in the dependency injection container and are available for use:

  • IOrganizationServiceFactory
  • IPluginExecutionContext
  • ILogger
  • ITracingService
  • IServiceEndpointNotificationService

In addition, there are a few other services registered which will help you when writing common plugins:

  • ICacheService
  • IDataverseServiceFactory
  • IDateTimeService
  • IHttpService
  • ILoggingService
  • IPluginConfigService

If you want to register additional services, you can! This is done by overriding the ConfigureServices method of the plugin class. For example:

public class TestPlugin : Plugin<TestPluginRunner>
{
    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<IBusinessLogic, BusinessLogic>();
    }
}

This will then make the IBusinessLogic interface available for injection into the plugin runner.

public class TestPluginRunner : IPluginRunner
{
    public TestPluginRunner(IBusinessLogic logic)
    {
        Logic = logic;
    }

    public IBusinessLogic Logic { get; }

    public void Execute()
    {
        // Do something with Logic
    }
}