ServiceRegistrationExtensions¶
Verified by tests
ServiceRegistrationGeneratorTests — library CI run #31657041675 (2026-08-13)
ServiceRegistrationExtensions is a source-generated static class containing extension methods to register discovered Perspective and Lens implementations with the dependency injection container.
Overview¶
Whizbang's source generator scans your codebase for:
- Classes implementing user interfaces that extend IPerspectiveFor<...> / IPerspectiveWithActionsFor<...> (or implementing those Whizbang interfaces directly with closed generic arguments)
- Classes implementing user interfaces that extend ILensQuery (or implementing ILensQuery<T> directly)
It then generates ServiceRegistrationExtensions with methods to register all discovered implementations as Transient services.
The generator also emits a [ModuleInitializer] that wires these methods into ServiceRegistrationCallbacks, so AddWhizbang() invokes them automatically when your assembly loads — the explicit methods below are only needed when you want to register services without calling AddWhizbang(), or with different options.
Generated Methods¶
| Method | Description |
|---|---|
AddPerspectiveServices |
Registers all discovered Perspectives |
AddLensServices |
Registers all discovered Lenses |
AddAllWhizbangServices |
Registers both Perspectives and Lenses |
Basic Usage¶
Basic Service Registration
var builder = WebApplication.CreateBuilder(args);
// Registers core Whizbang services AND auto-invokes the generated
// service registrations for discovered Perspectives and Lenses
builder.Services.AddWhizbang();
// Explicit call — only needed when bypassing AddWhizbang()
// or re-registering with different options
builder.Services.AddAllWhizbangServices();
Registration Order¶
The recommended registration order is:
Recommended Registration Order
var builder = WebApplication.CreateBuilder(args);
// 1. Database context
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(connectionString));
// 2. Core Whizbang services (auto-registers discovered services)
builder.Services.AddWhizbang();
// 3. Perspective runner registrations (for perspective materialization)
builder.Services.AddWhizbangPerspectives();
With Options¶
All registration methods accept an optional configuration action:
Registration with Options
builder.Services.AddAllWhizbangServices(options => {
// Disable self-registration (interface-only)
options.IncludeSelfRegistration = false;
});
See ServiceRegistrationOptions for available options.
Service Lifetime¶
All services are registered as Transient:
Service Lifetime
This ensures:
- A fresh instance per resolution
- Scoped dependencies (like DbContext) are supplied by the resolving scope
- No accidental state sharing between resolutions
Source Generation¶
The generator runs at compile-time and produces a file similar to:
Generated Code Structure
// <auto-generated/>
namespace Whizbang.Core.Generated;
/// <summary>
/// Extension methods for registering 3 discovered perspective service(s)
/// and 5 discovered lens service(s) with the DI container.
/// All services are registered as Transient.
/// </summary>
public static class ServiceRegistrationExtensions {
public static IServiceCollection AddPerspectiveServices(...) { ... }
public static IServiceCollection AddLensServices(...) { ... }
public static IServiceCollection AddAllWhizbangServices(...) { ... }
}
// Plus a module initializer that wires the methods into
// ServiceRegistrationCallbacks so AddWhizbang() invokes them automatically
internal static class ServiceRegistrationInitializer {
[ModuleInitializer]
internal static void Initialize() { ... }
}
See Also¶
- ServiceRegistrationOptions - Configuration options
- AddPerspectiveServices - Register Perspective services
- AddLensServices - Register Lens services
- AddAllWhizbangServices - Register all Whizbang services
- Perspectives - What Perspectives are
- Lenses - What Lenses are