Table of Contents

Class SimpleInjectorInitializer

Namespace
Splat.SimpleInjector
Assembly
Splat.SimpleInjector.dll

Provides a simple dependency resolver implementation using a factory-based registration model compatible with Simple Injector patterns.

public class SimpleInjectorInitializer : IDependencyResolver, IReadonlyDependencyResolver, IMutableDependencyResolver, IDisposable
Inheritance
SimpleInjectorInitializer
Implements
Extension Methods

Remarks

This class enables registration and resolution of services by type, supporting multiple factories per service. Contract-based registrations are not supported; contract parameters are ignored. Thread safety is ensured for all registration and resolution operations. This implementation is suitable for scenarios where lightweight, in-memory dependency resolution is required without advanced features such as scopes or contract-based resolution.

Constructors

SimpleInjectorInitializer()

public SimpleInjectorInitializer()

Properties

RegisteredFactories

Gets dictionary of registered factories.

public Dictionary<Type, List<Func<object?>>> RegisteredFactories { get; }

Property Value

Dictionary<Type, List<Func<object>>>

Methods

Dispose()

public void Dispose()

Dispose(bool)

Releases unmanaged and - optionally - managed resources.

protected virtual void Dispose(bool isDisposing)

Parameters

isDisposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

GetService(Type?)

Gets an instance of the given serviceType. Must return null if the service is not available (must not throw).

public object? GetService(Type? serviceType)

Parameters

serviceType Type

The object type.

Returns

object

The requested object, if found; null otherwise.

GetService(Type?, string?)

Gets an instance of the given serviceType. Must return null if the service is not available (must not throw).

public object? GetService(Type? serviceType, string? contract)

Parameters

serviceType Type

The object type.

contract string

A value which will retrieve only a object registered with the same contract.

Returns

object

The requested object, if found; null otherwise.

GetService<T>()

Gets an instance of the given T. Must return null if the service is not available (must not throw).

public T? GetService<T>()

Returns

T

The requested object, if found; null otherwise.

Type Parameters

T

The object type.

GetService<T>(string?)

Gets an instance of the given T. Must return null if the service is not available (must not throw).

public T? GetService<T>(string? contract)

Parameters

contract string

A value which will retrieve only a object registered with the same contract.

Returns

T

The requested object, if found; null otherwise.

Type Parameters

T

The object type.

GetServices(Type?)

Gets all instances of the given serviceType. Must return an empty collection if the service is not available (must not return null or throw).

public IEnumerable<object> GetServices(Type? serviceType)

Parameters

serviceType Type

The object type.

Returns

IEnumerable<object>

A sequence of instances of the requested serviceType. The sequence should be empty (not null) if no objects of the given type are available.

GetServices(Type?, string?)

Gets all instances of the given serviceType. Must return an empty collection if the service is not available (must not return null or throw).

public IEnumerable<object> GetServices(Type? serviceType, string? contract)

Parameters

serviceType Type

The object type.

contract string

A value which will retrieve only objects registered with the same contract.

Returns

IEnumerable<object>

A sequence of instances of the requested serviceType. The sequence should be empty (not null) if no objects of the given type are available.

GetServices<T>()

Gets all instances of the given T. Must return an empty collection if the service is not available (must not return null or throw).

public IEnumerable<T> GetServices<T>()

Returns

IEnumerable<T>

A sequence of instances of the requested T. The sequence should be empty (not null) if no objects of the given type are available.

Type Parameters

T

The object type.

GetServices<T>(string?)

Gets all instances of the given T. Must return an empty collection if the service is not available (must not return null or throw).

public IEnumerable<T> GetServices<T>(string? contract)

Parameters

contract string

A value which will retrieve only objects registered with the same contract.

Returns

IEnumerable<T>

A sequence of instances of the requested T. The sequence should be empty (not null) if no objects of the given type are available.

Type Parameters

T

The object type.

HasRegistration(Type?)

Determines whether a registration exists for the specified service type.

public bool HasRegistration(Type? serviceType)

Parameters

serviceType Type

The 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.

public bool HasRegistration(Type? serviceType, string? contract)

Parameters

serviceType Type

The type of the service to check for registration. Can be null to indicate a default or unspecified service type, depending on the implementation.

contract string

An 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.

public bool HasRegistration<T>()

Returns

bool

true if a registration for the specified service type exists; otherwise, false.

Type Parameters

T

The type of the service to check for registration.

