Quellcode durchsuchen

Use lazy provider

Bruce Wayne vor 4 Jahren
Ursprung
Commit
0656062407

+ 20 - 20
NatTypeTester.ViewModels/MainWindowViewModel.cs

@@ -8,6 +8,7 @@ using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Reactive.Linq;
+using System.Threading.Tasks;
 using Volo.Abp.DependencyInjection;
 
 namespace NatTypeTester.ViewModels
@@ -18,9 +19,9 @@ namespace NatTypeTester.ViewModels
 	)]
 	public class MainWindowViewModel : ViewModelBase, IScreen
 	{
-		public RoutingState Router { get; } = new();
+		public RoutingState Router => LazyServiceProvider.LazyGetRequiredService<RoutingState>();
 
-		public Config Config { get; }
+		public Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();
 
 		private readonly IEnumerable<string> _defaultServers = new HashSet<string>
 		{
@@ -34,11 +35,8 @@ namespace NatTypeTester.ViewModels
 		private SourceList<string> List { get; } = new();
 		public readonly IObservableCollection<string> StunServers = new ObservableCollectionExtended<string>();
 
-		public MainWindowViewModel(Config config)
+		public MainWindowViewModel()
 		{
-			Config = config;
-
-			LoadStunServer();
 			List.Connect()
 				.DistinctValues(x => x)
 				.ObserveOn(RxApp.MainThreadScheduler)
@@ -46,31 +44,33 @@ namespace NatTypeTester.ViewModels
 				.Subscribe();
 		}
 
-		private async void LoadStunServer()
+		public void LoadStunServer()
 		{
 			foreach (var server in _defaultServers)
 			{
 				List.Add(server);
 			}
-			Config.StunServer = _defaultServers.First();
 
-			const string path = @"stun.txt";
+			Config.StunServer = _defaultServers.First();
 
-			if (!File.Exists(path))
+			Task.Run(() =>
 			{
-				return;
-			}
+				const string path = @"stun.txt";
 
-			using var sw = new StreamReader(path);
-			string line;
-			var stun = new StunServer();
-			while ((line = await sw.ReadLineAsync()) != null)
-			{
-				if (!string.IsNullOrWhiteSpace(line) && stun.Parse(line))
+				if (!File.Exists(path))
 				{
-					List.Add(stun.ToString());
+					return;
 				}
-			}
+
+				var stun = new StunServer();
+				foreach (var line in File.ReadLines(path))
+				{
+					if (!string.IsNullOrWhiteSpace(line) && stun.Parse(line))
+					{
+						List.Add(stun.ToString());
+					}
+				}
+			});
 		}
 	}
 }

+ 7 - 10
NatTypeTester.ViewModels/RFC3489ViewModel.cs

