MVVMbasics Logo

mobilemotion.eu Logo


Back to Class Reference home page


MVVMbasics.Helpers
Class ServiceLocator

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.
Retrieve<T>()
Creates a service instance, stores and returns it, or throws an exception if no matching service has been registered.
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

Register

public void Register(IService service)

Registers an instance of a certain service.

Parameters:

service - Service to be registered.


Register<T>

public void Register<T>() where T : IService

Registers a reference to a certain service type, without instantly instantiating it.

Type Parameters:

T - Type of service to be registered (must be a subclass of IService).


RegisterAll

public void RegisterAll(Assembly assembly = null)

Traverses all services that are located in a given assembly and registers their types, without instantly instantiating them.

Parameters:

assembly - The assembly to be scanned. If NULL or not specified, the calling assembly will be scanned.


RegisterAll

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.

Parameters:

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.


Contains<T>

public bool Contains<T>()

Returns TRUE if a certain kind of service has been registered, FALSE otherwise.

Type Parameters:

T - Type of service to be retrieved (must be a subclass of IService).

Returns:

TRUE if a certain kind of service has been registered, FALSE otherwise.


Retrieve<T>

public T Retrieve<T>()

Creates a service instance, stores and returns it, or throws an exception if no matching service has been registered.

Type Parameters:

T - Type of service to be retrieved (must be a subclass of IService).

Returns:

Instance of the requested service.


RetrieveOnce<T>

public T RetrieveOnce<T>()

Creates and returns a service instance without storing it, or throws an exception if no matching service has been registered.

Type Parameters:

T - Type of service to be retrieved (must be a subclass of IService).

Returns:

Instance of the requested service.


TryRetrieve<T>

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.

Type Parameters:

T - Type of service to be retrieved (must be a subclass of IService).

Out Parameters:

serviceInstance - Instance of the requested service.

Returns:

TRUE if the requested service was found, FALSE otherwise.


TryRetrieveOnce<T>

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.

Type Parameters:

T - Type of service to be retrieved (must be a subclass of IService).

Out Parameters:

serviceInstance - Instance of the requested service.

Returns:

TRUE if the requested service was found, FALSE otherwise.