HasRegistration<T>(string?)

Determines whether a registration exists for the specified service type and contract.

public bool HasRegistration<T>(string? contract)

Parameters

contract string

An 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

T

The service type to check for a registration.

Register(Func<object?>, Type?)

Registers a factory method for creating instances of the specified service type.

public void Register(Func<object?> factory, Type? serviceType)

Parameters

factory Func<object>

A delegate that returns an instance of the service. The delegate may return null if appropriate for the service.

serviceType Type

The 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.

public void Register(Func<object?> factory, Type? serviceType, string? contract)

Parameters

factory Func<object>

A delegate that returns an instance of the service to register. Cannot be null.

serviceType Type

The type of the service to register. If null, the type is inferred from the factory's return value.

contract string

An 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.

public void RegisterConstant<T>(T? value) where T : class

Parameters

value T

The constant value to register. Can be null to represent the absence of a value.

Type Parameters

T

The 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.

public void RegisterConstant<T>(T? value, string? contract) where T : class

Parameters

value T

The constant instance to register. Can be null if null values are supported by the container.

contract string

An optional contract name that uniquely identifies the registration. Can be null to register without a contract.

Type Parameters

T

The 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.

public void RegisterLazySingleton<T>(Func<T?> valueFactory) where T : class

Parameters

valueFactory Func<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

T

The 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.

public void RegisterLazySingleton<T>(Func<T?> valueFactory, string? contract) where T : class

Parameters

valueFactory Func<T>

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.

Type Parameters

T

The 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.

public void Register<T>(Func<T?> factory)

Parameters

factory Func<T>

The factory function which generates our object.

Type Parameters

T

The 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.

public void Register<T>(Func<T?> factory, string? contract)

Parameters

factory Func<T>

The factory function which generates our object.

contract string

A contract value which will indicates to only generate the value if this contract is specified.

Type Parameters

T

The 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.

public void Register<TService, TImplementation>() where TService : class where TImplementation : class, TService, new()

Type Parameters

TService

The interface or base class type to register as a service. Must be a reference type.

TImplementation

The 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.

public void Register<TService, TImplementation>(string? contract) where TService : class where TImplementation : class, TService, new()

Parameters

contract string

An 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

TService

The type of the service to register. Must be a reference type.

TImplementation

The 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.

public IDisposable ServiceRegistrationCallback(Type serviceType, Action<IDisposable> callback)

Parameters

serviceType Type

The type of the service to monitor for registration. Cannot be null.

callback Action<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.

public IDisposable ServiceRegistrationCallback(Type serviceType, string? contract, Action<IDisposable> callback)

Parameters

serviceType Type

The type of the service to monitor for registration events. Cannot be null.

contract string

An optional contract name that further qualifies the service type. May be null to match any contract.

callback Action<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.

public IDisposable ServiceRegistrationCallback<T>(Action<IDisposable> callback)

Parameters

callback Action<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

T

The 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.

public IDisposable ServiceRegistrationCallback<T>(string? contract, Action<IDisposable> callback)

Parameters

contract string

The contract name to filter service registrations. If null, the callback is invoked for all contracts.

callback Action<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

T

The 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.

public void UnregisterAll(Type? serviceType)

Parameters

serviceType Type

The 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.

public void UnregisterAll(Type? serviceType, string? contract)

Parameters

serviceType Type

The type of the service to unregister. If null, all service types are considered.

contract string

The 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.

public void UnregisterAll<T>()

Type Parameters

T

The 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.

public void UnregisterAll<T>(string? contract)

Parameters

contract string

The contract name used to identify the registrations to remove. Can be null to target registrations without a contract.

Type Parameters

T

The type of the instances to unregister.

UnregisterCurrent(Type?)

Unregisters the current instance of the specified service type from the context.

public void UnregisterCurrent(Type? serviceType)

Parameters

serviceType Type

The 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.

public void UnregisterCurrent(Type? serviceType, string? contract)

Parameters

serviceType Type

The type of the service to unregister. If null, the default service type is used.

contract string

An 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.

public void UnregisterCurrent<T>()

Type Parameters

T

The 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.

public void UnregisterCurrent<T>(string? contract)

Parameters

contract string

An optional contract name that specifies which registration to remove. If null, the default registration for type T is unregistered.

Type Parameters

T

The type of the instance to unregister.

Remarks

If no instance of type T is registered with the specified contract, this method has no effect.