Interface IServiceLocator
- Namespace
- Splat
- Assembly
- Splat.Core.dll
Defines a contract for registering, retrieving, and managing service instances by type and optional contract name. Enables dependency resolution and service location within an application or component.
public interface IServiceLocator
- Extension Methods
Remarks
The IServiceLocator interface provides methods for registering services as singletons, transients, or lazy singletons, and for retrieving them by type or by type and contract name. It supports both direct retrieval and safe, non-throwing attempts to resolve services. This interface is commonly used to decouple service consumers from concrete implementations and to facilitate dependency injection patterns. Thread safety and lifetime management depend on the specific implementation of the interface.
Methods
AddLazySingleton<TContract>(Func<TContract>)
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.
void AddLazySingleton<TContract>(Func<TContract> instanceFactory) where TContract : class
Parameters
instanceFactoryFunc<TContract>A factory method that creates an instance of the service. The factory is invoked only once, when the service is first requested.
Type Parameters
TContractThe contract type of the service to register. Must have a public parameterless constructor.
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.
AddLazySingleton<TContract>(Func<TContract>, string)
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.
void AddLazySingleton<TContract>(Func<TContract> instanceFactory, string contract) where TContract : class
Parameters
instanceFactoryFunc<TContract>A factory method that creates an instance of the contract type. The factory is invoked only once, when the service is first requested.
contractstringThe unique contract name used to identify the service registration. Cannot be null or empty.
Type Parameters
TContractThe contract type of the service to register. Must have a public parameterless constructor.
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.
AddLazySingleton<TContract>(Func<TContract>, string, LazyThreadSafetyMode)
Registers a singleton service of the specified contract type that is created lazily using the provided factory and thread safety mode.
void AddLazySingleton<TContract>(Func<TContract> instanceFactory, string contract, LazyThreadSafetyMode threadSafetyMode) where TContract : class
Parameters
instanceFactoryFunc<TContract>A delegate that creates an instance of the contract type when the singleton is first requested. Cannot be null.
contractstringThe unique contract name used to identify the singleton registration. Cannot be null or empty.
threadSafetyModeLazyThreadSafetyModeSpecifies the thread safety mode to use when creating the singleton instance.
Type Parameters
TContractThe type of the contract to register as a singleton. Must have a public parameterless constructor.
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.
AddLazySingleton<TContract>(Func<TContract>, LazyThreadSafetyMode)
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.
void AddLazySingleton<TContract>(Func<TContract> instanceFactory, LazyThreadSafetyMode threadSafetyMode) where TContract : class
Parameters
instanceFactoryFunc<TContract>A delegate that creates an instance of the contract type when the singleton is first requested. Cannot be null.
threadSafetyModeLazyThreadSafetyModeSpecifies the thread safety mode to use when creating the singleton instance.
Type Parameters
TContractThe contract type of the service to register. Must have a public parameterless constructor.
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.
AddLazySingleton<TContract>(Lazy<TContract>)
Registers a lazily initialized singleton instance for the specified contract type.
void AddLazySingleton<TContract>(Lazy<TContract> lazy) where TContract : class
Parameters
lazyLazy<TContract>A lazily initialized instance of the contract type to register as a singleton. Cannot be null.
Type Parameters
TContractThe contract type to associate with the singleton instance. Must have a public parameterless constructor.
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.
AddLazySingleton<TContract>(Lazy<TContract>, string)
Registers a lazily initialized singleton instance for the specified contract type and contract name.
void AddLazySingleton<TContract>(Lazy<TContract> lazy, string contract) where TContract : class
Parameters
lazyLazy<TContract>A lazily initialized instance of the contract type to register as a singleton. Cannot be null.
contractstringThe contract name used to identify the singleton registration. Cannot be null or empty.
Type Parameters
TContractThe contract type to associate with the singleton instance. Must have a public parameterless constructor.
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.
AddService<T>(Func<T>)
Registers a transient service of the specified type using the provided factory function.
void AddService<T>(Func<T> instanceFactory)
Parameters
instanceFactoryFunc<T>A function that returns an instance of the service type. This function is called each time an instance is requested.
Type Parameters
TThe type of the service to register.
Remarks
Use this method to register services that require custom instantiation logic or dependencies not handled by default constructors. The factory function should not return null.
AddService<T>(Func<T>, string)
Registers a transient service implementation with the specified contract, using the provided factory to create instances of the service type.
void AddService<T>(Func<T> instanceFactory, string contract)
Parameters
instanceFactoryFunc<T>A factory function that creates instances of the service type. Cannot be null.
contractstringThe contract name under which the service is registered. Cannot be null or empty.
Type Parameters
TThe type of the service to register.
Remarks
Use this method to associate a service implementation with a specific contract, allowing for multiple implementations of the same service type to be registered under different contracts.
AddSingleton<TContract>(Func<TContract>)
Registers a singleton service of the specified contract type using the provided factory function.
void AddSingleton<TContract>(Func<TContract> instanceFactory)
Parameters
instanceFactoryFunc<TContract>A function that returns an instance of the service to be registered as a singleton. This factory is called once to create the singleton instance.
Type Parameters
TContractThe type of the service to register. The type must have a public parameterless constructor.
Remarks
Subsequent requests for the service will return the same instance created by the factory. This method is typically used to register services that should have a single shared instance for the application's lifetime.
AddSingleton<TContract>(Func<TContract>, string)
Registers a singleton service of the specified contract type using the provided factory function and associates it with the given contract name.
void AddSingleton<TContract>(Func<TContract> instanceFactory, string contract)
Parameters
instanceFactoryFunc<TContract>A function that creates an instance of the service to be registered as a singleton. This factory is called once to create the singleton instance.
contractstringThe name of the contract to associate with the registered singleton service. Cannot be null or empty.
Type Parameters
TContractThe type of the service to register. The type must have a public parameterless constructor.
Remarks
Subsequent requests for the specified contract will return the same singleton instance created by the factory. This method is typically used to register services that should have a single shared instance for the application's lifetime.
AddSingleton<TContract>(TContract)
Registers the specified instance as a singleton service for the given contract type.
void AddSingleton<TContract>(TContract instance) where TContract : class
Parameters
instanceTContractThe instance to use for the singleton service. This instance will be returned for all requests of the specified contract type. Cannot be null.
Type Parameters
TContractThe contract type of the service to register. Must be a reference type.
Remarks
Use this method to provide a specific instance that will be shared across all consumers of the contract type. The same instance is returned each time the service is requested.
AddSingleton<TContract>(TContract, string)
Registers the specified instance as a singleton for the given contract type and contract name.
void AddSingleton<TContract>(TContract instance, string contract) where TContract : class
Parameters
instanceTContractThe instance to register as a singleton. This instance will be returned for all requests matching the specified contract type and contract name.
contractstringThe contract name used to distinguish this registration. Cannot be null or empty.
Type Parameters
TContractThe contract type to associate with the singleton instance. Must be a reference type.
Remarks
Subsequent requests for the specified contract type and contract name will return the same instance. This method is typically used to provide a pre-constructed or externally managed singleton to the dependency injection container.
GetLazyService<T>()
Retrieves a lazily initialized instance of the specified service type from the dependency injection container.
Lazy<T> GetLazyService<T>()
Returns
- Lazy<T>
A Lazy<T> that provides access to the requested service instance. The service is created when the Value property is first accessed.
Type Parameters
TThe type of the service to retrieve. Must have a public parameterless constructor.
GetLazyService<T>(string)
Retrieves a lazily initialized service of the specified type associated with the given contract name.
Lazy<T> GetLazyService<T>(string contract)
Parameters
contractstringThe contract name that identifies the service to retrieve. Cannot be null.
Returns
- Lazy<T>
A Lazy<T> instance that provides access to the requested service. The service is created when the Value property is first accessed.
Type Parameters
TThe type of the service to retrieve. Must have a public parameterless constructor.
GetService<T>()
Gets an instance of the specified service type.
T GetService<T>()
Returns
- T
The service instance.
Type Parameters
TThe service type to retrieve.
Exceptions
- InvalidOperationException
Thrown when the service is not registered.
GetService<T>(string)
Gets an instance of the specified service type with a contract.
T GetService<T>(string contract)
Parameters
contractstringThe contract name.
Returns
- T
The service instance.
Type Parameters
TThe service type to retrieve.
Exceptions
- InvalidOperationException
Thrown when the service is not registered.
GetServices<T>()
Gets all registered service instances of the specified type.
IEnumerable<T> GetServices<T>()
Returns
- IEnumerable<T>
An enumerable collection of service instances of type T. The collection is empty if no services of the specified type are registered.
Type Parameters
TThe type of service to retrieve.
GetServices<T>(string)
Gets all registered service instances of the specified type that match the given contract name.
IEnumerable<T> GetServices<T>(string contract)
Parameters
contractstringThe contract name used to filter the services. Only services registered with this contract will be returned. Cannot be null.
Returns
- IEnumerable<T>
An enumerable collection of service instances of type T that match the specified contract. The collection is empty if no matching services are found.
Type Parameters
TThe type of service to retrieve.
HasService<T>()
Determines whether a service of the specified type is available from the service provider.
bool HasService<T>()
Returns
- bool
true if a service of type T is available; otherwise, false.
Type Parameters
TThe type of service to check for availability.
HasService<T>(string)
Determines whether a service of the specified type and contract is available from the service provider.
bool HasService<T>(string contract)
Parameters
contractstringThe contract name that identifies the service. Cannot be null.
Returns
- bool
true if a service of type T with the specified contract is available; otherwise, false.
Type Parameters
TThe type of the service to check for.
TryGetLazyService<T>(out Lazy<T>)
Attempts to retrieve a lazily initialized service of the specified type from the service provider.
bool TryGetLazyService<T>(out Lazy<T> service)
Parameters
serviceLazy<T>When this method returns, contains a Lazy{T} instance for the requested service if found; otherwise, null.
Returns
- bool
true if the service was found and assigned to the out parameter; otherwise, false.
Type Parameters
TThe type of service to retrieve. Must have a public parameterless constructor.
TryGetLazyService<T>(string, out Lazy<T>)
Attempts to retrieve a lazily initialized service of the specified type and contract.
bool TryGetLazyService<T>(string contract, out Lazy<T> service)
Parameters
contractstringThe contract name that uniquely identifies the requested service. Cannot be null.
serviceLazy<T>When this method returns, contains a Lazy{T} instance for the requested service if found; otherwise, null.
Returns
- bool
true if a matching service is found and assigned to the out parameter; otherwise, false.
Type Parameters
TThe type of the service to retrieve. Must have a public parameterless constructor.
Remarks
Use this method to attempt to obtain a service without throwing an exception if the service is not available. The service is created only when the Lazy{T} is evaluated.
TryGetService<T>(string, out T)
Attempts to retrieve a service of the specified type and contract.
bool TryGetService<T>(string contract, out T service)
Parameters
contractstringThe contract name that identifies the service. Cannot be null.
serviceTWhen this method returns, contains the requested service if found; otherwise, the default value for the type.
Returns
- bool
true if the service was found and returned in the out parameter; otherwise, false.
Type Parameters
TThe type of service to retrieve.
TryGetService<T>(out T)
Attempts to retrieve a service object of the specified type from the service provider.
bool TryGetService<T>(out T service)
Parameters
serviceTWhen this method returns, contains the service object of type
Tif found; otherwise, the default value for the type.
Returns
Type Parameters
TThe type of service to retrieve.