In this post, I’ll give a tutorial on how to apply dependency injection in DevExpress WPF application. This post assumes that you already know about dependency injection, WPF application, and also MVVM. DevExpress MVVM is one of the freely available MVVM frameworks we can use to develop a desktop application using WPF.
BackgroundA well-designed application can use dependency injection package to resolve object dependencies between modules. If one object depends on other objects, the required objects are injected to requiring object via some sort of ways. So, instead of explicitly creating objects, the required objects are created, injected, and managed by dependency injection package (third-party package) and then, ‘injected’ to the requiring object. The lifetime of the required objects is also managed by this package. Dependency Injection package is also called an IoC Container. IoC is an abbreviation of Inversion of Control.
BenefitThe main benefit of using dependency injection is loose-coupling between modules. By loose-coupling, we can have modules developed separately by different developers. It also implies that testing can be done easier because we can inject mock-up objects to the module being tested, replacing the real objects. Loose-coupling also implies that we can change or replace the implementation of required objects with other ones while maintaining minimal impact or changes on the application.
Another benefit is the automatic life-cycle management of object created by IoC Container. We don’t have to know when to create and destroy required objects. The IoC Container manages the creation, scopes, and also, the destruction of objects.
Dependencies in MVVMIn an MVVM application framework such as DevExpress-MVVM, the module dependency pattern can be seen from the following figure:

From the figure above, we can see that the views depend on view models, then view models depend on services. Dependency injection package can be used to resolve view models required by views. It can also resolve services objects required by view models.
Code SampleTo show how dependency injection works in DevExpress-MVVM WPF application, I’ll create a demo project here. To create this demo project, I use Visual Studio 2017, but you can use Visual Studio 2015 or earlier edition since WPF is a platform that already existed since .NET framework 3.5.
I’ll make a simple scenario here. In this scenario, I have one simple data entry view called: CustomerView. This view will access view model called CustomerViewModel. The view model will call CustomerRepository that acts as a service to persist data to a storage. To make this example as simple as possible, I will just make a dummy repository that does not really store data. The main emphasis of this sample is the implementation of Ioc Container to apply dependency injection.
DevExpress MVVM framework can be used freely as a separate package downloadable from NuGet package gallery. The IoC Container that I’ll use in this sample is UnityContainer, also can be freely downloaded from NuGet package gallery.
Create a WPF Project in Visual StudioOpen Visual Studio, and then create a WPF desktop project by choosing an existing template from template list. I name my project WPFDependencyInjection. This name is automatically used by the containing solution.
Download Required PackagesTo download required packages from NuGet package gallery, make sure we are connected to the internet.
Inside the Visual Studio project, we created earlier, open Package Manager Console, and type the following commands to download required packages,
- Install-Package DevExpressMvvm
- Install-Package Unity
Create CustomerView View
In Visual Studio project, create a new item of type User Control (WPF) and name it CustomerView.xaml. Then, in XAML markup of CustomerView, we’ll create a label, a text box, and a button. The view accepts a text box input that binds to the Name property of the view model. It also provides a button for the user to interact with the view for submitting data. This button is bound to SaveCommand of the view model. We also use DevExpress MVVM MessageBoxService to display the message. The final markup of CustomerView.xaml can be seen from the following code
- <UserControl x:Class="WpfDependencyInjection.CustomerView"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
- xmlns:local="clr-namespace:WpfDependencyInjection"
- mc:Ignorable="d"
- d:DesignHeight="300" d:DesignWidth="300">
- <dxmvvm:Interaction.Behaviors>
- <dxmvvm:MessageBoxService />
- </dxmvvm:Interaction.Behaviors>
- <StackPanel Orientation="Vertical" Margin="10">
- <TextBlock>This is customer view.</TextBlock>
- <TextBlock>Customer name: <TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Width="200" /></TextBlock>
- <Button Content="Save" Command="{Binding SaveCommand}" Width="50" />
- </StackPanel>
- </UserControl>
We have set up the view, but we haven’t set the data context for this view yet. We will do it later.
Create CustomerViewModel View ModelIn Visual Studio project, create a new class called CustomerViewModel, make it public, and make it a descendant of DevExpress.Mvvm.ViewModelBase. This view model has one single public bindable property called Name, and a command called SaveCommand. The source code of CustomerViewModel can be seen from the following code
- public class CustomerViewModel : ViewModelBase
- {
- public string Name
- {
- get => GetProperty(() => Name);
- set => SetProperty(() => Name, value);
- }
- public ICommand SaveCommand { get; private set; }
- public IMessageBoxService MessageBoxService => GetService<IMessageBoxService>();
- public CustomerViewModel()
- {
- SaveCommand = new DelegateCommand(Save, () => !string.IsNullOrWhiteSpace(Name), true);
- }
- private void Save()
- {
- }
- }



Gopi RaoPosted Sep 6, 2018, 1:09 AM
Good explained
Anand GanjikuntaPosted Jan 9, 2018, 3:30 AM
Superb.Keep it up Agus!