Advanced Registration
Self Registration
Section titled “Self Registration”Register a class as itself rather than against its interface:
[Singleton(AsSelf = true)]public class AppState : IStandardInterface;
// Registers as: services.AddSingleton<AppState>();// NOT as: services.AddSingleton<IStandardInterface, AppState>();Multiple Interfaces
Section titled “Multiple Interfaces”When a class implements multiple interfaces and is registered as Singleton or Scoped, Shiny automatically registers it against all interfaces with shared instance resolution:
[Singleton]public class MultiService : IServiceA, IServiceB { }// Both IServiceA and IServiceB resolve to the same MultiService instanceTo target a specific interface instead:
[Singleton(Type = typeof(IServiceA))]public class MultiService : IServiceA, IServiceB { }// Only registered against IServiceAKeyed Services
Section titled “Keyed Services”Register services with a key for disambiguation:
[Singleton(KeyedName = "primary")]public class PrimaryCache : ICache { }
[Singleton(KeyedName = "fallback")]public class FallbackCache : ICache { }TryAdd Semantics
Section titled “TryAdd Semantics”Prevent overwriting an existing registration:
[Singleton(TryAdd = true)]public class DefaultLogger : ILogger { }// Uses TryAddSingleton — won't replace an ILogger that's already registered