Services in POCO objects

  • 3 minutes to read

The POCO mechanism provides the capability to gain access to Services. The code below is an example of how to do this.

<UserControl x:Class="ViewModelBaseSample.View" 
             xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
             xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
             xmlns:ViewModels="clr-namespace:ViewModelBaseSample.ViewModels"
             DataContext="{dxmvvm:ViewModelSource ViewModel:ViewModel}"  ...> 
    <dxmvvm:Interaction.Behaviors>
        <dx:DXMessageBoxService/>
    </dxmvvm:Interaction.Behaviors>
    ...
</UserControl>
using DevExpress.Mvvm.POCO;
...
public class ViewModel {
    public IMessageBoxService MessageBoxService { get { return this.GetService<IMessageBoxService>(); } }
}

To control the generation of service properties, use the ServiceProperty attribute or Fluent API. For instance, you can define several services of the same type and access a specific service by its name.

<UserControl x:Class="ViewModelBaseSample.View" 
             xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
             xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
             xmlns:ViewModels="clr-namespace:ViewModelBaseSample.ViewModels"
             DataContext="{dxmvvm:ViewModelSource ViewModel:ViewModel}"  ...> 
    <dxmvvm:Interaction.Behaviors>
        <dx:DXMessageBoxService x:Name="service1"/>
        <dx:DXMessageBoxService x:Name="service2"/>
    </dxmvvm:Interaction.Behaviors>
    ...
</UserControl>
public class ViewModel {
        [ServiceProperty(Key = "service1")]
        public virtual IMessageBoxService MessageBoxService1 { get { return null; } }
        [ServiceProperty(Key = "service2")]
        public virtual IMessageBoxService MessageBoxService2 { get { return null; } }
    }

As an alternative way, you can manually define properties to access services. For this, define a non-virtual property and use one of POCOViewModelExtensions' GetService extension methods as shown in the code snippet below.

using DevExpress.Mvvm.POCO;
...
public class ViewModel {
    public IMessageBoxService MessageBoxService1 { get { return this.GetService<IMessageBoxService>("service1"); } }
    public IMessageBoxService MessageBoxService2 { get { return this.GetService<IMessageBoxService>("service2"); } }
}

View Models can be related to each other with the parent-child relationship. This is achieved with the ISupportParentViewModel interface that is automatically implemented when you create a POCO object with the ViewModelSource class. You can pass the main View Model to the child using the ISupportParentViewModel.ParentViewModel property. This allows the child View Model to access Services that are registered for the main View Model. Please review the following topic for more information about this mechanism: ViewModel relationships (ISupportParentViewModel).

Here is an example that demonstrates the use of services when POCO View Models are related to each other with the parent-child relationship: DXMessageBoxService.