# Services in Generated View Models | WPF Controls | DevExpress Documentation

[Runtime-generated POCO View Models](/WPF/17352/mvvm-framework/viewmodels/runtime-generated-poco-viewmodels) and [View Models Generated at Compile Time](/WPF/402989/mvvm-framework/viewmodels/compile-time-generated-viewmodels) can work with Services. 

## Services in POCO

- XAML

<section id="tabpanel_LPudlWPXjb_tabid-xaml" role="tabpanel" data-tab="tabid-xaml">
<pre><code class="lang-xaml">&lt;UserControl x:Class=&quot;ViewModelBaseSample.View&quot; 
             xmlns:dxmvvm=&quot;http://schemas.devexpress.com/winfx/2008/xaml/mvvm&quot; 
             xmlns:dx=&quot;http://schemas.devexpress.com/winfx/2008/xaml/core&quot;
             xmlns:ViewModels=&quot;clr-namespace:ViewModelBaseSample.ViewModels&quot;
             DataContext=&quot;{dxmvvm:ViewModelSource ViewModel:ViewModel}&quot;  ...&gt; 
    &lt;dxmvvm:Interaction.Behaviors&gt;
        &lt;dx:DXMessageBoxService/&gt;
    &lt;/dxmvvm:Interaction.Behaviors&gt;
    ...
&lt;/UserControl&gt;
</code></pre></section>

- C#
- VB.NET

<section id="tabpanel_LPudlWPXjb-1_tabid-csharp" role="tabpanel" data-tab="tabid-csharp">
<pre><code class="lang-csharp">using DevExpress.Mvvm.POCO;
...
public class ViewModel {
    public IMessageBoxService MessageBoxService { get { return this.GetService&lt;IMessageBoxService&gt;(); } }
}
</code></pre></section>
<section id="tabpanel_LPudlWPXjb-1_tabid-vb" role="tabpanel" data-tab="tabid-vb" aria-hidden="true" hidden="hidden">
<pre><code class="lang-vb">Imports DevExpress.Mvvm.POCO
...
Public Class ViewModel
    Public ReadOnly Property MessageBoxService() As IMessageBoxService
        Get
            Return Me.GetService(Of IMessageBoxService)()
        End Get
    End Property
End Class
</code></pre></section>

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.

- XAML

<section id="tabpanel_LPudlWPXjb-2_tabid-xaml" role="tabpanel" data-tab="tabid-xaml">
<pre><code class="lang-xaml">&lt;UserControl x:Class=&quot;ViewModelBaseSample.View&quot; 
             xmlns:dxmvvm=&quot;http://schemas.devexpress.com/winfx/2008/xaml/mvvm&quot; 
             xmlns:dx=&quot;http://schemas.devexpress.com/winfx/2008/xaml/core&quot;
             xmlns:ViewModels=&quot;clr-namespace:ViewModelBaseSample.ViewModels&quot;
             DataContext=&quot;{dxmvvm:ViewModelSource ViewModel:ViewModel}&quot;  ...&gt; 
    &lt;dxmvvm:Interaction.Behaviors&gt;
        &lt;dx:DXMessageBoxService x:Name=&quot;service1&quot;/&gt;
        &lt;dx:DXMessageBoxService x:Name=&quot;service2&quot;/&gt;
    &lt;/dxmvvm:Interaction.Behaviors&gt;
    ...
&lt;/UserControl&gt;
</code></pre></section>

- C#
- VB.NET

<section id="tabpanel_LPudlWPXjb-3_tabid-csharp" role="tabpanel" data-tab="tabid-csharp">
<pre><code class="lang-csharp">public class ViewModel {
        [ServiceProperty(Key = &quot;service1&quot;)]
        public virtual IMessageBoxService MessageBoxService1 { get { return null; } }
        [ServiceProperty(Key = &quot;service2&quot;)]
        public virtual IMessageBoxService MessageBoxService2 { get { return null; } }
    }
