Table of Contents

Class ModernDependencyResolver

Namespace
Splat
Assembly
Splat.Core.dll

This class is a dependency resolver written for modern C# 5.0 times. It implements all registrations via a Factory method. With the power of Closures, you can actually implement most lifetime styles (i.e. construct per call, lazy construct, singleton) using this.

Unless you have a very compelling reason not to, this is the only class you need in order to do dependency resolution, don't bother with using a full IoC container.

public class ModernDependencyResolver : IDependencyResolver, IReadonlyDependencyResolver, IMutableDependencyResolver, IDisposable
Inheritance
ModernDependencyResolver
Implements
Extension Methods
WhenAnyMixin.WhenAny<TSender, TRet, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(TSender?, Expression<Func<TSender, T1>>, Expression<Func<TSender, T2>>, Expression<Func<TSender, T3>>, Expression<Func<TSender, T4>>, Expression<Func<TSender, T5>>, Expression<Func<TSender, T6>>, Expression<Func<TSender, T7>>, Expression<Func<TSender, T8>>, Expression<Func<TSender, T9>>, Expression<Func<TSender, T10>>, Expression<Func<TSender, T11>>, Expression<Func<TSender, T12>>, Func<IObservedChange<TSender, T1>, IObservedChange<TSender, T2>, IObservedChange<TSender, T3>, IObservedChange<TSender, T4>, IObservedChange<TSender, T5>, IObservedChange<TSender, T6>, IObservedChange<TSender, T7>, IObservedChange<TSender, T8>, IObservedChange<TSender, T9>, IObservedChange<TSender, T10>, IObservedChange<TSender, T11>, IObservedChange<TSender, T12>, TRet>)
WhenAnyMixin.WhenAny<TSender, TRet, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(TSender?, Expression<Func<TSender, T1>>, Expression<Func<TSender, T2>>, Expression<Func<TSender, T3>>, Expression<Func<TSender, T4>>, Expression<Func<TSender, T5>>, Expression<Func<TSender, T6>>, Expression<Func<TSender, T7>>, Expression<Func<TSender, T8>>, Expression<Func<TSender, T9>>, Expression<Func<TSender, T10>>, Expression<Func<TSender, T11>>, Expression<Func<TSender, T12>>, Func<IObservedChange<TSender, T1>, IObservedChange<TSender, T2>, IObservedChange<TSender, T3>, IObservedChange<TSender, T4>, IObservedChange<TSender, T5>, IObservedChange<TSender, T6>, IObservedChange<TSender, T7>, IObservedChange<TSender, T8>, IObservedChange<TSender, T9>, IObservedChange<TSender, T10>, IObservedChange<TSender, T11>, IObservedChange<TSender, T12>, TRet>, bool)

Constructors

ModernDependencyResolver()

Initializes a new instance of the ModernDependencyResolver class.

public ModernDependencyResolver()

ModernDependencyResolver(Dictionary<(Type serviceType, string? contract), List<Func<object?>>>?)

Initializes a new instance of the ModernDependencyResolver class.

protected ModernDependencyResolver(Dictionary<(Type serviceType, string? contract), List<Func<object?>>>? registry)

Parameters

registry Dictionary<(Type serviceType, string contract), List<Func<object>>>

A registry of services.

Remarks

When provided, the registry is deep-copied at the list level to avoid sharing mutable lists between resolvers.

Methods

Dispose()

public void Dispose()

Dispose(bool)

Disposes of all managed memory from this class.

protected virtual void Dispose(bool isDisposing)

Parameters

isDisposing bool

If we are currently disposing managed resources.

Duplicate()

Generates a duplicate of the resolver with all the current registrations. Useful if you want to generate temporary resolver using the WithResolver(IDependencyResolver, bool) method.

public ModernDependencyResolver Duplicate()

Returns

ModernDependencyResolver

The newly generated ModernDependencyResolver class with the current registrations.

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?)

Check to see if a resolver has a registration for a type.

public bool HasRegistration(Type? serviceType)

Parameters

serviceType Type

The type to check for registration.

Returns

bool

Whether there is a registration for the type.

HasRegistration(Type?, string?)

Check to see if a resolver has a registration for a type.

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

Parameters

serviceType Type

The type to check for registration.

contract string

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

Returns

bool

Whether there is a registration for the type.

HasRegistration<T>()

Check to see if a resolver has a registration for a type.

public bool HasRegistration<T>()

Returns

bool

Whether there is a registration for the type.

Type Parameters

T

The type to check for registration.

HasRegistration<T>(string?)

Check to see if a resolver has a registration for a type.

public bool HasRegistration<T>(string? contract)

Parameters

contract string

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

Returns

bool

Whether there is a registration for the type.

Type Parameters

T

The type to check for registration.

Register(Func<object?>, Type?)

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(Func<object?> factory, Type? serviceType)

Parameters

factory Func<object>

The factory function which generates our object.

serviceType Type

The type which is used for the registration.

