From b8f2ddad5a79618fffa2106ceae576913c124747 Mon Sep 17 00:00:00 2001 From: Samuele Lorefice Date: Wed, 24 Sep 2025 03:51:39 +0200 Subject: [PATCH] Fixes #1 by searching also inside the implementation types list --- Syrette/ServiceContainer.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Syrette/ServiceContainer.cs b/Syrette/ServiceContainer.cs index 8115381..06cc528 100644 --- a/Syrette/ServiceContainer.cs +++ b/Syrette/ServiceContainer.cs @@ -92,12 +92,12 @@ public class ServiceContainer { /// /// Resolves and returns an instance of the requested service type. /// - /// Interface type of the service being requested + /// Interface type of the service being requested /// Resolved service instance - public TInterface GetService() { - var descriptor = descriptors.FirstOrDefault(d => d.ServiceType == typeof(TInterface)); + public TService GetService() { + var descriptor = descriptors.FirstOrDefault(d => d.ServiceType == typeof(TService) || d.ImplementationType == typeof(TService)); - if (descriptor == null) throw new Exception($"Service of type {typeof(TInterface)} not registered."); + if (descriptor == null) throw new Exception($"Service of type {typeof(TService)} not registered."); var ctors = descriptor.ImplementationType.GetConstructors(); int max = -1; @@ -115,19 +115,19 @@ public class ServiceContainer { } } if (bestCtor == null) - throw new Exception($"Cannot create service of type {typeof(TInterface)}. No suitable constructor found."); + throw new Exception($"Cannot create service of type {typeof(TService)}. No suitable constructor found."); // Transient: create a new instance each time if (descriptor.Lifetime != ServiceLifetime.Lifetime) { - var service = Instantiate(descriptor, bestCtor); + var service = Instantiate(descriptor, bestCtor); return service; } // Singleton: return existing instance - if (singletons.TryGetValue(descriptor.ServiceType, out object? singleton)) return (TInterface)singleton; + if (singletons.TryGetValue(descriptor.ServiceType, out object? singleton)) return (TService)singleton; // or create a new one if not yet created. - var newSingleton = Instantiate(descriptor); + var newSingleton = Instantiate(descriptor); singletons[descriptor.ServiceType] = newSingleton!; return newSingleton; }