Skip to content

Commit deb2b1f

Browse files
committed
Revert "Constructor injection for ViewModels"
This reverts commit fe09efb.
1 parent efa4163 commit deb2b1f

38 files changed

+82
-175
lines changed

samples/MvvmSample.Core/ViewModels/AsyncRelayCommandPageViewModel.cs

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

55
using System.Threading.Tasks;
66
using CommunityToolkit.Mvvm.Input;
7-
using MvvmSample.Core.Services;
8-
7+
98
namespace MvvmSample.Core.ViewModels
109
{
1110
public class AsyncRelayCommandPageViewModel : SamplePageViewModel
1211
{
13-
public AsyncRelayCommandPageViewModel(IFilesService filesService) : base(filesService)
12+
public AsyncRelayCommandPageViewModel()
1413
{
1514
DownloadTextCommand = new AsyncRelayCommand(DownloadTextAsync);
1615
}

samples/MvvmSample.Core/ViewModels/IocPageViewModel.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
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-
75
namespace MvvmSample.Core.ViewModels
86
{
97
public class IocPageViewModel : SamplePageViewModel
10-
{
11-
public IocPageViewModel(IFilesService filesService) : base(filesService)
12-
{
13-
}
8+
{
149
}
1510
}

samples/MvvmSample.Core/ViewModels/MessengerPageViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
using CommunityToolkit.Mvvm.Input;
88
using CommunityToolkit.Mvvm.Messaging;
99
using CommunityToolkit.Mvvm.Messaging.Messages;
10-
using MvvmSample.Core.Services;
11-
10+
1211
namespace MvvmSample.Core.ViewModels
1312
{
1413
public class MessengerPageViewModel : SamplePageViewModel
1514
{
16-
public MessengerPageViewModel(IFilesService filesService) : base(filesService)
15+
public MessengerPageViewModel()
1716
{
1817
RequestCurrentUsernameCommand = new RelayCommand(RequestCurrentUsername);
1918
ResetCurrentUsernameCommand = new RelayCommand(ResetCurrentUsername);

samples/MvvmSample.Core/ViewModels/ObservableObjectPageViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
using System.Threading.Tasks;
66
using System.Windows.Input;
77
using CommunityToolkit.Mvvm.Input;
8-
using MvvmSample.Core.Services;
9-
8+
109
namespace MvvmSample.Core.ViewModels
1110
{
1211
public class ObservableObjectPageViewModel : SamplePageViewModel
1312
{
14-
public ObservableObjectPageViewModel(IFilesService filesService) : base(filesService)
13+
public ObservableObjectPageViewModel()
1514
{
1615
ReloadTaskCommand = new RelayCommand(ReloadTask);
1716
}

samples/MvvmSample.Core/ViewModels/RelayCommandPageViewModel.cs

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

55
using System.Windows.Input;
66
using CommunityToolkit.Mvvm.Input;
7-
using MvvmSample.Core.Services;
8-
7+
98
namespace MvvmSample.Core.ViewModels
109
{
1110
public class RelayCommandPageViewModel : SamplePageViewModel
1211
{
13-
public RelayCommandPageViewModel(IFilesService filesService) : base(filesService)
12+
public RelayCommandPageViewModel()
1413
{
1514
IncrementCounterCommand = new RelayCommand(IncrementCounter);
1615
}

samples/MvvmSample.Core/ViewModels/SamplePageViewModel.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.Threading.Tasks;
88
using CommunityToolkit.Mvvm.ComponentModel;
9+
using CommunityToolkit.Mvvm.DependencyInjection;
910
using CommunityToolkit.Mvvm.Input;
1011
using MvvmSample.Core.Helpers;
1112
using MvvmSample.Core.Services;
@@ -20,12 +21,10 @@ public class SamplePageViewModel : ObservableObject
2021
/// <summary>
2122
/// The <see cref="IFilesService"/> instance currently in use.
2223
/// </summary>
23-
private readonly IFilesService FilesServices;
24+
private readonly IFilesService FilesServices = Ioc.Default.GetRequiredService<IFilesService>();
2425

25-
public SamplePageViewModel(IFilesService filesService)
26+
public SamplePageViewModel()
2627
{
27-
FilesServices = filesService;
28-
2928
LoadDocsCommand = new AsyncRelayCommand<string>(LoadDocsAsync);
3029
}
3130

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

Lines changed: 4 additions & 7 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;
25+
private readonly IRedditService RedditService = Ioc.Default.GetRequiredService<IRedditService>();
2626

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

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

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

5047
/// <summary>

samples/MvvmSampleXF/MvvmSampleXF/App.xaml.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
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;
64
using MvvmSampleXF.Services;
5+
using MvvmSampleXF.Views;
76
using Refit;
7+
using System;
8+
using System.IO;
9+
using System.Linq;
10+
using System.Reflection;
811
using Xamarin.Forms;
12+
using Xamarin.Forms.Xaml;
913

1014
namespace MvvmSampleXF
1115
{
@@ -23,19 +27,9 @@ public App()
2327
_initialized = true;
2428
Ioc.Default.ConfigureServices(
2529
new ServiceCollection()
26-
//Services
2730
.AddSingleton<IFilesService, FileService>()
2831
.AddSingleton<ISettingsService, SettingsService>()
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>()
32+
.AddSingleton(RestService.For<IRedditService>("https://www.reddit.com/"))
3933
.BuildServiceProvider());
4034
}
4135

samples/MvvmSampleXF/MvvmSampleXF/Views/AsyncRelayCommandPage.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
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>
1114
<ContentPage.Resources>
1215
<converters:TaskResultConverter x:Key="TaskResultConverter" />
1316
</ContentPage.Resources>

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using MvvmSample.Core.ViewModels;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Linq;
54
using System.Text;
65
using System.Threading.Tasks;
7-
using CommunityToolkit.Mvvm.DependencyInjection;
86

97
using Xamarin.Forms;
108
using Xamarin.Forms.Xaml;
@@ -17,14 +15,8 @@ public partial class AsyncRelayCommandPage : ContentPage
1715
public AsyncRelayCommandPage()
1816
{
1917
InitializeComponent();
20-
21-
ViewModel = Ioc.Default.GetRequiredService<AsyncRelayCommandPageViewModel>();
22-
23-
BindingContext = ViewModel;
2418
}
2519

26-
public AsyncRelayCommandPageViewModel ViewModel { get; }
27-
2820
protected override void OnAppearing()
2921
{
3022
base.OnAppearing();

0 commit comments

Comments
 (0)