Class AppLocator
- Namespace
- Splat
- Assembly
- Splat.Core.dll
Provides a global access point for dependency resolution within the application, allowing services and types to be registered and retrieved at runtime.
public static class AppLocator
- Inheritance
-
AppLocator
Remarks
AppLocator exposes static members for resolving and registering dependencies using a default or custom dependency resolver. It is intended for use by both application code and libraries that require service location. In most scenarios, the default resolver is sufficient and should be used unless advanced customization or testing scenarios require replacing it. Thread safety and resolver change notifications are managed internally. For most applications, use the Current and CurrentMutable properties to access the dependency resolver.
Properties
Current
Gets the read only dependency resolver. This class is used throughout libraries for many internal operations as well as for general use by applications. If this isn't assigned on startup, a default, highly capable implementation will be used, and it is advised for most people to simply use the default implementation.
public static IReadonlyDependencyResolver Current { get; }
Property Value
- IReadonlyDependencyResolver
The dependency resolver.
CurrentMutable
Gets the mutable dependency resolver. The default resolver is also a mutable resolver, so this will be non-null. Use this to register new types on startup if you are using the default resolver.
public static IMutableDependencyResolver CurrentMutable { get; }
Property Value
Methods
AreResolverCallbackChangedNotificationsEnabled()
Indicates if the we are notifying external classes of updates to the resolver being changed.
public static bool AreResolverCallbackChangedNotificationsEnabled()
Returns
- bool
A value indicating whether the notifications are happening.
GetLocator()
Gets the current dependency resolver instance used by the application.
public static IDependencyResolver GetLocator()
Returns
- IDependencyResolver
An IDependencyResolver representing the application's current dependency resolver.
Remarks
The returned resolver provides access to registered services and dependencies. The same instance is returned on each call. This method is intended for advanced scenarios where direct access to the dependency resolver is required.
GetService<T>()
Gets an instance of the given service type. Must return null
if the service is not available (must not throw).
public static T? GetService<T>()
Returns
- T
The requested object, if found;
nullotherwise.
Type Parameters
TThe object type.
GetService<T>(string)
Gets an instance of the given service type. Must return null
if the service is not available (must not throw).
public static T? GetService<T>(string contract)
Parameters
contractstringA value which will retrieve only a object registered with the same contract.
Returns
- T
The requested object, if found;
nullotherwise.
Type Parameters
TThe object type.
GetServices<T>()
Gets all instances of the given service type. Must return an empty
collection if the service is not available (must not return null or throw).
public static IEnumerable<T> GetServices<T>()
Returns
- IEnumerable<T>
A sequence of instances of the requested service type. The sequence should be empty (not
null) if no objects of the given type are available.
Type Parameters
TThe object type.
GetServices<T>(string)
Gets all instances of the given service type. Must return an empty
collection if the service is not available (must not return null or throw).
public static IEnumerable<T> GetServices<T>(string contract)
Parameters
contractstringA value which will retrieve only objects registered with the same contract.
Returns
- IEnumerable<T>
A sequence of instances of the requested service type. The sequence should be empty (not
null) if no objects of the given type are available.
Type Parameters
TThe object type.
HasRegistration<T>()
Check to see if a resolver has a registration for a type.
public static bool HasRegistration<T>()
Returns
- bool
Whether there is a registration for the type.
Type Parameters
TThe type to check for registration.
HasRegistration<T>(string)
Check to see if a resolver has a registration for a type.
public static bool HasRegistration<T>(string contract)
Parameters
contractstringA contract value which will indicates to only check for the registration if this contract is specified.
Returns
- bool
Whether there is a registration for the type.
Type Parameters
TThe type to check for registration.
RegisterConstant<T>(T?)
Registers a constant value which will always return the specified object instance.
public static void RegisterConstant<T>(T? value) where T : class
Parameters
valueTThe specified instance to always return.
Type Parameters
TThe 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 static void RegisterConstant<T>(T? value, string contract) where T : class
Parameters
valueTThe specified instance to always return.
contractstringA contract value which will indicates to only return the value if this contract is specified.
Type Parameters
TThe 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 static void RegisterLazySingleton<T>(Func<T?> valueFactory) where T : class
Parameters
valueFactoryFunc<T>A factory method for generating a object of the specified type.
Type Parameters
TThe 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 static void RegisterLazySingleton<T>(Func<T?> valueFactory, string contract) where T : class
Parameters
valueFactoryFunc<T>A factory method for generating a object of the specified type.
contractstringA contract value which will indicates to only return the value if this contract is specified.
Type Parameters
TThe service type to register for (must be a reference type).
RegisterResolverCallbackChanged(Action)
This method allows libraries to register themselves to be set up whenever the dependency resolver changes. Applications should avoid this method, it is usually used for libraries that depend on service location.
public static IDisposable RegisterResolverCallbackChanged(Action callback)
Parameters
callbackActionA callback that is invoked when the resolver is changed. This callback is also invoked immediately, to configure the current resolver.
Returns
- IDisposable
When disposed, removes the callback. You probably can ignore this.
Register<T>(Func<T?>)
Register a function with the resolver which will generate an object for the specified service type.
public static 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.
Register<T>(Func<T?>, string)
Register a function with the resolver which will generate an object for the specified service type.
public static 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.
SetLocator(IDependencyResolver)
Sets the dependency resolver to be used by the application.
public static void SetLocator(IDependencyResolver dependencyResolver)
Parameters
dependencyResolverIDependencyResolverThe dependency resolver instance that provides service resolution for the application. Cannot be null.
Remarks
Call this method at application startup to configure the global dependency resolver. Subsequent calls will replace the existing resolver.
SuppressResolverCallbackChangedNotifications()
This method will prevent resolver changed notifications from happening until the returned IDisposable is disposed.
public static IDisposable SuppressResolverCallbackChangedNotifications()
Returns
- IDisposable
A disposable which when disposed will indicate the change notification is no longer needed.
UnregisterAll<T>()
Unregisters the all the values for the specified type and the optional contract.
public static void UnregisterAll<T>()
Type Parameters
TThe type of items to unregister.
UnregisterAll<T>(string)
Unregisters the all the values for the specified type and the optional contract.
public static void UnregisterAll<T>(string contract)
Parameters
contractstringA contract which indicates to only removed those items registered with this contract.
Type Parameters
TThe type of items to unregister.
UnregisterCurrent<T>()
Unregisters the current the value for the specified type and the optional contract.
public static void UnregisterCurrent<T>()
Type Parameters
TThe type of item to unregister.
UnregisterCurrent<T>(string)
Unregisters the current the value for the specified type and the optional contract.
public static void UnregisterCurrent<T>(string contract)
Parameters
contractstringA contract which indicates to only removed the item registered with this contract.
Type Parameters
TThe type of item to unregister.