Fixes #4, renames ServiceLifetime.Lifetime to Singleton, adds support for arguments in constructors, version bumps.
All checks were successful
Nuget Pkg Build / build (push) Successful in 49s

This commit is contained in:
Samuele Lorefice
2025-09-24 18:06:57 +02:00
parent b9fbb4b851
commit c888da8045
5 changed files with 161 additions and 33 deletions

View File

@@ -49,6 +49,16 @@ class GuidDependantService {
}
}
public interface ICacheService {
public Uri CacheLocation { get; set; }
}
public class CacheService : ICacheService {
public required Uri CacheLocation { get; set; }
public CacheService() => CacheLocation = new Uri("http://default.cache");
public CacheService(Uri cacheLocation) => CacheLocation = cacheLocation;
}
static class Program {
static void Main(string[] args) {
var container = new ServiceContainer()
@@ -63,5 +73,12 @@ static class Program {
container.GetService<GuidDependantService>().LogWithId("Hello, sent from the dependency.");
container.GetService<IService>().Log("Goodbye, Dependency Injection!");
var res = container.GetServices<IService>();
var testContainer = new ServiceContainer()
.AddSingleton<ICacheService, CacheService>(new Uri("http://cache.local"));
var iCacheService = testContainer.GetService<ICacheService>();
Console.WriteLine($"[ICacheService] {iCacheService.CacheLocation}");
var cacheService = testContainer.GetService<CacheService>();
Console.WriteLine($"[CacheService] {cacheService.CacheLocation}");
}
}