Dependency and middleware registrations¶
When developing Framework Modules, it is quite common that they need custom
dependency injection registrations, or calling extension methods on
IServiceCollection
, or registering one/multiple middlewares.
When the ISuiteModule
implementation gets big, either by ConfigureServices
or ConfigureMiddleware
, we recommend refactoring into internal extension
methods over IServiceCollection
to clean up the module's definition file, and
improve its maintainability.
Extension methods can be defined either for dependency registrations, and/or for middleware registrations. In both cases the goal is the same: maintainability.
As an example, let's take the following module's definition:
After moving registrations to extension method, the module's definition looks like this:
C# | |
---|---|
Finally, the extension method is defined like this:
As said before, the same strategy could be taken for middleware registration, in
such case the method should be named as Use{ModuleName}
instead of the Add
suffix.
Convention¶
- Make extension methods internals. These are not to be reused by consumers of the module.
- Declare extension methods in the same namespace as the module, which should
be
ITsynch.Suite.App.Modules
. - For
ConfigureServices
, declare the extension method overIServiceCollection
following the naming convention:Add{ModuleName}Module
. i.eAddServiceDiscoveryModule
. - For
ConfigureMiddlewares
, declare the extension method overIApplicationBuilder
following the naming conventionUse{ModuleName}Module
. i.eUseServiceDiscoveryModule
. - If required, provide the
ModuleConfigurationContext
or your ModuleOptions instance,ILogger
, etc as the extension method's arguments.