Skip to main content

Module Features

Normally, a module (pulled together using an implementation of BaseModuleRegistrar) registers all its needed services and other configurations using the ModuleRegistrar implementation. But sometimes, a module is just larger or more diverse than others. In this case, there is a construct in the BaseModuleRegistrar that allows for registering implementations of IFeatureRegistrar types that will have the same module-startup hooks as a ModuleRegistrar itself.

Usage

To add an IFeatureRegistrar implementation in an implementation of a BaseModuleRegistrar, simply override the IEnumerable<Type> FeatureRegistrars { get } property and define all types of your feature-registrars (classes that implement IFeatureRegistrar). All the registered classes will be constructed, and the Registrar methods will be called after the equivalent Module-Registrar methods.

IFeatureRegistrar Implementation

Basically, you just have to implement all methods from the IFeatureRegistrar interface. But for convenience, there is a BaseFeatureRegistrar that implements each method as virtual, so you only have to override the needed ones. You can choose either of the two methods to implement your feature-registrar, as long as the interface implementing class contains a parameter-less constructor or at most a constructor with one parameter of IModuleRegistrar.

Code Reference

/// <summary>
/// Represents a feature registrar that can be used to split a module (and the initialization) into multiple features.
/// </summary>
public interface IFeatureRegistrar
{
/// <summary>
/// Additional registrations for IBuilder implementations to be used in the feature.
/// </summary>
Dictionary<Type, Action<IBuilder>> ConfigureBuilders { get; }

/// <summary>
/// Tracking-HashSet for executed builders.
/// </summary>
HashSet<Type> ExecutedBuilders { get; }

/// <summary>
/// Registers feature services needed for this feature.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to register the services.</param>
void ConfigureFeatureServices(IServiceCollection services);

/// <summary>
/// Configures the feature's app definition.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to configure the app.</param>
/// <param name="env">The <see cref="IWebHostEnvironment"/> to configure the app.</param>
void ConfigureFeature(IApplicationBuilder app, IWebHostEnvironment env);

/// <summary>
/// Configures the feature's authorization options.
/// </summary>
/// <param name="options">The options to configure.</param>
void ConfigureAuthorization(AuthorizationOptions options);

/// <summary>
/// Configures the feature's queue.
/// </summary>
/// <param name="configurator">The queue configurator.</param>
void ConfigureFeatureQueue(IBusRegistrationConfigurator configurator);

/// <summary>
/// Configures the feature's RabbitMq features.
/// </summary>
/// <param name="context">The Bus-Registration context to configure.</param>
/// <param name="cfg">The RabbitMq bus configuration.</param>
void ConfigureRabbitMqFeatures(IBusRegistrationContext context, IRabbitMqBusFactoryConfigurator cfg);

/// <summary>
/// Configures the feature's health checks.
/// </summary>
/// <param name="healthChecksBuilder">The <see cref="IHealthChecksBuilder"/> to configure.</param>
void HealthCheckConfiguration(IHealthChecksBuilder healthChecksBuilder);
}