SingletonAttribute
NexusLabs.Needlr¶
NexusLabs.Needlr¶
SingletonAttribute Class¶
Specifies that the decorated class should be registered with \<b>Singleton\</b> lifetime. Singleton services are created once for the lifetime of the application and reused for every request.
Inheritance System.Object 🡒 System.Attribute 🡒 SingletonAttribute
Example¶
[Singleton]
public class ConfigurationCache : IConfigurationCache
{
// Created once for the application lifetime
}
Remarks¶
Singleton is the default lifetime in Needlr. You only need to apply this attribute explicitly when you want to document intent clearly, or when overriding a different default set in your Needlr configuration.
Be careful with Singleton services that depend on Scoped or Transient services — resolving a shorter-lived dependency from a Singleton creates a "captive dependency" and can cause bugs.