Register(Func<object?>, Type?, 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(Func<object?> factory, Type? serviceType, string? contract)

Parameters

factory Func<object>

The factory function which generates our object.

serviceType Type

The type which is used for the registration.

contract string

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

RegisterConstant<T>(T?)

Registers a constant value which will always return the specified object instance.

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

Parameters

value T

The specified instance to always return.

Type Parameters

T

The service type to register for (must be a reference type).

RegisterConstant<T>(T?, string?)

Registers a constant value which will always return the specified object instance.

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

Parameters

value T

The specified instance to always return.

contract string

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

Type Parameters

T

The service type to register for (must be a reference type).

RegisterLazySingleton<T>(Func<T?>)

Registers a lazy singleton value which will always return the specified object instance once created. The value is only generated once someone requests the service from the resolver.

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

Parameters

valueFactory Func<T>

A factory method for generating a object of the specified type.

Type Parameters

T

The service type to register for (must be a reference type).

RegisterLazySingleton<T>(Func<T?>, string?)

Registers a lazy singleton value which will always return the specified object instance once created. The value is only generated once someone requests the service from the resolver.

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

Parameters

valueFactory Func<T>

A factory method for generating a object of the specified type.

contract string

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

Type Parameters

T

The service type to register for (must be a reference type).

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 factory for a service type that will be registered as a different type.

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

Type Parameters

TService

The service type to register as (interface or base class, must be a reference type).

TImplementation

The concrete implementation type (must be a reference type, have a parameterless constructor, and implement TService).

Register<TService, TImplementation>(string?)

Registers a factory for a service type that will be registered as a different type.

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

Parameters

contract string

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

Type Parameters

TService

The service type to register as (interface or base class, must be a reference type).

TImplementation

The concrete implementation type (must be a reference type, have a parameterless constructor, and implement TService).

ServiceRegistrationCallback(Type, Action<IDisposable>)

Register a callback to be called when a new service matching the type is registered.

When registered, the callback is also called for each currently matching service.

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

Parameters

serviceType Type

The type which is used for the registration.

callback Action<IDisposable>

The callback which will be called when the specified service type is registered.

Returns

IDisposable

When disposed removes the callback.

ServiceRegistrationCallback(Type, string?, Action<IDisposable>)

Register a callback to be called when a new service matching the type and contract is registered.

When registered, the callback is also called for each currently matching service.

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

Parameters

serviceType Type

The type which is used for the registration.

contract string

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

callback Action<IDisposable>

The callback which will be called when the specified service type and contract are registered.

Returns

IDisposable

When disposed removes the callback.

ServiceRegistrationCallback<T>(Action<IDisposable>)

Register a callback to be called when a new service matching the type is registered.

When registered, the callback is also called for each currently matching service.

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

Parameters

callback Action<IDisposable>

The callback which will be called when the specified service type is registered.

Returns

IDisposable

When disposed removes the callback.

Type Parameters

T

The type which is used for the registration.

ServiceRegistrationCallback<T>(string?, Action<IDisposable>)

Register a callback to be called when a new service matching the type and contract is registered.

When registered, the callback is also called for each currently matching service.

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

Parameters

contract string

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

callback Action<IDisposable>

The callback which will be called when the specified service type and contract are registered.

Returns

IDisposable

When disposed removes the callback.

Type Parameters

T

The type which is used for the registration.

UnregisterAll(Type?)

Unregisters all the values associated with the specified type.

public void UnregisterAll(Type? serviceType)

Parameters

serviceType Type

The service type to unregister.

UnregisterAll(Type?, string?)

Unregisters all the values associated with the specified type and contract.

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

Parameters

serviceType Type

The service type to unregister.

contract string

The contract value, which will only remove the value associated with the contract.

UnregisterAll<T>()

Unregisters all the values associated with the specified type.

public void UnregisterAll<T>()

Type Parameters

T

The service type to unregister.

UnregisterAll<T>(string?)

Unregisters all the values associated with the specified type and contract.

public void UnregisterAll<T>(string? contract)

Parameters

contract string

The contract value, which will only remove the value associated with the contract.

Type Parameters

T

The service type to unregister.

UnregisterCurrent(Type?)

Unregisters the current item based on the specified type.

public void UnregisterCurrent(Type? serviceType)

Parameters

serviceType Type

The service type to unregister.

UnregisterCurrent(Type?, string?)

Unregisters the current item based on the specified type and contract.

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

Parameters

serviceType Type

The service type to unregister.

contract string

The contract value, which will only remove the value associated with the contract.

UnregisterCurrent<T>()

Unregisters the current item based on the specified type.

public void UnregisterCurrent<T>()

Type Parameters

T

The service type to unregister.

UnregisterCurrent<T>(string?)

Unregisters the current item based on the specified type and contract.

public void UnregisterCurrent<T>(string? contract)

Parameters

contract string

The contract value, which will only remove the value associated with the contract.

Type Parameters

T

The service type to unregister.