From bfd5ceb0435bc74202ba811cec3e50c3f509abb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=BCrzd=C3=B6rfer?= Date: Tue, 27 Feb 2024 09:44:27 +0100 Subject: [PATCH] add findAssemblies for discovering types --- .../Extensions/AssemblyExtensions.cs | 12 ++++++++++++ .../Extensions/ImplementationResolverExtensions.cs | 3 +++ .../Services/IImplementationResolver.cs | 3 +++ .../Services/TypeImplementationResolver.cs | 5 +++++ .../TypeImplementationResolverTests.cs | 14 ++++++++++++++ 5 files changed, 37 insertions(+) create mode 100644 PiBox.Hosting/Abstractions/src/PiBox.Hosting.Abstractions/Extensions/AssemblyExtensions.cs diff --git a/PiBox.Hosting/Abstractions/src/PiBox.Hosting.Abstractions/Extensions/AssemblyExtensions.cs b/PiBox.Hosting/Abstractions/src/PiBox.Hosting.Abstractions/Extensions/AssemblyExtensions.cs new file mode 100644 index 0000000..2258021 --- /dev/null +++ b/PiBox.Hosting/Abstractions/src/PiBox.Hosting.Abstractions/Extensions/AssemblyExtensions.cs @@ -0,0 +1,12 @@ +using System.Reflection; + +namespace PiBox.Hosting.Abstractions.Extensions +{ + public static class AssemblyExtensions + { + public static bool HasType(this Assembly assembly, Predicate typePredicate) + { + return assembly.GetTypes().Any(t => typePredicate(t)); + } + } +} diff --git a/PiBox.Hosting/Abstractions/src/PiBox.Hosting.Abstractions/Extensions/ImplementationResolverExtensions.cs b/PiBox.Hosting/Abstractions/src/PiBox.Hosting.Abstractions/Extensions/ImplementationResolverExtensions.cs index f3f2196..78d9b77 100644 --- a/PiBox.Hosting/Abstractions/src/PiBox.Hosting.Abstractions/Extensions/ImplementationResolverExtensions.cs +++ b/PiBox.Hosting/Abstractions/src/PiBox.Hosting.Abstractions/Extensions/ImplementationResolverExtensions.cs @@ -1,3 +1,4 @@ +using System.Reflection; using PiBox.Hosting.Abstractions.Services; namespace PiBox.Hosting.Abstractions.Extensions @@ -16,5 +17,7 @@ public static List FindAndResolve(this IImplementationResolver implementat .Where(x => x is not null) .OfType().ToList(); + public static List FindAssemblies(this IImplementationResolver implementationResolver, + Predicate filter) => implementationResolver.FindAssemblies().Where(f => filter(f)).ToList(); } } diff --git a/PiBox.Hosting/Abstractions/src/PiBox.Hosting.Abstractions/Services/IImplementationResolver.cs b/PiBox.Hosting/Abstractions/src/PiBox.Hosting.Abstractions/Services/IImplementationResolver.cs index d41a509..54bcb02 100644 --- a/PiBox.Hosting/Abstractions/src/PiBox.Hosting.Abstractions/Services/IImplementationResolver.cs +++ b/PiBox.Hosting/Abstractions/src/PiBox.Hosting.Abstractions/Services/IImplementationResolver.cs @@ -1,3 +1,5 @@ +using System.Reflection; + namespace PiBox.Hosting.Abstractions.Services { public interface IImplementationResolver @@ -5,5 +7,6 @@ public interface IImplementationResolver void ClearInstances(); object ResolveInstance(Type type); List FindTypes(); + List FindAssemblies(); } } diff --git a/PiBox.Hosting/WebHost/src/PiBox.Hosting.WebHost/Services/TypeImplementationResolver.cs b/PiBox.Hosting/WebHost/src/PiBox.Hosting.WebHost/Services/TypeImplementationResolver.cs index 441370d..62fd994 100644 --- a/PiBox.Hosting/WebHost/src/PiBox.Hosting.WebHost/Services/TypeImplementationResolver.cs +++ b/PiBox.Hosting/WebHost/src/PiBox.Hosting.WebHost/Services/TypeImplementationResolver.cs @@ -81,6 +81,11 @@ public List FindTypes() return _resolvedTypes.ToList(); } + public List FindAssemblies() + { + return _resolvedTypes.Select(x => x.Assembly).Distinct().ToList(); + } + public void ClearInstances() { foreach (var disposable in _instances.OfType()) diff --git a/PiBox.Hosting/WebHost/test/PiBox.Hosting.WebHost.Tests/TypeImplementationResolverTests.cs b/PiBox.Hosting/WebHost/test/PiBox.Hosting.WebHost.Tests/TypeImplementationResolverTests.cs index b3c1150..3c2d8af 100644 --- a/PiBox.Hosting/WebHost/test/PiBox.Hosting.WebHost.Tests/TypeImplementationResolverTests.cs +++ b/PiBox.Hosting/WebHost/test/PiBox.Hosting.WebHost.Tests/TypeImplementationResolverTests.cs @@ -24,6 +24,20 @@ public void CanFindASpecificType() types.Should().NotContain(typeof(BaseClass)); } + [Test] + public void CanFindAssembliesForPlugins() + { + var typeService = new TypeImplementationResolver(_configuration, _resolvedTypes, new Dictionary()); + var assemblies = typeService.FindAssemblies(); + assemblies.Should().HaveCount(1); + var assembly = assemblies.Single(); + assembly.Should().BeSameAs(typeof(TypeImplementationResolverTests).Assembly); + assemblies = typeService.FindAssemblies(f => f.HasType(t => t.HasAttribute())); + assemblies.Should().HaveCount(1); + assembly = assemblies.Single(); + assembly.Should().BeSameAs(typeof(TypeImplementationResolverTests).Assembly); + } + [Test] public void InstancesWillBeReused() {