</code></pre></section>
<section id="tabpanel_LPudlWPXjb-3_tabid-vb" role="tabpanel" data-tab="tabid-vb" aria-hidden="true" hidden="hidden">
<pre><code class="lang-vb">Public Class ViewModel
    &lt;ServiceProperty(Key := &quot;service1&quot;)&gt; _
    Public Overridable ReadOnly Property MessageBoxService1 As IMessageBoxService
        Get
            Return Nothing
        End Get
    End Property
    &lt;ServiceProperty(Key := &quot;service2&quot;)&gt; _
    Public Overridable ReadOnly Property MessageBoxService2 As IMessageBoxService
        Get
            Return Nothing
        End Get
    End Property
End Class
</code></pre></section>

Alternatively, you can manually define properties to access services. To do so, define a non-virtual property and use one of **POCOViewModelExtensions**‘ **GetService** extension methods as shown in the code snippet below.

- C#
- VB.NET

<section id="tabpanel_LPudlWPXjb-4_tabid-csharp" role="tabpanel" data-tab="tabid-csharp">
<pre><code class="lang-csharp">using DevExpress.Mvvm.POCO;
...
public class ViewModel {
    public IMessageBoxService MessageBoxService1 { get { return this.GetService&lt;IMessageBoxService&gt;(&quot;service1&quot;); } }
    public IMessageBoxService MessageBoxService2 { get { return this.GetService&lt;IMessageBoxService&gt;(&quot;service2&quot;); } }
}
</code></pre></section>
<section id="tabpanel_LPudlWPXjb-4_tabid-vb" role="tabpanel" data-tab="tabid-vb" aria-hidden="true" hidden="hidden">
<pre><code class="lang-vb">Imports DevExpress.Mvvm.POCO
...
Public Class ViewModel
    Public ReadOnly Property MessageBoxService1() As IMessageBoxService
        Get
            Return Me.GetService(Of IMessageBoxService)(&quot;service1&quot;)
        End Get
    End Property
    Public ReadOnly Property MessageBoxService2() As IMessageBoxService
        Get
            Return Me.GetService(Of IMessageBoxService)(&quot;service1&quot;)
        End Get
    End Property
End Class
</code></pre></section>

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)](/WPF/17449/mvvm-framework/viewmodels/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](/WPF/17415/mvvm-framework/services/predefined-set/message-box-services/dxmessageboxservice).

## Services in View Models Generated at Compile Time

To use Services in [View Models Generated at Compile Time](/WPF/402989/mvvm-framework/viewmodels/compile-time-generated-viewmodels), set the **ImplementISupportServices** property of the **GenerateViewModel** attribute to **true**. 

- Base View Model

<section id="tabpanel_LPudlWPXjb-5_tabid-services-base" role="tabpanel" data-tab="tabid-services-base">
<pre><code data-highlight-lines="[[1]]" class="lang-csharp">[GenerateViewModel(ImplementISupportServices = true)]
public partial class ServicesViewModel {
    IMessageBoxService MessageBoxService =&gt; ServiceContainer.GetService&lt;IMessageBoxService&gt;(ServiceSearchMode.PreferParents);
    [GenerateCommand]
    void ShowMessage() =&gt; MessageBoxService.ShowMessage(&quot;Message&quot;);
}
</code></pre></section>

- Generated View Model

<section id="tabpanel_LPudlWPXjb-6_tabid-services-viewmodel" role="tabpanel" data-tab="tabid-services-viewmodel">
<pre><code data-highlight-lines="[[1],[3,5]]" class="lang-csharp">partial class ServicesViewModel : INotifyPropertyChanged, ISupportServices {
    public event PropertyChangedEventHandler? PropertyChanged;
    IServiceContainer? serviceContainer;
    protected IServiceContainer ServiceContainer { get =&gt; serviceContainer ??= new ServiceContainer(this); }
    IServiceContainer ISupportServices.ServiceContainer { get =&gt; ServiceContainer; }
    protected T? GetService&lt;T&gt;() where T : class =&gt; ServiceContainer.GetService&lt;T&gt;();
    protected T GetRequiredService&lt;T&gt;() where T : class =&gt; ServiceContainer.GetRequiredService&lt;T&gt;();

    protected void RaisePropertyChanged(PropertyChangedEventArgs e) =&gt; PropertyChanged?.Invoke(this, e);

    DelegateCommand? showMessageCommand;
    public DelegateCommand ShowMessageCommand =&gt; showMessageCommand ??= new DelegateCommand(ShowMessage, null, true);
}
</code></pre></section>