DI Patterns Composed — RexDac Academy

RexDac AcademyTechnical notes · Some prior knowledge helps
0%Published notes read · this device

Principles and Architecture / Dependency Injection

DI Patterns Composed

Original notes by SunDiaL · View English source

// Decorator
services.AddScoped<Service>();
services.AddScoped<IService>(provider =>
	new LoggedService(
		provider.GetRequiredService<Service>(),
		provider.GetRequiredService<ILogger>()
	));

// Composite
services.AddScoped<DesktopNotificationService>();
services.AddScoped<MobileNotificationService>();
services.AddScoped<INotificationService>(provider =>
    new CompositeNotificationService(
    [
        provider.GetRequiredService<DesktopNotificationService>(),
        provider.GetRequiredService<MobileNotificationService>()
    ]));