From fcc5cbe9a2a7fdc4f585c8ee6ff0174a72162b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20K=C3=B6nig?= Date: Wed, 3 Jan 2024 06:34:23 +0100 Subject: [PATCH] Updated GetServiceProvider and TryGetServiceProvider to be extension methods of IResourceHost --- .../DependencyInjectionTests.cs | 14 ++++++++++++++ ...rolExtensions.cs => IResourceHostExtensions.cs} | 9 ++++----- 2 files changed, 18 insertions(+), 5 deletions(-) rename src/RolandK.AvaloniaExtensions.DependencyInjection/{ControlExtensions.cs => IResourceHostExtensions.cs} (83%) diff --git a/src/RolandK.AvaloniaExtensions.DependencyInjection.Tests/DependencyInjectionTests.cs b/src/RolandK.AvaloniaExtensions.DependencyInjection.Tests/DependencyInjectionTests.cs index 0bfe18b..d651a40 100644 --- a/src/RolandK.AvaloniaExtensions.DependencyInjection.Tests/DependencyInjectionTests.cs +++ b/src/RolandK.AvaloniaExtensions.DependencyInjection.Tests/DependencyInjectionTests.cs @@ -142,6 +142,20 @@ public Task Create_Control_and_get_ServiceProvider_from_it() }); } + [Fact] + public Task Get_ServiceProvider_from_Application() + { + return UnitTestApplication.RunInApplicationContextAsync(() => + { + // Act + var serviceProvider = Application.Current?.GetServiceProvider(); + + // Assert + Assert.NotNull(serviceProvider); + Assert.NotNull(serviceProvider.GetService(typeof(IDummyService))); + }); + } + //************************************************************************* //************************************************************************* //************************************************************************* diff --git a/src/RolandK.AvaloniaExtensions.DependencyInjection/ControlExtensions.cs b/src/RolandK.AvaloniaExtensions.DependencyInjection/IResourceHostExtensions.cs similarity index 83% rename from src/RolandK.AvaloniaExtensions.DependencyInjection/ControlExtensions.cs rename to src/RolandK.AvaloniaExtensions.DependencyInjection/IResourceHostExtensions.cs index b1976c0..8dba4f0 100644 --- a/src/RolandK.AvaloniaExtensions.DependencyInjection/ControlExtensions.cs +++ b/src/RolandK.AvaloniaExtensions.DependencyInjection/IResourceHostExtensions.cs @@ -1,15 +1,14 @@ -using Avalonia; using Avalonia.Controls; namespace RolandK.AvaloniaExtensions.DependencyInjection; -public static class ControlExtensions +public static class IResourceHostExtensions { /// /// Gets the for this application. /// Returns null, if the could not be found. /// - public static IServiceProvider? TryGetServiceProvider(this StyledElement control) + public static IServiceProvider? TryGetServiceProvider(this IResourceHost control) { if(control.TryFindResource(DependencyInjectionConstants.SERVICE_PROVIDER_RESOURCE_KEY, out var resource) && resource is IServiceProvider serviceProvider) @@ -23,7 +22,7 @@ public static class ControlExtensions /// Gets the for this application. /// /// could not be found - public static IServiceProvider GetServiceProvider(this StyledElement control) + public static IServiceProvider GetServiceProvider(this IResourceHost control) { var serviceProvider = TryGetServiceProvider(control); if (serviceProvider == null) @@ -33,4 +32,4 @@ public static IServiceProvider GetServiceProvider(this StyledElement control) } return serviceProvider; } -} +} \ No newline at end of file