From 065606240798d0118385ca62ad451af6ea57638a Mon Sep 17 00:00:00 2001 From: Bruce Wayne Date: Wed, 14 Jul 2021 10:46:00 +0800 Subject: [PATCH] Use lazy provider --- .../MainWindowViewModel.cs | 40 +++++++++---------- NatTypeTester.ViewModels/RFC3489ViewModel.cs | 17 ++++---- NatTypeTester.ViewModels/RFC5780ViewModel.cs | 17 ++++---- NatTypeTester.ViewModels/SettingViewModel.cs | 10 +---- NatTypeTester.ViewModels/ViewModelBase.cs | 1 + NatTypeTester/App.xaml.cs | 7 ++-- NatTypeTester/MainWindow.xaml.cs | 19 ++++----- NatTypeTester/NatTypeTesterModule.cs | 6 +++ NatTypeTester/Views/SettingView.xaml.cs | 3 +- 9 files changed, 57 insertions(+), 63 deletions(-) diff --git a/NatTypeTester.ViewModels/MainWindowViewModel.cs b/NatTypeTester.ViewModels/MainWindowViewModel.cs index 3a3c2b344..4ec4e2bd1 100644 --- a/NatTypeTester.ViewModels/MainWindowViewModel.cs +++ b/NatTypeTester.ViewModels/MainWindowViewModel.cs @@ -8,6 +8,7 @@ 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(); - public Config Config { get; } + public Config Config => LazyServiceProvider.LazyGetRequiredService(); private readonly IEnumerable _defaultServers = new HashSet { @@ -34,11 +35,8 @@ public class MainWindowViewModel : ViewModelBase, IScreen private SourceList List { get; } = new(); public readonly IObservableCollection StunServers = new ObservableCollectionExtended(); - public MainWindowViewModel(Config config) + public MainWindowViewModel() { - Config = config; - - LoadStunServer(); List.Connect() .DistinctValues(x => x) .ObserveOn(RxApp.MainThreadScheduler) @@ -46,31 +44,33 @@ public MainWindowViewModel(Config config) .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()); + } + } + }); } } } diff --git a/NatTypeTester.ViewModels/RFC3489ViewModel.cs b/NatTypeTester.ViewModels/RFC3489ViewModel.cs index 1d12f3b79..8182e4195 100644 --- a/NatTypeTester.ViewModels/RFC3489ViewModel.cs +++ b/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(); - private readonly Config _config; + private Config Config => LazyServiceProvider.LazyGetRequiredService(); [Reactive] public ClassicStunResult Result3489 { get; set; } public ReactiveCommand 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 @@ public RFC3489ViewModel(IScreen hostScreen, Config config) 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); diff --git a/NatTypeTester.ViewModels/RFC5780ViewModel.cs b/NatTypeTester.ViewModels/RFC5780ViewModel.cs index 25fb56c03..f9d8df585 100644 --- a/NatTypeTester.ViewModels/RFC5780ViewModel.cs +++ b/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(); - private readonly Config _config; + private Config Config => LazyServiceProvider.LazyGetRequiredService(); [Reactive] public StunResult5389 Result5389 { get; set; } public ReactiveCommand 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 @@ public RFC5780ViewModel(IScreen hostScreen, Config config) 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); diff --git a/NatTypeTester.ViewModels/SettingViewModel.cs b/NatTypeTester.ViewModels/SettingViewModel.cs index 7660b96fb..5bd87f512 100644 --- a/NatTypeTester.ViewModels/SettingViewModel.cs +++ b/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(); - public Config Config { get; } - - public SettingViewModel(IScreen hostScreen, Config config) - { - HostScreen = hostScreen; - Config = config; - } + public Config Config => LazyServiceProvider.LazyGetRequiredService(); } } diff --git a/NatTypeTester.ViewModels/ViewModelBase.cs b/NatTypeTester.ViewModels/ViewModelBase.cs index a324738bf..73ea8553b 100644 --- a/NatTypeTester.ViewModels/ViewModelBase.cs +++ b/NatTypeTester.ViewModels/ViewModelBase.cs @@ -5,5 +5,6 @@ namespace NatTypeTester.ViewModels { public abstract class ViewModelBase : ReactiveObject, ISingletonDependency { + public IAbpLazyServiceProvider LazyServiceProvider { get; set; } = null!; } } diff --git a/NatTypeTester/App.xaml.cs b/NatTypeTester/App.xaml.cs index ed94ba5c4..9f8d46bd2 100644 --- a/NatTypeTester/App.xaml.cs +++ b/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 @@ protected override async void OnExit(ExitEventArgs e) private void Initialize(IServiceProvider serviceProvider) { _application.Initialize(serviceProvider); + serviceProvider.UseMicrosoftDependencyResolver(); } private static IHost CreateHostBuilder() { return Host.CreateDefaultBuilder() .UseAutofac() - .ConfigureServices((hostContext, services) => - { - services.AddApplication(); - }) + .ConfigureServices((_, services) => services.AddApplication()) .Build(); } } diff --git a/NatTypeTester/MainWindow.xaml.cs b/NatTypeTester/MainWindow.xaml.cs index e219f0384..ec1c425eb 100644 --- a/NatTypeTester/MainWindow.xaml.cs +++ b/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 @@ SettingViewModel settingViewModel { if (args.EventArgs.IsSettingsSelected) { - ViewModel.Router.Navigate.Execute(settingViewModel); + ViewModel.Router.Navigate.Execute(ServiceProvider.GetRequiredService()); return; } @@ -57,17 +56,19 @@ SettingViewModel settingViewModel { case @"1": { - ViewModel.Router.Navigate.Execute(rfc5780ViewModel); + ViewModel.Router.Navigate.Execute(ServiceProvider.GetRequiredService()); break; } case @"2": { - ViewModel.Router.Navigate.Execute(rfc3489ViewModel); + ViewModel.Router.Navigate.Execute(ServiceProvider.GetRequiredService()); break; } } }).DisposeWith(d); NavigationView.SelectedItem = NavigationView.MenuItems.OfType().First(); + + ViewModel.LoadStunServer(); }); } } diff --git a/NatTypeTester/NatTypeTesterModule.cs b/NatTypeTester/NatTypeTesterModule.cs index bc5eb3487..c43e92dee 100644 --- a/NatTypeTester/NatTypeTesterModule.cs +++ b/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 @@ public override void PreConfigureServices(ServiceConfigurationContext context) Locator.CurrentMutable.InitializeSplat(); Locator.CurrentMutable.InitializeReactiveUI(RegistrationNamespace.Wpf); } + + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.TryAddTransient(); + } } } diff --git a/NatTypeTester/Views/SettingView.xaml.cs b/NatTypeTester/Views/SettingView.xaml.cs index 25ed9d9a9..7a7d96c9f 100644 --- a/NatTypeTester/Views/SettingView.xaml.cs +++ b/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 => {