@@ -18,20 +18,17 @@ namespace NatTypeTester.ViewModels
 	public class RFC3489ViewModel : ViewModelBase, IRoutableViewModel
 	{
 		public string UrlPathSegment => @"RFC3489";
-		public IScreen HostScreen { get; }
+		public IScreen HostScreen => LazyServiceProvider.LazyGetRequiredService<IScreen>();
 
-		private readonly Config _config;
+		private Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();
 
 		[Reactive]
 		public ClassicStunResult Result3489 { get; set; }
 
 		public ReactiveCommand<Unit, Unit> TestClassicNatType { get; }
 
-		public RFC3489ViewModel(IScreen hostScreen, Config config)
+		public RFC3489ViewModel()
 		{
-			HostScreen = hostScreen;
-			_config = config;
-
 			Result3489 = new ClassicStunResult { LocalEndPoint = new IPEndPoint(IPAddress.Any, 0) };
 			TestClassicNatType = ReactiveCommand.CreateFromTask(TestClassicNatTypeImpl);
 		}
@@ -39,16 +36,16 @@ namespace NatTypeTester.ViewModels
 		private async Task TestClassicNatTypeImpl(CancellationToken token)
 		{
 			var server = new StunServer();
-			if (!server.Parse(_config.StunServer))
+			if (!server.Parse(Config.StunServer))
 			{
 				throw new Exception(@"Wrong STUN Server!");
 			}
 
 			using var proxy = ProxyFactory.CreateProxy(
-					_config.ProxyType,
+					Config.ProxyType,
 					Result3489.LocalEndPoint,
-					NetUtils.ParseEndpoint(_config.ProxyServer),
-					_config.ProxyUser, _config.ProxyPassword
+					NetUtils.ParseEndpoint(Config.ProxyServer),
+					Config.ProxyUser, Config.ProxyPassword
 			);
 
 			using var client = new StunClient3489(server.Hostname, server.Port, Result3489.LocalEndPoint, proxy);

+ 7 - 10
NatTypeTester.ViewModels/RFC5780ViewModel.cs

@@ -18,20 +18,17 @@ namespace NatTypeTester.ViewModels
 	public class RFC5780ViewModel : ViewModelBase, IRoutableViewModel
 	{
 		public string UrlPathSegment => @"RFC5780";
-		public IScreen HostScreen { get; }
+		public IScreen HostScreen => LazyServiceProvider.LazyGetRequiredService<IScreen>();
 
-		private readonly Config _config;
+		private Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();
 
 		[Reactive]
 		public StunResult5389 Result5389 { get; set; }
 
 		public ReactiveCommand<Unit, Unit> DiscoveryNatType { get; }
 
-		public RFC5780ViewModel(IScreen hostScreen, Config config)
+		public RFC5780ViewModel()
 		{
-			HostScreen = hostScreen;
-			_config = config;
-
 			Result5389 = new StunResult5389 { LocalEndPoint = new IPEndPoint(IPAddress.Any, 0) };
 			DiscoveryNatType = ReactiveCommand.CreateFromTask(DiscoveryNatTypeImpl);
 		}
@@ -39,16 +36,16 @@ namespace NatTypeTester.ViewModels
 		private async Task DiscoveryNatTypeImpl(CancellationToken token)
 		{
 			var server = new StunServer();
-			if (!server.Parse(_config.StunServer))
+			if (!server.Parse(Config.StunServer))
 			{
 				throw new Exception(@"Wrong STUN Server!");
 			}
 
 			using var proxy = ProxyFactory.CreateProxy(
-					_config.ProxyType,
+					Config.ProxyType,
 					Result5389.LocalEndPoint,
-					NetUtils.ParseEndpoint(_config.ProxyServer),
-					_config.ProxyUser, _config.ProxyPassword
+					NetUtils.ParseEndpoint(Config.ProxyServer),
+					Config.ProxyUser, Config.ProxyPassword
 			);
 
 			using var client = new StunClient5389UDP(server.Hostname, server.Port, Result5389.LocalEndPoint, proxy);

+ 2 - 8
NatTypeTester.ViewModels/SettingViewModel.cs

@@ -8,14 +8,8 @@ namespace NatTypeTester.ViewModels
 	public class SettingViewModel : ViewModelBase, IRoutableViewModel
 	{
 		public string UrlPathSegment => @"Settings";
-		public IScreen HostScreen { get; }
+		public IScreen HostScreen => LazyServiceProvider.LazyGetRequiredService<IScreen>();
 
-		public Config Config { get; }
-
-		public SettingViewModel(IScreen hostScreen, Config config)
-		{
-			HostScreen = hostScreen;
-			Config = config;
-		}
+		public Config Config => LazyServiceProvider.LazyGetRequiredService<Config>();
 	}
 }

+ 1 - 0
NatTypeTester.ViewModels/ViewModelBase.cs

@@ -5,5 +5,6 @@ namespace NatTypeTester.ViewModels
 {
 	public abstract class ViewModelBase : ReactiveObject, ISingletonDependency
 	{
+		public IAbpLazyServiceProvider LazyServiceProvider { get; set; } = null!;
 	}
 }

+ 3 - 4
NatTypeTester/App.xaml.cs

@@ -1,5 +1,6 @@
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Hosting;
+using Splat.Microsoft.Extensions.DependencyInjection;
 using System;
 using System.Windows;
 using Volo.Abp;
@@ -41,16 +42,14 @@ namespace NatTypeTester
 		private void Initialize(IServiceProvider serviceProvider)
 		{
 			_application.Initialize(serviceProvider);
+			serviceProvider.UseMicrosoftDependencyResolver();
 		}
 
 		private static IHost CreateHostBuilder()
 		{
 			return Host.CreateDefaultBuilder()
 					.UseAutofac()
-					.ConfigureServices((hostContext, services) =>
-					{
-						services.AddApplication<NatTypeTesterModule>();
-					})
+					.ConfigureServices((_, services) => services.AddApplication<NatTypeTesterModule>())
 					.Build();
 		}
 	}

+ 10 - 9
NatTypeTester/MainWindow.xaml.cs

@@ -1,3 +1,4 @@
+using Microsoft.Extensions.DependencyInjection;
 using ModernWpf;
 using ModernWpf.Controls;
 using NatTypeTester.ViewModels;
@@ -12,15 +13,13 @@ namespace NatTypeTester
 {
 	public partial class MainWindow : ISingletonDependency
 	{
-		public MainWindow(MainWindowViewModel viewModel,
-			RFC5780ViewModel rfc5780ViewModel,
-			RFC3489ViewModel rfc3489ViewModel,
-			SettingViewModel settingViewModel
-			)
+		public IServiceProvider ServiceProvider { get; set; } = null!;
+
+		public MainWindow(MainWindowViewModel viewModel)
 		{
 			InitializeComponent();
 			ViewModel = viewModel;
-			ThemeManager.Current.ApplicationTheme = null;
+			ThemeManager.Current.ApplicationTheme = default;
 
 			this.WhenActivated(d =>
 			{
@@ -44,7 +43,7 @@ namespace NatTypeTester
 				{
 					if (args.EventArgs.IsSettingsSelected)
 					{
-						ViewModel.Router.Navigate.Execute(settingViewModel);
+						ViewModel.Router.Navigate.Execute(ServiceProvider.GetRequiredService<SettingViewModel>());
 						return;
 					}
 
@@ -57,17 +56,19 @@ namespace NatTypeTester
 					{
 						case @"1":
 						{
-							ViewModel.Router.Navigate.Execute(rfc5780ViewModel);
+							ViewModel.Router.Navigate.Execute(ServiceProvider.GetRequiredService<RFC5780ViewModel>());
 							break;
 						}
 						case @"2":
 						{
-							ViewModel.Router.Navigate.Execute(rfc3489ViewModel);
+							ViewModel.Router.Navigate.Execute(ServiceProvider.GetRequiredService<RFC3489ViewModel>());
 							break;
 						}
 					}
 				}).DisposeWith(d);
 				NavigationView.SelectedItem = NavigationView.MenuItems.OfType<NavigationViewItem>().First();
+
+				ViewModel.LoadStunServer();
 			});
 		}
 	}

+ 6 - 0
NatTypeTester/NatTypeTesterModule.cs

@@ -1,4 +1,5 @@
 using JetBrains.Annotations;
+using Microsoft.Extensions.DependencyInjection.Extensions;
 using NatTypeTester.Models;
 using NatTypeTester.ViewModels;
 using ReactiveUI;
@@ -23,5 +24,10 @@ namespace NatTypeTester
 			Locator.CurrentMutable.InitializeSplat();
 			Locator.CurrentMutable.InitializeReactiveUI(RegistrationNamespace.Wpf);
 		}
+
+		public override void ConfigureServices(ServiceConfigurationContext context)
+		{
+			context.Services.TryAddTransient<RoutingState>();
+		}
 	}
 }

+ 1 - 2
NatTypeTester/Views/SettingView.xaml.cs

@@ -11,10 +11,9 @@ namespace NatTypeTester.Views
 	[UsedImplicitly]
 	public partial class SettingView : ITransientDependency
 	{
-		public SettingView(SettingViewModel viewModel)
+		public SettingView()
 		{
 			InitializeComponent();
-			ViewModel = viewModel;
 
 			this.WhenActivated(d =>
 			{