From 544ec776673f1d196e0100e6a78c05b2729131f3 Mon Sep 17 00:00:00 2001 From: Macocian Alexandru Victor Date: Fri, 9 Sep 2022 20:13:59 +0200 Subject: [PATCH] Integrate with Microsoft.Extensions.DependencyInjection (#12) * Integrate with Microsoft.Extensions.DependencyInjection * Setup CODEOWNERS --- .github/CODEOWNERS | 1 + .../WpfExtended.SourceGeneration.Tests.csproj | 2 +- .../WpfExtended.SourceGeneration.csproj | 4 +- WpfExtended.Test/TestApplication/Launcher.cs | 16 +++++-- WpfExtended.Test/WpfExtended.Tests.csproj | 6 +-- WpfExtended/Launch/ExtendedApplication.cs | 48 +++++++++++-------- WpfExtended/WpfExtended.csproj | 7 +-- 7 files changed, 51 insertions(+), 33 deletions(-) create mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..707dc58 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* amacocian@yahoo.com \ No newline at end of file diff --git a/WpfExtended.SourceGeneration.Tests/WpfExtended.SourceGeneration.Tests.csproj b/WpfExtended.SourceGeneration.Tests/WpfExtended.SourceGeneration.Tests.csproj index d868149..6963ec8 100644 --- a/WpfExtended.SourceGeneration.Tests/WpfExtended.SourceGeneration.Tests.csproj +++ b/WpfExtended.SourceGeneration.Tests/WpfExtended.SourceGeneration.Tests.csproj @@ -10,7 +10,7 @@ - + diff --git a/WpfExtended.SourceGeneration/WpfExtended.SourceGeneration.csproj b/WpfExtended.SourceGeneration/WpfExtended.SourceGeneration.csproj index 2c23fd3..f3d8f9b 100644 --- a/WpfExtended.SourceGeneration/WpfExtended.SourceGeneration.csproj +++ b/WpfExtended.SourceGeneration/WpfExtended.SourceGeneration.csproj @@ -13,10 +13,10 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/WpfExtended.Test/TestApplication/Launcher.cs b/WpfExtended.Test/TestApplication/Launcher.cs index 170ce05..d9407ca 100644 --- a/WpfExtended.Test/TestApplication/Launcher.cs +++ b/WpfExtended.Test/TestApplication/Launcher.cs @@ -1,9 +1,12 @@ -using Microsoft.Extensions.Http.Logging; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Http.Logging; using Microsoft.Extensions.Logging; using Slim; +using Slim.Integration.ServiceCollection; using System; using System.Extensions; using System.Http; +using System.Logging; using System.Net.Http; using System.Windows.Extensions; using WpfExtended.Test; @@ -21,8 +24,10 @@ namespace WpfExtended.Tests Instance.Run(); } - protected override void SetupServiceManager(IServiceManager serviceManager) + protected override System.IServiceProvider SetupServiceProvider(IServiceCollection services) { + var serviceManager = new ServiceManager(); + serviceManager.RegisterResolver(new LoggerResolver()); serviceManager.RegisterDebugLoggerFactory(); serviceManager.RegisterResolver( new HttpClientResolver() @@ -34,6 +39,9 @@ namespace WpfExtended.Tests return handler; })); serviceManager.RegisterOptionsManager(); + + var provider = services.BuildSlimServiceProvider(serviceManager); + return provider; } protected override void ApplicationClosing() @@ -49,9 +57,9 @@ namespace WpfExtended.Tests return false; } - protected override void RegisterServices(IServiceProducer serviceProducer) + protected override void RegisterServices(IServiceCollection services) { - serviceProducer.RegisterSingleton(); + services.AddSingleton(); } } } diff --git a/WpfExtended.Test/WpfExtended.Tests.csproj b/WpfExtended.Test/WpfExtended.Tests.csproj index fb9cedb..3d7d197 100644 --- a/WpfExtended.Test/WpfExtended.Tests.csproj +++ b/WpfExtended.Test/WpfExtended.Tests.csproj @@ -11,9 +11,9 @@ - - - + + + diff --git a/WpfExtended/Launch/ExtendedApplication.cs b/WpfExtended/Launch/ExtendedApplication.cs index d4af327..47ebfcb 100644 --- a/WpfExtended/Launch/ExtendedApplication.cs +++ b/WpfExtended/Launch/ExtendedApplication.cs @@ -1,5 +1,7 @@ -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using Slim; +using Slim.Integration.ServiceCollection; using System.Extensions; using System.Logging; using System.Threading.Tasks; @@ -14,21 +16,28 @@ namespace System.Windows.Extensions public abstract class ExtendedApplication : Application where T : Window { - protected IServiceManager ServiceManager { get; } = new ServiceManager(); - protected ILoggerFactory LoggerFactory { get; private set; } + private readonly ServiceCollection services = new(); + + protected IServiceProvider ServiceProvider { get; private set; } public ExtendedApplication() { - this.RegisterInternals(); } /// - /// Do some work on the before calling . + /// Create a new . /// - protected virtual void SetupServiceManager(IServiceManager serviceManager) + protected virtual IServiceProvider SetupServiceProvider(IServiceCollection services) { - serviceManager.RegisterDebugLoggerFactory(); - serviceManager.RegisterHttpFactory(); + var serviceProvider = services.BuildSlimServiceProvider(); + if (serviceProvider is IServiceManager serviceManager) + { + serviceManager.RegisterDebugLoggerFactory(); + serviceManager.RegisterHttpFactory(); + serviceManager.RegisterResolver(new LoggerResolver()); + } + + return serviceProvider; } /// @@ -40,10 +49,10 @@ namespace System.Windows.Extensions /// protected abstract void ApplicationClosing(); /// - /// Register services into the . + /// Register services into the . /// - /// - protected abstract void RegisterServices(IServiceProducer serviceProducer); + /// + protected abstract void RegisterServices(IServiceCollection services); /// /// Handle a caught exception. /// @@ -54,17 +63,18 @@ namespace System.Windows.Extensions protected sealed override void OnStartup(StartupEventArgs e) { this.SetupExceptionHandling(); - this.SetupServiceManager(this.ServiceManager); - this.RegisterServices(this.ServiceManager); + this.RegisterInternals(); + this.RegisterServices(this.services); + this.ServiceProvider = this.SetupServiceProvider(this.services); this.SetupApplicationLifetime(); this.LaunchWindow(); } protected sealed override void OnExit(ExitEventArgs e) { base.OnExit(e); - foreach (var service in this.ServiceManager.GetServicesOfType()) + foreach (var service in this.ServiceProvider.GetServices()) { - service.OnClosing(); + service?.OnClosing(); } this.ApplicationClosing(); @@ -77,9 +87,9 @@ namespace System.Windows.Extensions private void LaunchWindow() { - var window = this.ServiceManager.GetService(); + var window = this.ServiceProvider.GetRequiredService(); this.ApplicationStarting(); - foreach(var service in this.ServiceManager.GetServicesOfType()) + foreach(var service in this.ServiceProvider.GetServices()) { service.OnStartup(); } @@ -88,9 +98,7 @@ namespace System.Windows.Extensions } private void RegisterInternals() { - this.ServiceManager.RegisterServiceManager(); - this.ServiceManager.RegisterSingleton(); - this.ServiceManager.RegisterResolver(new LoggerResolver()); + this.services.AddSingleton(); } private void SetupExceptionHandling() { diff --git a/WpfExtended/WpfExtended.csproj b/WpfExtended/WpfExtended.csproj index c5dccb2..993f024 100644 --- a/WpfExtended/WpfExtended.csproj +++ b/WpfExtended/WpfExtended.csproj @@ -6,7 +6,7 @@ true true License.txt - 0.6.2 + 0.7 latest Extension library for Windows Presentation Platform. @@ -86,9 +86,10 @@ http://www.java2s.com/Open-Source/CSharp_Free_Code/Windows_Presentation_Foundati - + + - +