Skip to content

Commit 5167c9f

Browse files
committed
Switch to Labs generator for Dependency Injection
1 parent 8eb59ef commit 5167c9f

File tree

5 files changed

+131
-93
lines changed

5 files changed

+131
-93
lines changed

nuget.config

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<clear />
5+
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
6+
<add key="toolkit-labs" value="https://pkgs.dev.azure.com/dotnet/CommunityToolkit/_packaging/CommunityToolkit-Labs/nuget/v3/index.json" />
7+
</packageSources>
8+
</configuration>

src/Brainf_ckSharp.Uwp/App.Main.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using System.Linq;
44
using Windows.ApplicationModel;
@@ -24,7 +24,7 @@ public sealed partial class App : Application, IFilesManagerService
2424
public App(string id)
2525
{
2626
Id = id;
27-
Services = ConfigureServices();
27+
Services = BuildServiceProvider();
2828

2929
this.InitializeComponent();
3030
}
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using Brainf_ckSharp.Enums;
3+
using Brainf_ckSharp.Services;
4+
using Brainf_ckSharp.Services.Uwp.Analytics;
5+
using Brainf_ckSharp.Services.Uwp.Email;
6+
using Brainf_ckSharp.Services.Uwp.Store;
7+
using Brainf_ckSharp.Services.Uwp.SystemInformation;
8+
using Brainf_ckSharp.Shared.Constants;
9+
using Brainf_ckSharp.Shared.Enums.Settings;
10+
using Brainf_ckSharp.Shared.ViewModels;
11+
using Brainf_ckSharp.Shared.ViewModels.Controls;
12+
using Brainf_ckSharp.Shared.ViewModels.Controls.SubPages;
13+
using Brainf_ckSharp.Shared.ViewModels.Controls.SubPages.Settings;
14+
using Brainf_ckSharp.Shared.ViewModels.Views;
15+
using Brainf_ckSharp.Uwp.Services.Clipboard;
16+
using Brainf_ckSharp.Uwp.Services.Files;
17+
using Brainf_ckSharp.Uwp.Services.Keyboard;
18+
using Brainf_ckSharp.Uwp.Services.Settings;
19+
using Brainf_ckSharp.Uwp.Services.Share;
20+
using CommunityToolkit.Extensions.DependencyInjection;
21+
using CommunityToolkit.Mvvm.Messaging;
22+
using GitHub;
23+
using Microsoft.Extensions.DependencyInjection;
24+
25+
#nullable enable
26+
27+
namespace Brainf_ckSharp.Uwp;
28+
29+
/// <inheritdoc/>
30+
partial class App
31+
{
32+
/// <summary>
33+
/// Builds the <see cref="IServiceProvider"/> instance to use in the application.
34+
/// </summary>
35+
/// <returns>The resulting <see cref="IServiceProvider"/> instance.</returns>
36+
/// <remarks>This method should only be called once during startup.</remarks>
37+
private IServiceProvider BuildServiceProvider()
38+
{
39+
ServiceCollection services = new();
40+
41+
// Manually register services that need manual control. That is:
42+
// - AppConfiguration, as it requires known product ids
43+
// - GitHubService, as it requires the user agent to be set
44+
// - IFilesManagerService, as the app instance implements it
45+
_ = services.AddSingleton(new AppConfiguration() { UnlockThemesIapId = "9P4Q63CCFPBM" });
46+
_ = services.AddSingleton<IGitHubService>(_ => new GitHubService("Brainf_ckSharp|Uwp"));
47+
_ = services.AddSingleton<IFilesManagerService>(this);
48+
49+
// Register all other services (this will invoke the source generated stubs)
50+
ConfigureServices(services);
51+
52+
// Build the service provider to use in the application
53+
return services.BuildServiceProvider();
54+
}
55+
56+
/// <summary>
57+
/// Performs additional settings configuration and other startup initialization.
58+
/// </summary>
59+
/// <param name="serviceProvider">The input service provider to use.</param>
60+
private void InitializeServices(IServiceProvider serviceProvider)
61+
{
62+
ISettingsService settings = serviceProvider.GetRequiredService<ISettingsService>();
63+
64+
// Initialize default settings
65+
settings.SetValue(SettingsKeys.IsVirtualKeyboardEnabled, true, false);
66+
settings.SetValue(SettingsKeys.BracketsFormattingStyle, BracketsFormattingStyle.NewLine, false);
67+
settings.SetValue(SettingsKeys.IdeTheme, IdeTheme.VisualStudio, false);
68+
settings.SetValue(SettingsKeys.RenderWhitespaces, true, false);
69+
settings.SetValue(SettingsKeys.SelectedView, ViewType.Console, false);
70+
settings.SetValue(SettingsKeys.ClearStdinBufferOnRequest, false, false);
71+
settings.SetValue(SettingsKeys.ShowPBrainButtons, true, false);
72+
settings.SetValue(SettingsKeys.DataType, DataType.Byte, false);
73+
settings.SetValue(SettingsKeys.ExecutionOptions, ExecutionOptions.AllowOverflow, false);
74+
settings.SetValue(SettingsKeys.MemorySize, 128, false);
75+
}
76+
77+
/// <summary>
78+
/// Configures all services used by the app.
79+
/// </summary>
80+
/// <param name="services">The target <see cref="IServiceCollection"/> instance to register services with.</param>
81+
[Singleton(typeof(WeakReferenceMessenger), typeof(IMessenger))]
82+
[Singleton(typeof(FilesService), typeof(IFilesService))]
83+
[Singleton(typeof(SettingsService), typeof(ISettingsService))]
84+
[Singleton(typeof(KeyboardListenerService), typeof(IKeyboardListenerService))]
85+
[Singleton(typeof(ClipboardService), typeof(IClipboardService))]
86+
[Singleton(typeof(ShareService), typeof(IShareService))]
87+
[Singleton(typeof(EmailService), typeof(IEmailService))]
88+
[Singleton(typeof(SystemInformationService), typeof(ISystemInformationService))]
89+
#if DEBUG
90+
[Singleton(typeof(TestStoreService), typeof(IStoreService))]
91+
[Singleton(typeof(TestAnalyticsService), typeof(IAnalyticsService))]
92+
#else
93+
[Singleton(typeof(ProductionStoreService), typeof(IStoreService))]
94+
[Singleton(typeof(ReleaseAnalyticsService), typeof(IAnalyticsService))]
95+
#endif
96+
[Singleton(typeof(ShellViewModel))]
97+
[Singleton(typeof(CompactMemoryViewerViewModel))]
98+
[Singleton(typeof(ConsoleViewModel))]
99+
[Singleton(typeof(IdeViewModel))]
100+
[Singleton(typeof(VirtualKeyboardViewModel))]
101+
[Singleton(typeof(StdinHeaderViewModel))]
102+
[Singleton(typeof(StatusBarViewModel))]
103+
[Transient(typeof(SettingsSubPageViewModel))]
104+
[Transient(typeof(ReviewPromptSubPageViewModel))]
105+
[Transient(typeof(IdeResultSubPageViewModel))]
106+
[Transient(typeof(CodeLibrarySubPageViewModel))]
107+
[Transient(typeof(AboutSubPageViewModel))]
108+
private static partial void ConfigureServices(IServiceCollection services);
109+
}

