Categories & Tasks
Categories
Section titled “Categories”Categories solve the problem of needing different service registrations for different environments. Tag services with a category, and they’re only registered when that category is activated:
[Singleton(Category = "Test")]public class MockPaymentService : IPaymentService;
[Singleton(Category = "Production")]public class StripePaymentService : IPaymentService;// Production startup — registers all uncategorized services + "Production" tagged servicesbuilder.Services.AddShinyServiceRegistry("Production");
// Test startup — registers all uncategorized services + "Test" tagged servicesservices.AddShinyServiceRegistry("Test");
// Multiple categoriesservices.AddShinyServiceRegistry("Production", "Analytics");Startup Tasks
Section titled “Startup Tasks”Run initialization code after DI is fully configured by implementing IShinyStartupTask:
[Singleton]public class DatabaseMigrationTask : IShinyStartupTask{ public void Start() { // Run migrations, seed data, etc. }}
// Execute all registered startup tasksapp.Services.RunStartupTasks();Helper Extensions
Section titled “Helper Extensions”Additional extension methods on IServiceCollection:
// Register against all implemented interfacesservices.AddSingletonAsImplementedInterfaces<MyService>();services.AddScopedAsImplementedInterfaces<MyService>();
// Keyed variantservices.AddSingletonAsImplementedInterfaces<MyService>("keyName");
// Check registrationsbool hasService = services.HasService<IMyService>();bool hasImpl = services.HasImplementation<MyService>();
// Lazy resolutionLazy<IMyService> lazy = services.GetLazyService<IMyService>(required: true);