-
Notifications
You must be signed in to change notification settings - Fork 4
IOC Usage Within Plugins
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.
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.
- 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
- If a
PluginServicesRegistrationRecorderAttribute
is found, it will then assert that it implements the IPluginServicesRegistrationRecorder interface - Then it will create an instance of it
- Finally, it will call the
RegisterPluginServices
method on the instance, passing in theIocContainer
, 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.
When testing plugins, there are two simple ways to add registrations.
- 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. - 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 theIIocServicProviderBuilder
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 theContainer.BuildServiceProvider
method will be used by default.