Service Manager

Peter.Common->Services->ServiceManager.cs [view source]

The ServiceManager is a static class that holds a dictionary of objects that are needed throughout the lifetime of an application. This class helps maintain the Model-View View-Model paradigm by making 'contracts' available to any object created during the lifetime of the application.

A 'contract' is an interface of methods and properties that a given 'service' (object) will expose for use by other objects in the application. A 'service' can be created on demand, or it can be a singleton object that will be the same every time the contract is requested.

Aside from making services available to the entire application, another purpose for the ServiceManager is to be able to replace certain services with another implementation. A useful example of this is in Unit Testing when we do not want to show dialogs that stop the flow of code. During a unit test, we can replace a service that would require user interaction with a more generic non-blocking implementation so our tests can run without interruption. We call these services 'Mock' services, because they mock the behavior of another service.

At the startup of an application, or whenever an object becomes available, you can add services to the ServiceManager by using the Add method.

ServiceManager.Add<IMessageBoxService, MessageBoxService> ();

The code above adds a generic message box service [MessageBoxService.cs] (that implements the IMesageBoxService interface) to the service manager for use throughout the application.

Any object can get a desired service with the code below (showing an example of getting the MessageBoxService. The get below will create a new object of the Type that was registered with the IMessageBoxService contract, in this instance, the pre-made MessageBoxService. NOTE: Every time Get is called for a normal contract/service relationship, a new object is created via Activator.CreateInstance

var messageBox = ServiceManager.Get<IMessageBoxService> ();
if (messageBox != null)
{
   messageBox.ShowError ("An error has occurred!", "Error");
}

If we want a service that will never change, we need to add it as a singleton, like we do for the options in Peter:

ServiceManager.AddSingleton<IPeterOptions, ModelOptions> (this.m_OptionsModel);

Now every time the IPeterOptions contract is requested, the same m_OptionsModel object will be returned as the implementation of the IPeterOptions interface.

You can also add a singleton without supplying an object. The first time the contract is requested, the service will be created via Activator.CreateInstance, and treated as a singleton.

Services that require user interaction (like windows) should be added to the ServiceManager in App.xaml.cs. This way when running unit tests, the mocked version of the service can be added without interfering with any other non-mocked service. This is because the code in App.xaml.cs will never be ran during unit tests.

Properties:

Property Return Description
Count int Gets number of services that are currently being managed by the ServiceManager.

Methods:

Method Return Description
Add <TContract, TService> () void Adds a service of Type TService, registered with the contract of Type TContract.
AddSingleton <TContract, TService> () void Adds a service of Type TService, registered with the contract of Type TContract. Only one instance of the TService will be created during the lifetime of the application.
AddSingleton <TContract, TService> (TService service) void Adds a service of Type TService, registered with the contract of Type TContract. The service object is the object with will be used whenever the TContract is requested during the lifetime of the application.
Enqueue <TContract, TService> (TService service) void Adds a service of Type TService, registered with the contract of Type TContract. The service object is added to a queue of objects that will be Dequeued when the TContract is requested.
Get <TContract> () TContract Gets the service that satisfies the Type of TContract, returning the implementation of the TContract.
Clear () void Removes all services and contracts stored in the SerivceManager.

Last edited May 5, 2014 at 3:38 PM by Jpmon1, version 12