Skip to content

Commit fe09efb

Browse files
committed
Constructor injection for ViewModels
1 parent 17f1251 commit fe09efb

38 files changed

+175
-82
lines changed

samples/MvvmSample.Core/ViewModels/AsyncRelayCommandPageViewModel.cs

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

55
using System.Threading.Tasks;
66
using CommunityToolkit.Mvvm.Input;
7-
7+
using MvvmSample.Core.Services;
8+
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using MvvmSample.Core.Services;
6+
57
namespace MvvmSample.Core.ViewModels
68
{
79
public class IocPageViewModel : SamplePageViewModel
8-
{
10+
{
11+
public IocPageViewModel(IFilesService filesService) : base(filesService)
12+
{
13+
}
914
}
1015
}

samples/MvvmSample.Core/ViewModels/MessengerPageViewModel.cs

Lines changed: 3 additions & 2 deletions
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-
10+
using MvvmSample.Core.Services;
11+
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: 3 additions & 2 deletions
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-
8+
using MvvmSample.Core.Services;
9+
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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
using System.Windows.Input;
66
using CommunityToolkit.Mvvm.Input;
7-
7+
using MvvmSample.Core.Services;
8+
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
@@ -6,7 +6,6 @@
66
using System.IO;
77
using System.Threading.Tasks;
88
using CommunityToolkit.Mvvm.ComponentModel;
9-
using CommunityToolkit.Mvvm.DependencyInjection;
109
using CommunityToolkit.Mvvm.Input;
1110
using MvvmSample.Core.Helpers;
1211
using MvvmSample.Core.Services;
@@ -21,10 +20,12 @@ public class SamplePageViewModel : ObservableObject
2120
/// <summary>
2221
/// The <see cref="IFilesService"/> instance currently in use.
2322
/// </summary>
24-
private readonly IFilesService FilesServices = Ioc.Default.GetRequiredService<IFilesService>();
23+
private readonly IFilesService FilesServices;
2524

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

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

Lines changed: 7 additions & 4 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,11 +37,14 @@ 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

44-
selectedSubreddit = SettingsService.GetValue<string>(nameof(SelectedSubreddit)) ?? Subreddits[0];
47+
selectedSubreddit = SettingsService.GetValue<string>(nameof(SelectedSubreddit)) ?? Subreddits[0];
4548
}
4649

4750
/// <summary>

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)