Interface IMutableDependencyResolver
- Namespace
- Splat
- Assembly
- Splat.Core.dll
Represents a mutable dependency resolver that allows for the registration, unregistration, and querying of service factories and instances at runtime. Enables dynamic management of service lifetimes and contracts within a dependency injection system.
public interface IMutableDependencyResolver
- Extension Methods
Remarks
Implementations of this interface support advanced scenarios such as registering services with specific contracts, handling multiple registrations per service type, and reacting to service registration events via callbacks. This interface is typically used to extend or modify the set of available services during application execution, such as in plugin architectures or for testing purposes. Thread safety and registration order semantics may vary by implementation; consult the specific resolver's documentation for details.
Methods
HasRegistration(Type?)
Determines whether a registration exists for the specified service type.
bool HasRegistration(Type? serviceType)
Parameters
serviceTypeTypeThe type of the service to check for registration. Can be null to indicate an unspecified service type.
Returns
- bool
true if a registration exists for the specified service type; otherwise, false.
HasRegistration(Type?, string?)
Determines whether a registration exists for the specified service type and contract.
bool HasRegistration(Type? serviceType, string? contract)
Parameters
serviceTypeTypeThe type of the service to check for registration. Can be null to indicate a default or unspecified service type, depending on the implementation.
contractstringAn optional contract name that distinguishes between multiple registrations of the same service type. Can be null or empty to indicate the default contract.
Returns
- bool
true if a registration exists for the specified service type and contract; otherwise, false.
HasRegistration<T>()
Determines whether a registration exists for the specified service type.
bool HasRegistration<T>()
Returns
- bool
true if a registration for the specified service type exists; otherwise, false.
Type Parameters
TThe type of the service to check for registration.
HasRegistration<T>(string?)
Determines whether a registration exists for the specified service type and contract.
bool HasRegistration<T>(string? contract)
Parameters
contractstringAn optional contract name that identifies a specific registration. Can be null to check for the default registration.
Returns
- bool
true if a registration exists for the specified service type and contract; otherwise, false.
Type Parameters
TThe service type to check for a registration.
Register(Func<object?>, Type?)
Registers a factory method for creating instances of the specified service type.
void Register(Func<object?> factory, Type? serviceType)
Parameters
factoryFunc<object>A delegate that returns an instance of the service. The delegate may return null if appropriate for the service.
serviceTypeTypeThe type of the service to register. If null, the type may be inferred from the factory's return type.
Remarks
Use this method to provide custom logic for creating service instances. If serviceType is null, the registration mechanism may attempt to infer the service type from the factory delegate's return type.
Register(Func<object?>, Type?, string?)
Registers a factory method for creating instances of a specified service type and contract.
void Register(Func<object?> factory, Type? serviceType, string? contract)
Parameters
factoryFunc<object>A delegate that returns an instance of the service to register. Cannot be null.
serviceTypeTypeThe type of the service to register. If null, the type is inferred from the factory's return value.
contractstringAn optional contract name that distinguishes this registration from others of the same service type. Can be null or empty for the default contract.
Remarks
Use this method to register services with custom creation logic, such as when dependencies or configuration are required at instantiation. If multiple registrations exist for the same service type and contract, the most recent registration may take precedence, depending on the container's behavior.
RegisterConstant<T>(T?)
Registers a constant value of the specified reference type for later retrieval or use.
void RegisterConstant<T>(T? value) where T : class
Parameters
valueTThe constant value to register. Can be null to represent the absence of a value.
Type Parameters
TThe reference type of the constant value to register.
RegisterConstant<T>(T?, string?)
Registers a constant instance of the specified type for use in dependency resolution.
void RegisterConstant<T>(T? value, string? contract) where T : class
Parameters
valueTThe constant instance to register. Can be null if null values are supported by the container.
contractstringAn optional contract name that uniquely identifies the registration. Can be null to register without a contract.
Type Parameters
TThe type of the constant instance to register. Must be a reference type.
Remarks
Registering a constant ensures that the same instance is returned for all requests matching the specified type and contract. If a contract is provided, the constant is associated only with that contract; otherwise, it is registered for the type without a contract.
RegisterLazySingleton<T>(Func<T?>)
Registers a singleton service of type T that is created lazily using the specified factory function.
void RegisterLazySingleton<T>(Func<T?> valueFactory) where T : class
Parameters
valueFactoryFunc<T>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.
Type Parameters
TThe type of the service to register. Must be a reference type with a public parameterless constructor.
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.
RegisterLazySingleton<T>(Func<T?>, string?)
Registers a singleton service of type T that is created lazily using the specified factory method.
void RegisterLazySingleton<T>(Func<T?> valueFactory, string? contract) where T : class
Parameters
valueFactoryFunc<T>A function that provides the instance of T when the singleton is first requested. May return null if a null singleton is desired.
contractstringAn optional contract name used to distinguish between multiple registrations of the same service type. If null, the default contract is used.
Type Parameters
TThe type of the service to register. Must be a reference type with a public parameterless constructor.
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.
Register<T>(Func<T?>)
Register a function with the resolver which will generate an object for the specified service type. Most implementations will use a stack based approach to allow for multiple items to be registered.
void Register<T>(Func<T?> factory)
Parameters
factoryFunc<T>The factory function which generates our object.
Type Parameters
TThe type which is used for the registration.
Remarks
This generic method is preferred over the non-generic Register(Func<object?>, Type?) method for better performance and type safety. It enables optimizations in resolvers like GlobalGenericFirstDependencyResolver which use static generic containers for zero-cost service resolution.
Register<T>(Func<T?>, string?)
Register a function with the resolver which will generate an object for the specified service type. Optionally a contract can be registered which will indicate that registration will only work with that contract. Most implementations will use a stack based approach to allow for multiple items to be registered.
void Register<T>(Func<T?> factory, string? contract)
Parameters
factoryFunc<T>The factory function which generates our object.
contractstringA contract value which will indicates to only generate the value if this contract is specified.
Type Parameters
TThe type which is used for the registration.
Remarks
This generic method is preferred over the non-generic Register(Func<object?>, Type?, string?) method for better performance and type safety. It enables optimizations in resolvers like GlobalGenericFirstDependencyResolver which use static generic containers for zero-cost service resolution.
Register<TService, TImplementation>()
Registers a service type and its implementation for dependency resolution.
void Register<TService, TImplementation>() where TService : class where TImplementation : class, TService, new()
Type Parameters
TServiceThe interface or base class type to register as a service. Must be a reference type.
TImplementationThe concrete implementation type to instantiate when resolving the service. Must be a reference type, implement TService, and have a public parameterless constructor.
Remarks
Subsequent requests for TService will resolve to instances of TImplementation. If the service type is already registered, this method may overwrite the existing registration depending on the implementation.
Register<TService, TImplementation>(string?)
Registers a service implementation with an optional contract name for dependency resolution.
void Register<TService, TImplementation>(string? contract) where TService : class where TImplementation : class, TService, new()
Parameters
contractstringAn optional contract name that distinguishes this registration from others of the same service type. Specify null to register the implementation without a contract.
Type Parameters
TServiceThe type of the service to register. Must be a reference type.
TImplementationThe concrete implementation type to register for the service. Must be a reference type with a public parameterless constructor.
Remarks
Use this method to associate a service interface or base class with a specific implementation, optionally under a contract name. This enables resolving different implementations of the same service type by contract. If multiple implementations are registered for the same service and contract, the behavior may depend on the container's resolution strategy.
ServiceRegistrationCallback(Type, Action<IDisposable>)
Registers a callback to be invoked when a service of the specified type is registered or becomes available.
IDisposable ServiceRegistrationCallback(Type serviceType, Action<IDisposable> callback)
Parameters
serviceTypeTypeThe type of the service to monitor for registration. Cannot be null.
callbackAction<IDisposable>The action to invoke when the service is registered. The callback receives an IDisposable representing the service registration. Cannot be null.
Returns
- IDisposable
An IDisposable that can be used to unregister the callback.
ServiceRegistrationCallback(Type, string?, Action<IDisposable>)
Registers a callback to be invoked when a service of the specified type and contract is registered or unregistered.
IDisposable ServiceRegistrationCallback(Type serviceType, string? contract, Action<IDisposable> callback)
Parameters
serviceTypeTypeThe type of the service to monitor for registration events. Cannot be null.
contractstringAn optional contract name that further qualifies the service type. May be null to match any contract.
callbackAction<IDisposable>An action to invoke when the service registration changes. The callback receives an IDisposable representing the registration. Cannot be null.
Returns
- IDisposable
An IDisposable that can be disposed to unregister the callback.
Remarks
The callback is invoked whenever a matching service is registered or unregistered. Disposing the returned IDisposable will stop further notifications.
ServiceRegistrationCallback<T>(Action<IDisposable>)
Registers a callback to be invoked when a service of type T is registered, and returns a disposable object that can be used to unregister the callback.
IDisposable ServiceRegistrationCallback<T>(Action<IDisposable> callback)
Parameters
callbackAction<IDisposable>The action to invoke when a service of type T is registered. The callback receives an IDisposable that can be used to unregister the callback.
Returns
- IDisposable
An IDisposable that, when disposed, unregisters the callback.
Type Parameters
TThe type of the service to monitor for registration events.
ServiceRegistrationCallback<T>(string?, Action<IDisposable>)
Registers a callback to be invoked when a service of type T is registered under the specified contract.
IDisposable ServiceRegistrationCallback<T>(string? contract, Action<IDisposable> callback)
Parameters
contractstringThe contract name to filter service registrations. If null, the callback is invoked for all contracts.
callbackAction<IDisposable>The action to invoke when a matching service is registered. Receives an IDisposable that can be used to unregister the callback.
Returns
- IDisposable
An IDisposable that, when disposed, unregisters the callback.
Type Parameters
TThe type of the service to monitor for registration.
Remarks
Use this method to observe dynamic service registrations and perform actions when services become available. The callback is invoked each time a matching service is registered. Disposing the returned IDisposable will stop further notifications.
UnregisterAll(Type?)
Unregisters all service registrations for the specified service type.
void UnregisterAll(Type? serviceType)
Parameters
serviceTypeTypeThe type of service for which to remove all registrations. If null, all service registrations are removed.
UnregisterAll(Type?, string?)
Unregisters all service registrations that match the specified service type and contract.
void UnregisterAll(Type? serviceType, string? contract)
Parameters
serviceTypeTypeThe type of the service to unregister. If null, all service types are considered.
contractstringThe contract name to match when unregistering services. If null, all contracts are considered.
UnregisterAll<T>()
Unregisters all instances of the specified type from the registry or container.
void UnregisterAll<T>()
Type Parameters
TThe type of objects to unregister.
Remarks
After calling this method, no instances of type T will remain registered. Subsequent requests for T may fail or result in new registrations, depending on the container's behavior.
UnregisterAll<T>(string?)
Unregisters all instances of the specified type that are associated with the given contract.
void UnregisterAll<T>(string? contract)
Parameters
contractstringThe contract name used to identify the registrations to remove. Can be null to target registrations without a contract.
Type Parameters
TThe type of the instances to unregister.
UnregisterCurrent(Type?)
Unregisters the current instance of the specified service type from the context.
void UnregisterCurrent(Type? serviceType)
Parameters
serviceTypeTypeThe type of the service to unregister. If null, the default service type is used.
Remarks
If no instance of the specified service type is registered, this method has no effect.
UnregisterCurrent(Type?, string?)
Unregisters the current instance of the specified service type and contract from the container.
void UnregisterCurrent(Type? serviceType, string? contract)
Parameters
serviceTypeTypeThe type of the service to unregister. If null, the default service type is used.
contractstringAn optional contract name that identifies the registration to remove. If null, the default contract is used.
UnregisterCurrent<T>()
Unregisters the current instance of the specified type from the context.
void UnregisterCurrent<T>()
Type Parameters
TThe type of the instance to unregister from the current context.
UnregisterCurrent<T>(string?)
Unregisters the current instance of type T associated with the specified contract, if any.
void UnregisterCurrent<T>(string? contract)
Parameters
contractstringAn optional contract name that specifies which registration to remove. If null, the default registration for type T is unregistered.
Type Parameters
TThe type of the instance to unregister.
Remarks
If no instance of type T is registered with the specified contract, this method has no effect.