src/Brainf_ckSharp.Uwp/App.xaml.cs

+3-90
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,27 @@
11
using System;
22
using System.Linq;
3-
using System.Reflection;
43
using Windows.ApplicationModel.Activation;
54
using Windows.ApplicationModel.Core;
65
using Windows.Foundation;
76
using Windows.Storage;
87
using Windows.UI.Core.Preview;
98
using Windows.UI.Xaml;
10-
using Brainf_ckSharp.Enums;
119
using Brainf_ckSharp.Services;
12-
using Brainf_ckSharp.Services.Uwp.Analytics;
13-
using Brainf_ckSharp.Services.Uwp.Email;
14-
using Brainf_ckSharp.Shared.Enums.Settings;
1510
using Brainf_ckSharp.Uwp.Controls.Host;
1611
using Brainf_ckSharp.Uwp.Helpers;
17-
using Brainf_ckSharp.Uwp.Services.Clipboard;
1812
using Brainf_ckSharp.Uwp.Services.Files;
19-
using Brainf_ckSharp.Uwp.Services.Keyboard;
20-
using Brainf_ckSharp.Uwp.Services.Settings;
21-
using Brainf_ckSharp.Uwp.Services.Share;
22-
using Brainf_ckSharp.Services.Uwp.Store;
23-
using Brainf_ckSharp.Services.Uwp.SystemInformation;
2413
using Brainf_ckSharp.Shared.Constants;
25-
using Brainf_ckSharp.Shared.ViewModels;
26-
using Brainf_ckSharp.Shared.ViewModels.Controls;
27-
using Brainf_ckSharp.Shared.ViewModels.Controls.SubPages;
28-
using Brainf_ckSharp.Shared.ViewModels.Controls.SubPages.Settings;
2914
using Brainf_ckSharp.Shared.ViewModels.Views;
3015
using Brainf_ckSharp.Uwp.Controls.SubPages.Host;
31-
using CommunityToolkit.Mvvm.Messaging;
32-
using GitHub;
3316
using Microsoft.Extensions.DependencyInjection;
34-
using Microsoft.Toolkit.Uwp.Helpers;
3517
using Microsoft.Toolkit.Uwp.UI;
3618

