Services in Custom ViewModels
- 4 minutes to read
This documentation topic describes how you can implement support of the Service mechanism in a custom View Model (not derived from the ViewModelBase class and not a POCO ViewModel).
If your View Model is not a POCO View Model and it is not derived from the ViewModelBase class, you can still use the Service mechanism. To implement Service support, implement the ISupportServices interface in your View Model.
The ISupportServices interface is shown below.
To implement the ISupportServices interface, use the following construction.
public class ViewModel : ISupportServices {
IServiceContainer serviceContainer = null;
protected IServiceContainer ServiceContainer {
get {
if(serviceContainer == null)
serviceContainer = new ServiceContainer(this);
return serviceContainer;
}
}
IServiceContainer ISupportServices.ServiceContainer { get { return ServiceContainer; } }
}
The ServiceContainer class implements the IServiceContainer interface that provides methods for registering and accessing services.
The IServiceContainer interface is shown below.
public interface IServiceContainer {
void Clear();
T GetService<T>(ServiceSearchMode searchMode = ServiceSearchMode.PreferLocal) where T : class;
T GetService<T>(string key, ServiceSearchMode searchMode = ServiceSearchMode.PreferLocal) where T : class;
void RegisterService(object service);
void RegisterService(string key, object service);
}
When Services (derived from the ServiceBase class) are registered in a View, they analyze their DataContext. If the DataContext is set to an ISupportServices object, a service registers itself within this object. This is accomplished by calling the IServiceContainer.RegisterService method on the ISupportServices.ServiceContainer object. Thus, the service becomes available for use within the ISupportServices object via the IServiceContainer.GetService<T> method.
The following code defines a command within a View Model that displays a message box via a dedicated Message Box service. The service is accessed via the GetService<T> method invoked on the View Model’s ServiceContainer protected property. The GetService method retrieves an actual service from a View that implements the specified IMessageBoxService interface.
public class ViewModel : ISupportServices {
IServiceContainer serviceContainer = null;
protected IServiceContainer ServiceContainer {
get {
if(serviceContainer == null)
serviceContainer = new ServiceContainer(this);
return serviceContainer;
}
}
IServiceContainer ISupportServices.ServiceContainer { get { return ServiceContainer; } }
IMessageBoxService MessageBoxService { get { return ServiceContainer.GetService<IMessageBoxService>(); } }
public ICommand ShowMessageCommand { get; private set; }
public ViewModel() {
ShowMessageCommand = new DelegateCommand(ShowMessage);
}
void ShowMessage() {
MessageBoxService.Show("Hello");
}
}
The following documentation topic contains an example that demonstrates how to support the ISupportService interface in a custom View Model and use services: DXMessageBoxService.