Skip to content

Commit 87a42ec

Browse files
committed
Constructor injection for ViewModels
1 parent 34b70a0 commit 87a42ec

38 files changed

+167
-76
lines changed

samples/MvvmSample.Core/ViewModels/AsyncRelayCommandPageViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
using System.Threading.Tasks;
66
using CommunityToolkit.Mvvm.Input;
7+
using MvvmSample.Core.Services;
78

89
namespace MvvmSample.Core.ViewModels;
910

1011
public class AsyncRelayCommandPageViewModel : SamplePageViewModel
1112
{
12-
public AsyncRelayCommandPageViewModel()
13+
public AsyncRelayCommandPageViewModel(IFilesService filesService) : base(filesService)
1314
{
1415
DownloadTextCommand = new AsyncRelayCommand(DownloadTextAsync);
1516
}

samples/MvvmSample.Core/ViewModels/IocPageViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ namespace MvvmSample.Core.ViewModels;
66

77
public class IocPageViewModel : SamplePageViewModel
88
{
9+
public IocPageViewModel(IFilesService filesService) : base(filesService)
10+
{
11+
}
912
}

samples/MvvmSample.Core/ViewModels/MessengerPageViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
using CommunityToolkit.Mvvm.Input;
88
using CommunityToolkit.Mvvm.Messaging;
99
using CommunityToolkit.Mvvm.Messaging.Messages;
10+
using MvvmSample.Core.Services;
1011

1112
namespace MvvmSample.Core.ViewModels;
1213

1314
public class MessengerPageViewModel : SamplePageViewModel
1415
{
15-
public MessengerPageViewModel()
16+
public MessengerPageViewModel(IFilesService filesService) : base(filesService)
1617
{
1718
RequestCurrentUsernameCommand = new RelayCommand(RequestCurrentUsername);
1819
ResetCurrentUsernameCommand = new RelayCommand(ResetCurrentUsername);

samples/MvvmSample.Core/ViewModels/ObservableObjectPageViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
using System.Threading.Tasks;
66
using System.Windows.Input;
77
using CommunityToolkit.Mvvm.Input;
8+
using MvvmSample.Core.Services;
89

910
namespace MvvmSample.Core.ViewModels;
1011

1112
public class ObservableObjectPageViewModel : SamplePageViewModel
1213
{
13-
public ObservableObjectPageViewModel()
14+
public ObservableObjectPageViewModel(IFilesService filesService) : base(filesService)
1415
{
1516
ReloadTaskCommand = new RelayCommand(ReloadTask);
1617
}

samples/MvvmSample.Core/ViewModels/RelayCommandPageViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
using System.Windows.Input;
66
using CommunityToolkit.Mvvm.Input;
7+
using MvvmSample.Core.Services;
78

89
namespace MvvmSample.Core.ViewModels;
910

1011
public class RelayCommandPageViewModel : SamplePageViewModel
1112
{
12-
public RelayCommandPageViewModel()
13+
public RelayCommandPageViewModel(IFilesService filesService) : base(filesService)
1314
{
1415
IncrementCounterCommand = new RelayCommand(IncrementCounter);
1516
}

samples/MvvmSample.Core/ViewModels/SamplePageViewModel.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Text.RegularExpressions;
88
using System.Threading.Tasks;
99
using CommunityToolkit.Mvvm.ComponentModel;
10-
using CommunityToolkit.Mvvm.DependencyInjection;
1110
using CommunityToolkit.Mvvm.Input;
1211
using MvvmSample.Core.Helpers;
1312
using MvvmSample.Core.Services;
@@ -22,10 +21,12 @@ public class SamplePageViewModel : ObservableObject
2221
/// <summary>
2322
/// The <see cref="IFilesService"/> instance currently in use.
2423
/// </summary>
25-
private readonly IFilesService FilesServices = Ioc.Default.GetRequiredService<IFilesService>();
24+
private readonly IFilesService FilesServices;
2625

27-
public SamplePageViewModel()
26+
public SamplePageViewModel(IFilesService filesService)
2827
{
28+
FilesServices = filesService;
29+
2930
LoadDocsCommand = new AsyncRelayCommand<string>(LoadDocsAsync);
3031
}
3132

samples/MvvmSample.Core/ViewModels/Widgets/SubredditWidgetViewModel.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public sealed class SubredditWidgetViewModel : ObservableRecipient
2222
/// <summary>
2323
/// Gets the <see cref="IRedditService"/> instance to use.
2424
/// </summary>
25-
private readonly IRedditService RedditService = Ioc.Default.GetRequiredService<IRedditService>();
25+
private readonly IRedditService RedditService;
2626

2727
/// <summary>
2828
/// Gets the <see cref="ISettingsService"/> instance to use.
2929
/// </summary>
30-
private readonly ISettingsService SettingsService = Ioc.Default.GetRequiredService<ISettingsService>();
30+
private readonly ISettingsService SettingsService;
3131

3232
/// <summary>
3333
/// An <see cref="AsyncLock"/> instance to avoid concurrent requests.
@@ -37,8 +37,11 @@ public sealed class SubredditWidgetViewModel : ObservableRecipient
3737
/// <summary>
3838
/// Creates a new <see cref="SubredditWidgetViewModel"/> instance.
3939
/// </summary>
40-
public SubredditWidgetViewModel()
40+
public SubredditWidgetViewModel(IRedditService redditService, ISettingsService settingsService)
4141
{
42+
RedditService = redditService;
43+
SettingsService = settingsService;
44+
4245
LoadPostsCommand = new AsyncRelayCommand(LoadPostsAsync);
4346

4447
selectedSubreddit = SettingsService.GetValue<string>(nameof(SelectedSubreddit)) ?? Subreddits[0];

samples/MvvmSampleXF/MvvmSampleXF/App.xaml.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
using CommunityToolkit.Mvvm.DependencyInjection;
22
using Microsoft.Extensions.DependencyInjection;
33
using MvvmSample.Core.Services;
4+
using MvvmSample.Core.ViewModels;
5+
using MvvmSample.Core.ViewModels.Widgets;
46
using MvvmSampleXF.Services;
5-
using MvvmSampleXF.Views;
67
using Refit;
7-
using System;
8-
using System.IO;
9-
using System.Linq;
10-
using System.Reflection;
118
using Xamarin.Forms;
12-
using Xamarin.Forms.Xaml;
139

1410
namespace MvvmSampleXF
1511
{
@@ -27,9 +23,19 @@ public App()
2723
_initialized = true;
2824
Ioc.Default.ConfigureServices(
2925
new ServiceCollection()
26+
//Services
3027
.AddSingleton<IFilesService, FileService>()
3128
.AddSingleton<ISettingsService, SettingsService>()
32-
.AddSingleton(RestService.For<IRedditService>("https://www.reddit.com/"))
29+
.AddSingleton(RestService.For<IRedditService>("https://www.reddit.com/"))
30+
//ViewModels
31+
.AddTransient<PostWidgetViewModel>()
32+
.AddTransient<SubredditWidgetViewModel>()
33+
.AddTransient<AsyncRelayCommandPageViewModel>()
34+
.AddTransient<IocPageViewModel>()
35+
.AddTransient<MessengerPageViewModel>()
36+
.AddTransient<ObservableObjectPageViewModel>()
37+
.AddTransient<RelayCommandPageViewModel>()
38+
.AddTransient<SamplePageViewModel>()
3339
.BuildServiceProvider());
3440
}
3541

samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
x:Class="MvvmSampleXF.Views.AsyncRelayCommandPage"
99
x:DataType="vm:AsyncRelayCommandPageViewModel"
1010
Title="Commands">
11-
<ContentPage.BindingContext>
12-
<vm:AsyncRelayCommandPageViewModel x:Name="ViewModel" />
13-
</ContentPage.BindingContext>
1411
<ContentPage.Resources>
1512
<converters:TaskResultConverter x:Key="TaskResultConverter" />
1613
</ContentPage.Resources>

samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using System;
1+
using MvvmSample.Core.ViewModels;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
7+
using CommunityToolkit.Mvvm.DependencyInjection;
68

79
using Xamarin.Forms;
810
using Xamarin.Forms.Xaml;
@@ -15,8 +17,14 @@ public partial class AsyncRelayCommandPage : ContentPage
1517
public AsyncRelayCommandPage()
1618
{
1719
InitializeComponent();
20+
21+
ViewModel = Ioc.Default.GetRequiredService<AsyncRelayCommandPageViewModel>();
22+
23+
BindingContext = ViewModel;
1824
}
1925

26+
public AsyncRelayCommandPageViewModel ViewModel { get; }
27+
2028
protected override void OnAppearing()
2129
{
2230
base.OnAppearing();

0 commit comments

Comments
 (0)