Back to Class Reference home page
MVVMbasics.Helpers.ServiceLocator
public class ServiceLocator
Service locator that allows services to be registered and retrieved. All services must implement the IService interface in order to be detected by ServiceLocator
.
Method Summary | |
---|---|
void |
Register(IService service)
Registers an instance of a certain service. |
void |
Register<T>() where T : IService
Registers a reference to a certain service type, without instantly instantiating it. |
void |
RegisterAll(Assembly assembly = null)
Traverses all services that are located in a given assembly and registers their types, without instantly instantiating them. |
void |
RegisterAll(string ns, Assembly assembly = null)
Traverses all services that are located in a given namespace inside a given assembly and registers their types, without instantly instantiating them. |
bool |
Contains<T>()
Returns TRUE if a certain kind of service has been registered, FALSE otherwise. |
T |
Retrieve<T>()
Creates a service instance, stores and returns it, or throws an exception if no matching service has been registered. |
T |
RetrieveOnce<T>()
Creates and returns a service instance without storing it, or throws an exception if no matching service has been registered. |
bool |
TryRetrieve<T>(out T serviceInstance)
Creates a service instance, stores it, and returns TRUE, if a matching service has been registered. If no matching service is found, returns FALSE. |
bool |
TryRetrieveOnce<T>(out T serviceInstance)
Creates a service instance and returns TRUE, if a matching service has been registered. If no matching service is found, returns FALSE. |
Method Detail |
---|
public void Register(IService service)
Registers an instance of a certain service.
service
- Service to be registered.
public void Register<T>() where T : IService
Registers a reference to a certain service type, without instantly instantiating it.
T
- Type of service to be registered (must be a subclass of IService).
public void RegisterAll(Assembly assembly = null)
Traverses all services that are located in a given assembly and registers their types, without instantly instantiating them.
assembly
- The assembly to be scanned. If NULL or not specified, the calling assembly will be scanned.
public void RegisterAll(string ns, Assembly assembly = null)
Traverses all services that are located in a given namespace inside a given assembly and registers their types, without instantly instantiating them.
ns
- Namespace to be scanned. If ends with '.*', also sub-directories will be scanned. If NULL, the whole assembly will be scanned.
assembly
- The assembly to be scanned. If NULL, the calling assembly will be scanned.
public bool Contains<T>()
Returns TRUE if a certain kind of service has been registered, FALSE otherwise.
T
- Type of service to be retrieved (must be a subclass of IService).
TRUE if a certain kind of service has been registered, FALSE otherwise.
public T Retrieve<T>()
Creates a service instance, stores and returns it, or throws an exception if no matching service has been registered.
T
- Type of service to be retrieved (must be a subclass of IService).
Instance of the requested service.
public T RetrieveOnce<T>()
Creates and returns a service instance without storing it, or throws an exception if no matching service has been registered.
T
- Type of service to be retrieved (must be a subclass of IService).
Instance of the requested service.
public bool TryRetrieve<T>(out T serviceInstance)
Creates a service instance, stores it, and returns TRUE, if a matching service has been registered. If no matching service is found, returns FALSE.
T
- Type of service to be retrieved (must be a subclass of IService).
serviceInstance
- Instance of the requested service.
TRUE if the requested service was found, FALSE otherwise.
public bool TryRetrieveOnce<T>(out T serviceInstance)
Creates a service instance and returns TRUE, if a matching service has been registered. If no matching service is found, returns FALSE.
T
- Type of service to be retrieved (must be a subclass of IService).
serviceInstance
- Instance of the requested service.
TRUE if the requested service was found, FALSE otherwise.