Class DependencyResolverMixins
- Namespace
- Splat
- Assembly
- Splat.Core.dll
Provides extension methods for registering and managing dependency resolvers and service instances in a dependency injection system.
public static class DependencyResolverMixins
- Inheritance
-
DependencyResolverMixins
Remarks
These mixin methods extend dependency resolver interfaces to simplify common registration patterns, such as registering constant or singleton services. They are intended to be used in scenarios where dynamic or test-time configuration of dependency resolution is required.
Methods
RegisterConstant(IMutableDependencyResolver, object?, Type)
Registers a constant value as a service implementation for the specified service type in the dependency resolver.
public static void RegisterConstant(this IMutableDependencyResolver resolver, object? value, Type serviceType)
Parameters
resolverIMutableDependencyResolverThe dependency resolver in which to register the constant value. Cannot be null.
valueobjectThe constant value to register as the service implementation. May be null if the service type allows null values.
serviceTypeTypeThe type of service to associate with the constant value. Cannot be null.
Remarks
After registration, requests for the specified service type will always return the provided constant value. This method is typically used to register singleton or pre-constructed instances.
RegisterConstant(IMutableDependencyResolver, object?, Type, string)
Registers a constant value as a service implementation for the specified service type and contract in the dependency resolver.
public static void RegisterConstant(this IMutableDependencyResolver resolver, object? value, Type serviceType, string contract)
Parameters
resolverIMutableDependencyResolverThe dependency resolver in which to register the constant value. Cannot be null.
valueobjectThe constant value to register as the service implementation. May be null if the service type allows null values.
serviceTypeTypeThe type of the service to associate with the constant value. Cannot be null.
contractstringThe contract name to associate with the registration. Use null or an empty string to register the service without a contract.
Remarks
After registration, all requests for the specified service type and contract will return the provided constant value. This method is typically used to register singleton or pre-constructed instances.
RegisterLazySingleton(IMutableDependencyResolver, Func<object?>, Type)
Registers a singleton service with lazy initialization in the specified dependency resolver.
public static void RegisterLazySingleton(this IMutableDependencyResolver resolver, Func<object?> valueFactory, Type serviceType)
Parameters
resolverIMutableDependencyResolverThe dependency resolver in which to register the singleton service. Cannot be null.
valueFactoryFunc<object>A delegate that provides the value to be used as the singleton instance. The factory is invoked only once, upon first resolution.
serviceTypeTypeThe type of the service to register as a singleton. Cannot be null.
Remarks
The singleton instance is created lazily and is shared for all subsequent resolutions of the specified service type. This method is thread-safe and ensures that the value factory is invoked only once, even in multithreaded scenarios.
RegisterLazySingleton(IMutableDependencyResolver, Func<object?>, Type, string)
Registers a singleton service in the dependency resolver using lazy initialization. The service instance is created on first request and shared for all subsequent requests with the specified contract.
public static void RegisterLazySingleton(this IMutableDependencyResolver resolver, Func<object?> valueFactory, Type serviceType, string contract)
Parameters
resolverIMutableDependencyResolverThe dependency resolver in which to register the singleton service. Cannot be null.
valueFactoryFunc<object>A delegate that provides the instance of the service when it is first requested. Cannot be null.
serviceTypeTypeThe type of the service to register. Cannot be null.
contractstringThe contract string used to distinguish between multiple registrations of the same service type. Cannot be null.
Remarks
The service instance is created only once, upon the first request, and the same instance is returned for all subsequent requests with the specified contract. This method is thread-safe and ensures that the service is initialized only once, even in multithreaded scenarios.
WithResolver(IDependencyResolver, bool)
Temporarily replaces the current application dependency resolver with the specified resolver, restoring the original resolver when the returned object is disposed.
public static IDisposable WithResolver(this IDependencyResolver resolver, bool suppressResolverCallback = true)
Parameters
resolverIDependencyResolverThe dependency resolver to set as the current resolver. Cannot be null.
suppressResolverCallbackbooltrue to suppress resolver callback changed notifications while the resolver is replaced; otherwise, false. The default is true.
Returns
- IDisposable
An IDisposable that, when disposed, restores the original dependency resolver and re-enables notifications if they were suppressed.
Remarks
Use this method to temporarily override the application's dependency resolver within a specific scope. This is useful for testing or for scenarios where a different resolver is needed temporarily. Ensure that the returned IDisposable is properly disposed to avoid leaving the application in an inconsistent state.