Project rename

This commit is contained in:
Samuele Lorefice
2025-09-21 19:59:54 +02:00
parent df9ad345ea
commit bf46d235af
19 changed files with 119 additions and 29 deletions

View File

@@ -0,0 +1,26 @@
name: Nuget Pkg Build
on:
workflow_dispatch:
push:
tags:
- v*
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup .NET SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.x
- name: Build
run: dotnet build Syrette -c Release
#- name: Test
# run: dotnet test -c Release --no-build
- name: Pack nugets
run: dotnet pack Syrette -c Release --no-build --output .
- name: Push to NuGet
run: dotnet nuget push "*.nupkg" --api-key ${{secrets.NUGETAPIKEY}} --source https://api.nuget.org/v3/index.json

View File

@@ -8,7 +8,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\QuickDI\QuickDI.csproj" /> <ProjectReference Include="..\Syrette\Syrette.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,4 +1,4 @@
using QuickDI; using Syrette;
namespace DISandbox; namespace DISandbox;

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Lorefice Samuele
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -1,10 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -1,3 +1,36 @@
# Quick DI # Syrette
_Extremely tiny dependency injection C# library_ _An extremely tiny dependency injection C# library_
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.
It is designed to be used in minimalistic applications, such as console applications or small services, where simplicity and ease of implementation are preferred over extensive feature sets and robust systems.
## Features
- **Lightweight**: Minimal codebase with no external dependencies. 1 class, the service container. You don't need anything else.
- **Simple API**: You register services using two methods (one for singletons, one for transients) and resolve them with one.
- **Supports Singleton and Transient lifetimes**: Choose between singleton (one instance per container) and transient (new instance per resolution) lifetimes for your services.
## Limitations
- **No support for scoped lifetimes**
- **No support for property injection or method injection.**
- **Every service should have only 1 constructor.** (planned to be lifted in future versions)
## Usage
````csharp
//istantiate your service container
var container = new Syrette.ServiceContainer();
//register your services
container.RegisterSingleton<IMyService, MyService>();
container.RegisterTransient<IOtherService, OtherService>();
//you can also use fluent syntax
container.RegisterSingleton<IMyService, MyService>()
.RegisterTransient<IOtherService, OtherService>()
.RegisterSingleton<IThirdService, ThirdService>();
//resolve your services
var myService = container.GetService<IMyService>();
````
## Licence
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

View File

@@ -1,6 +1,6 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickDI", "QuickDI\QuickDI.csproj", "{4730ABA2-3979-4B74-A3FF-042F6C3C47D6}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Syrette", "Syrette\Syrette.csproj", "{4730ABA2-3979-4B74-A3FF-042F6C3C47D6}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DISandbox", "DISandbox\DISandbox.csproj", "{536F5490-926D-4B2A-8F07-9A7D1F8B9381}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DISandbox", "DISandbox\DISandbox.csproj", "{536F5490-926D-4B2A-8F07-9A7D1F8B9381}"
EndProject EndProject

View File