3719
#nullable enable
3820

3921
namespace Brainf_ckSharp.Uwp;
4022

41-
/// <summary>
42-
/// Provides application-specific behavior to supplement the default <see cref="Application"/> class
43-
/// </summary>
44-
public sealed partial class App : Application
23+
/// <inheritdoc/>
24+
partial class App
4525
{
4626
/// <summary>
4727
/// The currently requested file to open
@@ -158,7 +138,7 @@ private void OnActivated(bool prelaunchActivated)
158138
// Initialize the UI if needed
159139
if (Window.Current.Content is not Shell)
160140
{
161-
InitializeServices();
141+
InitializeServices(Services);
162142

163143
// Initial UI styling
164144
TitleBarHelper.ExpandViewIntoTitleBar();
@@ -207,71 +187,4 @@ private async void OnCloseRequested(object sender, SystemNavigationCloseRequeste
207187

208188
deferral.Complete();
209189
}
210-
211-
/// <summary>
212-
/// Configures the services to use in the app
213-
/// </summary>
214-
/// <returns>An <see cref="IServiceProvider"/> instance with the app services.</returns>
215-
private IServiceProvider ConfigureServices()
216-
{
217-
ServiceCollection services = new();
218-
219-
// Prepare the app configuration
220-
services.AddSingleton(new AppConfiguration() { UnlockThemesIapId = "9P4Q63CCFPBM" });
221-
222-
// Services
223-
services.AddSingleton<IMessenger, StrongReferenceMessenger>();
224-
services.AddSingleton<IFilesService, FilesService>();
225-
services.AddSingleton<IFilesManagerService>(this);
226-
services.AddSingleton<ISettingsService, SettingsService>();
227-
services.AddSingleton<IKeyboardListenerService, KeyboardListenerService>();
228-
services.AddSingleton<IClipboardService, ClipboardService>();
229-
services.AddSingleton<IShareService, ShareService>();
230-
services.AddSingleton<IEmailService, EmailService>();
231-
services.AddSingleton<ISystemInformationService, SystemInformationService>();
232-
services.AddSingleton<IGitHubService>(_ => new GitHubService("Brainf_ckSharp|Uwp"));
233-
#if DEBUG
234-
services.AddSingleton<IStoreService, TestStoreService>();
235-
services.AddSingleton<IAnalyticsService, TestAnalyticsService>();
236-
#else
237-
services.AddSingleton<IStoreService, ProductionStoreService>();
238-
services.AddSingleton<IAnalyticsService, ReleaseAnalyticsService>();
239-
#endif
240-
241-
// Viewmodels
242-
services.AddSingleton<ShellViewModel>();
243-
services.AddSingleton<CompactMemoryViewerViewModel>();
244-
services.AddSingleton<ConsoleViewModel>();
245-
services.AddSingleton<IdeViewModel>();
246-
services.AddSingleton<VirtualKeyboardViewModel>();
247-
services.AddSingleton<StdinHeaderViewModel>();
248-
services.AddSingleton<StatusBarViewModel>();
249-
services.AddTransient<SettingsSubPageViewModel>();
250-
services.AddTransient<ReviewPromptSubPageViewModel>();
251-
services.AddTransient<IdeResultSubPageViewModel>();
252-
services.AddTransient<CodeLibrarySubPageViewModel>();
253-
services.AddTransient<AboutSubPageViewModel>();
254-
255-
return services.BuildServiceProvider();
256-
}
257-
258-
/// <summary>
259-
/// Performs additional settings configuration and other startup initialization
260-
/// </summary>
261-
private void InitializeServices()
262-
{
263-
ISettingsService settings = Services.GetRequiredService<ISettingsService>();
264-
265-
// Initialize default settings
266-
settings.SetValue(SettingsKeys.IsVirtualKeyboardEnabled, true, false);
267-
settings.SetValue(SettingsKeys.BracketsFormattingStyle, BracketsFormattingStyle.NewLine, false);
268-
settings.SetValue(SettingsKeys.IdeTheme, IdeTheme.VisualStudio, false);
269-
settings.SetValue(SettingsKeys.RenderWhitespaces, true, false);
270-
settings.SetValue(SettingsKeys.SelectedView, ViewType.Console, false);
271-
settings.SetValue(SettingsKeys.ClearStdinBufferOnRequest, false, false);
272-
settings.SetValue(SettingsKeys.ShowPBrainButtons, true, false);
273-
settings.SetValue(SettingsKeys.DataType, DataType.Byte, false);
274-
settings.SetValue(SettingsKeys.ExecutionOptions, ExecutionOptions.AllowOverflow, false);
275-
settings.SetValue(SettingsKeys.MemorySize, 128, false);
276-
}
277190
}

src/Brainf_ckSharp.Uwp/Brainf_ckSharp.Uwp.csproj

+9-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@
119119
<Compile Include="App.xaml.cs">
120120
<DependentUpon>App.xaml</DependentUpon>
121121
</Compile>
122-
<Compile Include="App.Main.cs" />
122+
<Compile Include="App.Main.cs">
123+
<DependentUpon>App.xaml</DependentUpon>
124+
</Compile>
125+
<Compile Include="App.Services.cs">
126+
<DependentUpon>App.xaml</DependentUpon>
127+
</Compile>
123128
<Compile Include="AttachedProperties\MarkdownHelper.cs" />
124129
<Compile Include="AttachedProperties\Brainf_ckInlineFormatterHelper.StackTrace.cs" />
125130
<Compile Include="AttachedProperties\CharacterRotationHelper.cs" />
@@ -373,6 +378,9 @@
373378
</ApplicationDefinition>
374379
</ItemGroup>
375380
<ItemGroup>
381+
<PackageReference Include="CommunityToolkit.Labs.Extensions.DependencyInjection">
382+
<Version>0.1.240911-build.1751</Version>
383+
</PackageReference>
376384
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
377385
<Version>8.0.0</Version>
378386
</PackageReference>

0 commit comments

Comments
 (0)