IMutableDependencyResolver.RegisterLazySingleton(Func) method¶
Defined in
Type: IMutableDependencyResolver
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, net471
Overloads¶
- 1.
void RegisterLazySingleton<T>(Func<T?> valueFactory) where T : class - 2.
void RegisterLazySingleton<T>(Func<T?> valueFactory, string? contract) where T : class
1. Overload¶
void RegisterLazySingleton<T>(Func<T?> valueFactory) where T : class
Summary: Registers a singleton service of type T that is created lazily using the specified factory function.
Type parameters
| Name | Description |
|---|---|
T | The type of the service to register. Must be a reference type with a public parameterless constructor. |
Parameters
| Name | Type | Description |
|---|---|---|
valueFactory | Func | A function that provides the instance of type T when the service is first requested. The function may return null if no instance should be registered. |
Remarks
The service instance is not created until it is first requested. Subsequent requests will return the same instance. Registering multiple lazy singletons of the same type may result in only the first registration being used, depending on the container's behavior.
2. Overload¶
void RegisterLazySingleton<T>(Func<T?> valueFactory, string? contract) where T : class
Summary: Registers a singleton service of type T that is created lazily using the specified factory method.
Type parameters
| Name | Description |
|---|---|
T | The type of the service to register. Must be a reference type with a public parameterless constructor. |
Parameters
| Name | Type | Description |
|---|---|---|
valueFactory | Func | A function that provides the instance of T when the singleton is first requested. May return null if a null singleton is desired. |
contract | string? | An optional contract name used to distinguish between multiple registrations of the same service type. If null, the default contract is used. |
Remarks
The singleton instance is not created until it is first requested. Subsequent requests for the service will return the same instance. Registering multiple lazy singletons with the same contract will overwrite previous registrations.