Skip to content

Commit 2251e13

Browse files
authored
Merge pull request #70 from denis-tsv/feature/constructor-injection-viewmodels
Constructor injection for ViewModels
2 parents e5b63ab + 4a7f065 commit 2251e13

File tree

70 files changed

+236
-191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+236
-191
lines changed

samples/MvvmSample.Core/ViewModels/AsyncRelayCommandPageViewModel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
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)
14+
: base(filesService)
1315
{
1416
DownloadTextCommand = new AsyncRelayCommand(DownloadTextAsync);
1517
}

samples/MvvmSample.Core/ViewModels/IocPageViewModel.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +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
810
{
11+
public IocPageViewModel(IFilesService filesService)
12+
: base(filesService)
13+
{
14+
}
915
}

samples/MvvmSample.Core/ViewModels/MessengerPageViewModel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
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)
17+
: base(filesService)
1618
{
1719
RequestCurrentUsernameCommand = new RelayCommand(RequestCurrentUsername);
1820
ResetCurrentUsernameCommand = new RelayCommand(ResetCurrentUsername);

samples/MvvmSample.Core/ViewModels/ObservableObjectPageViewModel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
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)
15+
: base(filesService)
1416
{
1517
ReloadTaskCommand = new RelayCommand(ReloadTask);
1618
}

samples/MvvmSample.Core/ViewModels/RelayCommandPageViewModel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
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)
14+
: base(filesService)
1315
{
1416
IncrementCounterCommand = new RelayCommand(IncrementCounter);
1517
}

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/MvvmSampleUwp/App.xaml.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Refit;
1212
using MvvmSampleUwp.Helpers;
1313
using MvvmSample.Core.Services;
14+
using MvvmSample.Core.ViewModels.Widgets;
15+
using MvvmSample.Core.ViewModels;
1416

1517
namespace MvvmSampleUwp;
1618

@@ -41,10 +43,20 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
4143

4244
// Register services
4345
Ioc.Default.ConfigureServices(
44-
new ServiceCollection()
46+
new ServiceCollection()
47+
//Services
4548
.AddSingleton<IFilesService, FilesService>()
4649
.AddSingleton<ISettingsService, SettingsService>()
47-
.AddSingleton(RestService.For<IRedditService>("https://www.reddit.com/"))
50+
.AddSingleton(RestService.For<IRedditService>("https://www.reddit.com/"))
51+
//ViewModels
52+
.AddTransient<PostWidgetViewModel>()
53+
.AddTransient<SubredditWidgetViewModel>()
54+
.AddTransient<AsyncRelayCommandPageViewModel>()
55+
.AddTransient<IocPageViewModel>()
56+
.AddTransient<MessengerPageViewModel>()
57+
.AddTransient<ObservableObjectPageViewModel>()
58+
.AddTransient<RelayCommandPageViewModel>()
59+
.AddTransient<SamplePageViewModel>()
4860
.BuildServiceProvider());
4961
}
5062

samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
xmlns:viewModels="using:MvvmSample.Core.ViewModels"
1313
NavigationCacheMode="Enabled"
1414
mc:Ignorable="d">
15-
<Page.DataContext>
16-
<viewModels:AsyncRelayCommandPageViewModel x:Name="ViewModel" />
17-
</Page.DataContext>
1815
<interactivity:Interaction.Behaviors>
1916
<core:EventTriggerBehavior EventName="Loaded">
2017
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="AsyncRelayCommand" />

samples/MvvmSampleUwp/Views/AsyncRelayCommandPage.xaml.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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 CommunityToolkit.Mvvm.DependencyInjection;
6+
using MvvmSample.Core.ViewModels;
57
using Windows.UI.Xaml.Controls;
68

79
namespace MvvmSampleUwp.Views;
@@ -14,5 +16,9 @@ public sealed partial class AsyncRelayCommandPage : Page
1416
public AsyncRelayCommandPage()
1517
{
1618
this.InitializeComponent();
19+
20+
DataContext = Ioc.Default.GetRequiredService<AsyncRelayCommandPageViewModel>();
1721
}
22+
23+
public AsyncRelayCommandPageViewModel ViewModel => (AsyncRelayCommandPageViewModel)DataContext;
1824
}

0 commit comments

Comments
 (0)