)} ,System.Threading.LazyThreadSafetyMode)} ,System.String)} ,System.String,System.Threading.LazyThreadSafetyMode)} )} ,System.String)}
IServiceLocator.AddLazySingleton(Lazy) method¶
Defined in
Type: IServiceLocator
Namespace: Splat
Assembly: Splat.Core.dll
Applies to
net10.0, net10.0-browserwasm1.0, net10.0-desktop1.0, net9.0, net9.0-browserwasm1.0, net9.0-desktop1.0, net8.0, net8.0-ios17.5, net8.0-maccatalyst17.5, net8.0-macos14.2, net8.0-macos14.5, net8.0-tvos17.2, netstandard2.1, net462, net481
Overloads¶
- 1.
void AddLazySingleton<TContract>(Lazy<TContract> lazy) where TContract : class - 2.
void AddLazySingleton<TContract>(Func<TContract> instanceFactory, LazyThreadSafetyMode threadSafetyMode) where TContract : class - 3.
void AddLazySingleton<TContract>(Lazy<TContract> lazy, string contract) where TContract : class - 4.
void AddLazySingleton<TContract>(Func<TContract> instanceFactory, string contract, LazyThreadSafetyMode threadSafetyMode) where TContract : class - 5.
void AddLazySingleton<TContract>(Func<TContract> instanceFactory) where TContract : class - 6.
void AddLazySingleton<TContract>(Func<TContract> instanceFactory, string contract) where TContract : class
1. Overload¶
void AddLazySingleton<TContract>(Lazy<TContract> lazy) where TContract : class
Summary: Registers a lazily initialized singleton instance for the specified contract type.
Type parameters
| Name | Description |
|---|---|
TContract | The contract type to associate with the singleton instance. Must have a public parameterless constructor. |
Parameters
| Name | Type | Description |
|---|---|---|
lazy | Lazy | A lazily initialized instance of the contract type to register as a singleton. Cannot be null. |
Remarks
This method allows deferred creation of the singleton instance. The instance will be created only when first requested from the container. Subsequent requests will return the same instance.
2. Overload¶
void AddLazySingleton<TContract>(Func<TContract> instanceFactory, LazyThreadSafetyMode threadSafetyMode) where TContract : class
Summary: Registers a singleton service of the specified contract type, where the instance is created lazily using the provided factory and with the specified thread safety mode.
Type parameters
| Name | Description |
|---|---|
TContract | The contract type of the service to register. Must have a public parameterless constructor. |
Parameters
| Name | Type | Description |
|---|---|---|
instanceFactory | Func | A delegate that creates an instance of the contract type when the singleton is first requested. Cannot be null. |
threadSafetyMode | LazyThreadSafetyMode | Specifies the thread safety mode to use when creating the singleton instance. |
Remarks
The singleton instance is not created until it is first requested. The thread safety behavior during instance creation is determined by the specified thread safety mode. This method is useful for deferring expensive object creation or for services that should be instantiated only when needed.
3. Overload¶
void AddLazySingleton<TContract>(Lazy<TContract> lazy, string contract) where TContract : class
Summary: Registers a lazily initialized singleton instance for the specified contract type and contract name.
Type parameters
| Name | Description |
|---|---|
TContract | The contract type to associate with the singleton instance. Must have a public parameterless constructor. |
Parameters
| Name | Type | Description |
|---|---|---|
lazy | Lazy | A lazily initialized instance of the contract type to register as a singleton. Cannot be null. |
contract | string | The contract name used to identify the singleton registration. Cannot be null or empty. |
Remarks
Use this method to register a singleton that is created only when first requested, which can improve startup performance or defer expensive initialization. Subsequent requests for the contract will return the same instance.
4. Overload¶
void AddLazySingleton<TContract>(Func<TContract> instanceFactory, string contract, LazyThreadSafetyMode threadSafetyMode) where TContract : class
Summary: Registers a singleton service of the specified contract type that is created lazily using the provided factory and thread safety mode.
Type parameters
| Name | Description |
|---|---|
TContract | The type of the contract to register as a singleton. Must have a public parameterless constructor. |
Parameters
| Name | Type | Description |
|---|---|---|
instanceFactory | Func | A delegate that creates an instance of the contract type when the singleton is first requested. Cannot be null. |
contract | string | The unique contract name used to identify the singleton registration. Cannot be null or empty. |
threadSafetyMode | LazyThreadSafetyMode | Specifies the thread safety mode to use when creating the singleton instance. |
Remarks
The singleton instance is not created until it is first requested. The specified thread safety mode determines how concurrent access is handled during instance creation. Registering multiple singletons with the same contract name may result in unexpected behavior.
5. Overload¶
void AddLazySingleton<TContract>(Func<TContract> instanceFactory) where TContract : class
Summary: Registers a singleton service of the specified contract type using a factory method. The service instance is created lazily upon first request and reused for subsequent requests.
Type parameters
| Name | Description |
|---|---|
TContract | The contract type of the service to register. Must have a public parameterless constructor. |
Parameters
| Name | Type | Description |
|---|---|---|
instanceFactory | Func | A factory method that creates an instance of the service. The factory is invoked only once, when the service is first requested. |
Remarks
Use this method to defer the creation of a singleton service until it is actually needed. This can improve startup performance and resource usage if the service is expensive to create or may not always be required.
6. Overload¶
void AddLazySingleton<TContract>(Func<TContract> instanceFactory, string contract) where TContract : class
Summary: Registers a singleton service of the specified contract type using a factory method. The service instance is created lazily upon first request and reused for subsequent requests.
Type parameters
| Name | Description |
|---|---|
TContract | The contract type of the service to register. Must have a public parameterless constructor. |
Parameters
| Name | Type | Description |
|---|---|---|
instanceFactory | Func | A factory method that creates an instance of the contract type. The factory is invoked only once, when the service is first requested. |
contract | string | The unique contract name used to identify the service registration. Cannot be null or empty. |
Remarks
Use this method to register services that should be instantiated only when needed and shared as a singleton for the lifetime of the container. The same instance will be returned for all requests with the specified contract name.