From 3df2f507654836bd01579c8569ec623cca9465c9 Mon Sep 17 00:00:00 2001 From: Samuele Lorefice Date: Wed, 24 Sep 2025 04:07:24 +0200 Subject: [PATCH] Fixes #2 by providing a proper implementation of GetServices that actually returns instances of those services instead of just a list of types. --- DISandbox/Program.cs | 8 ++++++++ Syrette/ServiceContainer.cs | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/DISandbox/Program.cs b/DISandbox/Program.cs index 513a1fc..f50aed7 100644 --- a/DISandbox/Program.cs +++ b/DISandbox/Program.cs @@ -12,6 +12,12 @@ class Service : IService { } } +class AnotherService : IService { + public void Log(string message) { + Console.WriteLine($"[AnotherService] {message}"); + } +} + interface IOtherService { public Guid Id { get; } @@ -47,6 +53,7 @@ static class Program { static void Main(string[] args) { var container = new ServiceContainer() .AddSingleton() + .AddTransient() .AddTransient() .AddTransient(); @@ -55,5 +62,6 @@ static class Program { container.GetService().ShowId(); container.GetService().LogWithId("Hello, sent from the dependency."); container.GetService().Log("Goodbye, Dependency Injection!"); + var res = container.GetServices(); } } \ No newline at end of file diff --git a/Syrette/ServiceContainer.cs b/Syrette/ServiceContainer.cs index a32afc1..a0c226a 100644 --- a/Syrette/ServiceContainer.cs +++ b/Syrette/ServiceContainer.cs @@ -18,6 +18,14 @@ public class ServiceContainer { descriptors.Where(d => d.ServiceType == typeof(TServices)) .Select(d => d.ImplementationType).ToList(); + /// + /// Get all registered services for a given service type. + /// + /// + public List GetServices() where TService : class => + descriptors.Where(d => d.ServiceType == typeof(TService)) + .Select(d => (TService)GetService(d.ImplementationType)).ToList(); + /// /// Registers a singleton service with its implementation. ///