Skip to content

IOC Usage Within Plugins

Daryl LaBar edited this page Sep 5, 2024 · 5 revisions

The IoC container usage within the plugins is designed to be flexible, but defaulting to the desired behavior. The ideal scenario is to have a single place to register all of the services/dependencies, which also allows for individual plugin customization if needed, and simple injection during unit testing.

Here are the options when it comes to registering and organizing plugin dependencies.

Injecting your own IIocContainer

The DLaBGenericPluginBase constructor does allow for injecting an IIocContainer. Be aware that the DLaBGenerigPluginBase.RegisterServices method will still be called and step on any potential registrations that are already defined in the container, although this method can be overridden to prevent this from happening if so desired.

Registering custom services/dependencies via the DLaBGenericPluginBase.RegisterServices method.

This method defines how custom dependencies are registered and works in the following order.

  1. First it will attempt to locate a class in the plugin assembly that implements the PluginServicesRegistrationRecorderAttribute. If none is found, it will just call the IIocContiainer.RegisterDataversePluginDefaults extension method
  2. If a PluginServicesRegistrationRecorderAttribute is found, it will then assert that it implements the IPluginServicesRegistrationRecorder interface
  3. Then it will create an instance of it
  4. Finally, it will call the RegisterPluginServices method on the instance, passing in the IocContainer, the plugin, and the unsecure and secure config string values.

This allows for defining different registrations per plugin assembly, or having a single class in a shared assembly. It is recommended to simplify the logic in the RegisterPluginServices by first calling the IIocContiainer.RegisterDataversePluginDefaults extension method, before defining customer registrations.

Overriding Registrations for Testing

When testing plugins, there are two simple ways to add registrations.

  1. Utilize the DLaBGenericPluginBase.Container property.
    This property exposes the Container of the plugin itself, after it has had the DLaBGenerigPluginBase.RegisterServices method executed, allowing any registration to be overwritten via the standard IocContainer registration methods.
  2. Utilize the IIocServiceProviderBuilder interface.
    This gives complete control over the IServiceProvider utilized by the plugin. The IServiceProvider passed into the Execute method of the plugin will just need to provide the IIocServicProviderBuilder which will return the actual ServiceProvider utilized by the plugin. In the normal execution of the plugin on the server, the IServiceProvider will return null for this type, and the Container.BuildServiceProvider method will be used by default.