Skip to content

Advanced 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>();

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 instance

To target a specific interface instead:

[Singleton(Type = typeof(IServiceA))]
public class MultiService : IServiceA, IServiceB { }
// Only registered against IServiceA

Register services with a key for disambiguation:

[Singleton(KeyedName = "primary")]
public class PrimaryCache : ICache { }
[Singleton(KeyedName = "fallback")]
public class FallbackCache : ICache { }

Prevent overwriting an existing registration:

[Singleton(TryAdd = true)]
public class DefaultLogger : ILogger { }
// Uses TryAddSingleton — won't replace an ILogger that's already registered