@@ -1,10 +1,8 @@
using System.Reflection; namespace Syrette;
namespace QuickDI;
public class ServiceContainer { public class ServiceContainer {
private List<ServiceDescriptor> _descriptors = new(); private readonly List<ServiceDescriptor> descriptors = new();
private Dictionary<Type, object> _singletons = new(); private readonly Dictionary<Type, object> singletons = new();
/// <summary> /// <summary>
/// Get all registered implementation types for a given service type. /// Get all registered implementation types for a given service type.
@@ -12,12 +10,12 @@ public class ServiceContainer {
/// <typeparam name="TServices"></typeparam> /// <typeparam name="TServices"></typeparam>
/// <returns></returns> /// <returns></returns>
public List<Type> GetServices<TServices>() => public List<Type> GetServices<TServices>() =>
_descriptors.Where(d => d.ServiceType == typeof(TServices)) descriptors.Where(d => d.ServiceType == typeof(TServices))
.Select(d => d.ImplementationType).ToList(); .Select(d => d.ImplementationType).ToList();
public ServiceContainer AddSingleton<TInterface, TImplementation>() public ServiceContainer AddSingleton<TInterface, TImplementation>()
where TImplementation : class, TInterface { where TImplementation : class, TInterface {
_descriptors.Add(new ServiceDescriptor { descriptors.Add(new ServiceDescriptor {
ServiceType = typeof(TInterface), ServiceType = typeof(TInterface),
ImplementationType = typeof(TImplementation), ImplementationType = typeof(TImplementation),
Lifetime = ServiceLifetime.Lifetime, Lifetime = ServiceLifetime.Lifetime,
@@ -29,7 +27,7 @@ public class ServiceContainer {
public ServiceContainer AddTransient<TInterface, TImplementation>() public ServiceContainer AddTransient<TInterface, TImplementation>()
where TImplementation : class, TInterface { where TImplementation : class, TInterface {
_descriptors.Add(new ServiceDescriptor { descriptors.Add(new ServiceDescriptor {
ServiceType = typeof(TInterface), ServiceType = typeof(TInterface),
ImplementationType = typeof(TImplementation), ImplementationType = typeof(TImplementation),
Lifetime = ServiceLifetime.Transient, Lifetime = ServiceLifetime.Transient,
@@ -51,7 +49,7 @@ public class ServiceContainer {
} }
public TInterface GetService<TInterface>() { public TInterface GetService<TInterface>() {
var descriptor = _descriptors.FirstOrDefault(d => d.ServiceType == typeof(TInterface)); var descriptor = descriptors.FirstOrDefault(d => d.ServiceType == typeof(TInterface));
if (descriptor == null) throw new Exception($"Service of type {typeof(TInterface)} not registered."); if (descriptor == null) throw new Exception($"Service of type {typeof(TInterface)} not registered.");
@@ -59,7 +57,7 @@ public class ServiceContainer {
//TODO: some services might be asking for specific implementations, not interfaces. We should check for that too. //TODO: some services might be asking for specific implementations, not interfaces. We should check for that too.
var missing = descriptor.RequiredTypes var missing = descriptor.RequiredTypes
//filter all required types that are not in the registered descriptors //filter all required types that are not in the registered descriptors
.Where(t => _descriptors.All(d => d.ServiceType != t)) .Where(t => descriptors.All(d => d.ServiceType != t))
.Select(t => t.Name) .Select(t => t.Name)
.ToList(); .ToList();
@@ -73,11 +71,11 @@ public class ServiceContainer {
} }
// Singleton: return existing instance // Singleton: return existing instance
if (_singletons.TryGetValue(descriptor.ServiceType, out object? singleton)) return (TInterface)singleton; if (singletons.TryGetValue(descriptor.ServiceType, out object? singleton)) return (TInterface)singleton;
// or create a new one if not yet created. // or create a new one if not yet created.
var newSingleton = Instantiate<TInterface>(descriptor); var newSingleton = Instantiate<TInterface>(descriptor);
_singletons[descriptor.ServiceType] = newSingleton!; singletons[descriptor.ServiceType] = newSingleton!;
return newSingleton; return newSingleton;
} }

View File

@@ -1,4 +1,4 @@
namespace QuickDI; namespace Syrette;
public class ServiceDescriptor { public class ServiceDescriptor {
public required Type ServiceType { get; set; } public required Type ServiceType { get; set; }

View File

@@ -1,4 +1,4 @@
namespace QuickDI; namespace Syrette;
public enum ServiceLifetime { public enum ServiceLifetime {
Lifetime, Lifetime,

22
Syrette/Syrette.csproj Normal file
View File

@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>0.0.1-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>
<Copyright>2025 Lorefice Samuele</Copyright>
<PackageProjectUrl>https://git.r3d.codes/REDCODE/Syrette</PackageProjectUrl>
<PackageLicenseUrl>https://git.r3d.codes/REDCODE/Syrette/src/branch/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://git.r3d.codes/REDCODE/Syrette</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>DI, Dependency Injection</PackageTags>
<Company>Samuele Lorefice</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
</Project>