Class FuncDependencyResolver
- Namespace
- Splat
- Assembly
- Splat.dll
A simple dependency resolver which takes Funcs for all its actions. GetService is always implemented via GetServices().LastOrDefault(). This container is not thread safe.
public class FuncDependencyResolver : IDependencyResolver, IReadonlyDependencyResolver, IMutableDependencyResolver, IDisposable
- Inheritance
-
FuncDependencyResolver
- Implements
- Extension Methods
Remarks
Initializes a new instance of the FuncDependencyResolver class.
Constructors
FuncDependencyResolver(Func<Type?, string?, IEnumerable<object>>, Action<Func<object?>, Type?, string?>?, Action<Type?, string?>?, Action<Type?, string?>?, IDisposable?)
A simple dependency resolver which takes Funcs for all its actions. GetService is always implemented via GetServices().LastOrDefault(). This container is not thread safe.
public FuncDependencyResolver(Func<Type?, string?, IEnumerable<object>> getAllServices, Action<Func<object?>, Type?, string?>? register = null, Action<Type?, string?>? unregisterCurrent = null, Action<Type?, string?>? unregisterAll = null, IDisposable? toDispose = null)
Parameters
getAllServices
Func<Type, string, IEnumerable<object>>A func which will return all the services contained for the specified service type and contract.
register
Action<Func<object>, Type, string>A func which will be called when a service type and contract are registered.
unregisterCurrent
Action<Type, string>A func which will unregister the current registered element for a service type and contract.
unregisterAll
Action<Type, string>A func which will unregister all the registered elements for a service type and contract.
toDispose
IDisposableA optional disposable which is called when this resolver is disposed.
Remarks
Initializes a new instance of the FuncDependencyResolver class.
Methods
Dispose()
public void Dispose()
Dispose(bool)
Disposes of all managed memory from this class.
protected virtual void Dispose(bool isDisposing)
Parameters
isDisposing
boolIf we are currently disposing managed resources.
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 = null)
Parameters
serviceType
TypeThe object type.
contract
stringA optional value which will retrieve only a object registered with the same contract.
Returns
- object
The requested object, if found;
null
otherwise.
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 = null)
Parameters
serviceType
TypeThe object type.
contract
stringA optional 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 (notnull
) if no objects of the given type are available.
HasRegistration(Type?, string?)
Check to see if a resolvers has a registration for a type.
public bool HasRegistration(Type? serviceType, string? contract = null)
Parameters
serviceType
TypeThe type to check for registration.
contract
stringA optional 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.
Register(Func<object?>, Type?, string?)
Register a function with the resolver which will generate a 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 multile items to be registered.
public void Register(Func<object?> factory, Type? serviceType, string? contract = null)
Parameters
factory
Func<object>The factory function which generates our object.
serviceType
TypeThe type which is used for the registration.
contract
stringA optional contract value which will indicates to only generate the value if this contract is specified.
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
TypeThe type which is used for the registration.
contract
stringA optional 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.
UnregisterAll(Type?, string?)
Unregisters all the values associated with the specified type and contract.
public void UnregisterAll(Type? serviceType, string? contract = null)
Parameters
serviceType
TypeThe service type to unregister.
contract
stringThe optional contract value, which will only remove the value associated with the contract.
UnregisterCurrent(Type?, string?)
Unregisters the current item based on the specified type and contract.
public void UnregisterCurrent(Type? serviceType, string? contract = null)