Compare commits
5 Commits
v0.0.1.4-a
...
v0.0.1.5-a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b9fbb4b851 | ||
|
|
3df2f50765 | ||
|
|
d20788de33 | ||
|
|
b8f2ddad5a | ||
| 86513ec6c6 |
@@ -26,4 +26,6 @@ jobs:
|
||||
- name: Pack nugets
|
||||
run: dotnet pack Syrette -c Release --no-build --output . --include-symbols --include-source -p:SymbolPackageFormat=snupkg
|
||||
- name: Push to NuGet
|
||||
run: dotnet nuget push "*.nupkg" --api-key ${{secrets.NUGETAPIKEY}} --source https://api.nuget.org/v3/index.json
|
||||
run: dotnet nuget push "*.nupkg" --api-key ${{secrets.NUGETAPIKEY}} --skip-duplicate --source https://api.nuget.org/v3/index.json
|
||||
- name: Push to Gitea
|
||||
run: dotnet nuget push "*.nupkg" --api-key ${{secrets.NUGETGITEA}} --skip-duplicate --source https://git.r3d.codes/api/packages/REDCODE/nuget/index.json
|
||||
|
||||
@@ -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<IService, Service>()
|
||||
.AddTransient<IService, AnotherService>()
|
||||
.AddTransient<IOtherService, GuidService>()
|
||||
.AddTransient<GuidDependantService, GuidDependantService>();
|
||||
|
||||
@@ -55,5 +62,6 @@ static class Program {
|
||||
container.GetService<IOtherService>().ShowId();
|
||||
container.GetService<GuidDependantService>().LogWithId("Hello, sent from the dependency.");
|
||||
container.GetService<IService>().Log("Goodbye, Dependency Injection!");
|
||||
var res = container.GetServices<IService>();
|
||||
}
|
||||
}
|
||||
@@ -14,10 +14,18 @@ public class ServiceContainer {
|
||||
/// </summary>
|
||||
/// <typeparam name="TServices"></typeparam>
|
||||
/// <returns></returns>
|
||||
public List<Type> GetServices<TServices>() =>
|
||||
public List<Type> GetServiceTypes<TServices>() =>
|
||||
descriptors.Where(d => d.ServiceType == typeof(TServices))
|
||||
.Select(d => d.ImplementationType).ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Get all registered services for a given service type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TService"></typeparam>
|
||||
public List<TService> GetServices<TService>() where TService : class =>
|
||||
descriptors.Where(d => d.ServiceType == typeof(TService))
|
||||
.Select(d => (TService)GetService(d.ImplementationType)).ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Registers a singleton service with its implementation.
|
||||
/// </summary>
|
||||
@@ -92,12 +100,12 @@ public class ServiceContainer {
|
||||
/// <summary>
|
||||
/// Resolves and returns an instance of the requested service type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TInterface">Interface type of the service being requested</typeparam>
|
||||
/// <typeparam name="TService">Interface type of the service being requested</typeparam>
|
||||
/// <returns>Resolved service instance</returns>
|
||||
public TInterface GetService<TInterface>() {
|
||||
var descriptor = descriptors.FirstOrDefault(d => d.ServiceType == typeof(TInterface));
|
||||
public TService GetService<TService>() {
|
||||
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 +123,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<TInterface>(descriptor, bestCtor);
|
||||
var service = Instantiate<TService>(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<TInterface>(descriptor);
|
||||
var newSingleton = Instantiate<TService>(descriptor);
|
||||
singletons[descriptor.ServiceType] = newSingleton!;
|
||||
return newSingleton;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<LangVersion>latest</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Version>0.0.1.4-alpha</Version>
|
||||
<Version>0.0.1.5-alpha</Version>
|
||||
<Title>Syrette </Title>
|
||||
<Authors>Lorefice Samuele</Authors>
|
||||
<Description>Syrette is a minimalistic dependency injection library for C#. It aims to provide a simple and efficient way to achieve dependency injections in your applications without the overhead of larger frameworks.</Description>
|
||||
|
||||
Reference in New Issue
Block a user