Skip to content

Commit

Permalink
Merge pull request #13 from Moreault/2_2_0
Browse files Browse the repository at this point in the history
2 2 0
  • Loading branch information
Moreault authored Jan 11, 2024
2 parents f9ca03b + 3ecdc7f commit 1003b96
Show file tree
Hide file tree
Showing 20 changed files with 256 additions and 106 deletions.
22 changes: 10 additions & 12 deletions .github/workflows/nuget.yml → .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
name: Publish to NuGet
name: Release

on:
push:
branches:
- master

env:
NuGetDirectory: ${{ github.workspace}}/nuget
release:
types: [published]
tags:
- "[0-9]+.[0-9]+.[0-9]+"
- "[0-9]+.[0-9]+.[0-9]+-*"

jobs:
build-and-publish:
Expand All @@ -28,10 +27,9 @@ jobs:

- name: Test
run: dotnet test --no-restore --verbosity normal

- name: Pack projects
run: dotnet pack --configuration Release --output ${{ env.NuGetDirectory }}
run: dotnet pack --no-build -c Release -o nupkg /p:PackageVersion=${{ github.event.release.tag_name }}

- name: Publish to NuGet.org
run: |
nuget push ${{ env.NuGetDirectory }}/ToolBX.AutoInject.*.nupkg -Source 'https://api.nuget.org/v3/index.json' -ApiKey ${{secrets.NUGET_API_KEY}} -SkipDuplicate
- name: Publish to NuGet
run: dotnet nuget push ./nupkg/*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
12 changes: 12 additions & 0 deletions AutoInject.Sample/AbstractGreeter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace AutoInject.Sample;

public abstract class AbstractGreeter
{
public abstract string Greet();
}

[AutoInject<AbstractGreeter>]
public class ConcreteGreeter : AbstractGreeter, IWeirdGreeter
{
public override string Greet() => "Hello, theoretically";
}
27 changes: 14 additions & 13 deletions AutoInject.Sample/AutoInject.Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ToolBX.AssemblyInitializer.Console" Version="2.0.2" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ToolBX.AssemblyInitializer.Console" Version="2.2.0-beta4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AutoInject\AutoInject.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AutoInject\AutoInject.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion AutoInject.Sample/FormalGreeter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public interface IFormalGreeter
string Greet();
}

[AutoInject]
[AutoInject(ServiceLifetime.Scoped)]
public class FormalGreeter : IFormalGreeter
{
public string Greet() => "Hello";
Expand Down
2 changes: 1 addition & 1 deletion AutoInject.Sample/GenericGreeter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public interface IGenericGreeter<out T>
T Greet();
}

[AutoInject]
[AutoInject(Lifetime = ServiceLifetime.Scoped)]
public class GenericGreeter : IGenericGreeter<string>
{
public string Greet() => "Hi!";
Expand Down
1 change: 1 addition & 0 deletions AutoInject.Sample/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

global using AutoInject.Sample;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using ToolBX.AssemblyInitializer.Console;
global using ToolBX.AutoInject;
10 changes: 9 additions & 1 deletion AutoInject.Sample/Greeter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ public class Greeter : IGreeter
private readonly IComplexGreeter<ComplexGreeter> _complexGreeter;
private readonly IGenericGreeter<string> _genericGreeter;
private readonly IOpenGenericGreeter<int> _openGenericGreeter;
private readonly IWeirdGreeter _weirdGreeter;
private readonly AbstractGreeter _abstractGreeter;

public Greeter(IFamiliarGreeter familiarGreeter, IFormalGreeter formalGreeter, IOpenGenericGreeter<int> openGenericGreeter , IComplexGreeter<ComplexGreeter> complexGreeter, IGenericGreeter<string> genericGreeter)
public Greeter(IFamiliarGreeter familiarGreeter, IFormalGreeter formalGreeter, IOpenGenericGreeter<int> openGenericGreeter , IComplexGreeter<ComplexGreeter> complexGreeter, IGenericGreeter<string> genericGreeter, IWeirdGreeter weirdGreeter, AbstractGreeter abstractGreeter)
{
_familiarGreeter = familiarGreeter;
_formalGreeter = formalGreeter;
_openGenericGreeter = openGenericGreeter;
_complexGreeter = complexGreeter;
_genericGreeter = genericGreeter;
_weirdGreeter = weirdGreeter;
_abstractGreeter = abstractGreeter;
}

public string Greet(GreetingKind kind)
Expand All @@ -37,6 +41,10 @@ public string Greet(GreetingKind kind)
return _genericGreeter.Greet();
case GreetingKind.OpenGeneric:
return _openGenericGreeter.Greet().ToString();
case GreetingKind.Weird:
return _weirdGreeter.Greet();
case GreetingKind.Abstract:
return _abstractGreeter.Greet();
default:
return "What?! Stick to the script!";
}
Expand Down
4 changes: 3 additions & 1 deletion AutoInject.Sample/GreetingKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ public enum GreetingKind
Familiar,
Complex,
Generic,
OpenGeneric
OpenGeneric,
Weird,
Abstract,
}
18 changes: 15 additions & 3 deletions AutoInject.Sample/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.Extensions.DependencyInjection;

namespace AutoInject.Sample;
namespace AutoInject.Sample;

public class Startup : ConsoleStartup
{
Expand All @@ -18,6 +16,8 @@ public override void Run(IServiceProvider serviceProvider)
Console.WriteLine("3. Complex");
Console.WriteLine("4. Generic");
Console.WriteLine("5. Open generic");
Console.WriteLine("6. Weird");
Console.WriteLine("7. Abstract");
while (true)
{
var key = Console.ReadKey(true);
Expand Down Expand Up @@ -50,6 +50,18 @@ public override void Run(IServiceProvider serviceProvider)
Console.WriteLine(greeter.Greet(GreetingKind.OpenGeneric));
break;
}

if (key.Key == ConsoleKey.D6)
{
Console.WriteLine(greeter.Greet(GreetingKind.Weird));
break;
}

if (key.Key == ConsoleKey.D7)
{
Console.WriteLine(greeter.Greet(GreetingKind.Abstract));
break;
}
}

Console.WriteLine("Now get the hell of my lawn!");
Expand Down
15 changes: 15 additions & 0 deletions AutoInject.Sample/WeirdGreeter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace AutoInject.Sample;

public interface IWeirdGreeter
{
string Greet();
}

//We need to provide the interface's type to AutoInject since it has a wildly different name from its interface and implements multiple
[AutoInject<IWeirdGreeter>()]
public class Greta : IWeirdGreeter, IFormalGreeter
{
public string Greet() => "Siap!";

string IFormalGreeter.Greet() => Greet();
}
3 changes: 2 additions & 1 deletion AutoInject/AutoConfigAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace ToolBX.AutoInject;

[Obsolete("Use the AutoConfig package from nuget.org instead. Will be removed in 3.0.0")]
[AttributeUsage(AttributeTargets.Class)]
public class AutoConfigAttribute : Attribute
public class AutoConfigAttribute : Attribute
{
public string Name { get; }

Expand Down
32 changes: 16 additions & 16 deletions AutoInject/AutoInject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<RepositoryType>git</RepositoryType>
<Authors>Mathieu Moreault</Authors>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>2.1.1</Version>
<Description>A .NET library to make it easier to use DI by adding [AutoInject] or [AutoConfig] attributes directly on classes.</Description>
<Version>2.2.0-beta6</Version>
<Description>A .NET library to make it easier to use DI by adding [AutoInject] attributes directly on classes.</Description>
<PackageId>ToolBX.$(AssemblyName)</PackageId>
<PackageIcon>autoinject.png</PackageIcon>
<PackageProjectUrl>https://github.com/Moreault/AutoInject</PackageProjectUrl>
Expand All @@ -24,27 +24,27 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
<PackageReference Include="ToolBX.Reflection4Humans.TypeFetcher" Version="2.1.0-beta1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.1" />
<PackageReference Include="ToolBX.Reflection4Humans.TypeFetcher" Version="2.2.0" />

</ItemGroup>

<ItemGroup>
<Compile Update="Resources\Exceptions.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Exceptions.resx</DependentUpon>
</Compile>
<Compile Update="Resources\Exceptions.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Exceptions.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources\Exceptions.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Exceptions.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Update="Resources\Exceptions.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Exceptions.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
Expand Down
32 changes: 29 additions & 3 deletions AutoInject/AutoInjectAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
namespace ToolBX.AutoInject;

[AttributeUsage(AttributeTargets.Class)]
public class AutoInjectAttribute : Attribute
public sealed class AutoInjectAttribute : AutoInjectAttributeBase
{
public ServiceLifetime Lifetime { get; init; }

[Obsolete("Use AutoInject<T> instead. Will be removed in 3.0.0")]
public Type? Interface { get; init; }

public AutoInjectAttribute() { }
public AutoInjectAttribute(ServiceLifetime lifetime) : base(lifetime) { }
}

// ReSharper disable once UnusedTypeParameter : It is used via reflection and implementing types outside its assembly
public sealed class AutoInjectAttribute<T> : AutoInjectAttributeBase
{
public AutoInjectAttribute() { }
public AutoInjectAttribute(ServiceLifetime lifetime) : base(lifetime) { }
}

public abstract class AutoInjectAttributeBase : Attribute
{
public ServiceLifetime Lifetime
{
get => RealLifetime ?? (ServiceLifetime)(-1);
init => RealLifetime = value;
}
internal ServiceLifetime? RealLifetime;

internal AutoInjectAttributeBase() { }

internal AutoInjectAttributeBase(ServiceLifetime lifetime)
{
Lifetime = lifetime;
}
}
6 changes: 6 additions & 0 deletions AutoInject/AutoInjectOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ToolBX.AutoInject;

public sealed record AutoInjectOptions
{
public ServiceLifetime DefaultLifetime { get; init; } = ServiceLifetime.Singleton;
}
5 changes: 4 additions & 1 deletion AutoInject/Usings.cs → AutoInject/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
global using Microsoft.Extensions.Configuration;
// Global using directives

global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using System.Reflection;
global using System.Text.RegularExpressions;
global using ToolBX.AutoInject.Resources;
global using ToolBX.Reflection4Humans.Extensions;
global using ToolBX.Reflection4Humans.TypeFetcher;
2 changes: 1 addition & 1 deletion AutoInject/Resources/Exceptions.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
<value>Impossible d'injecter le service automatiquement : La valeur de ServiceLifetime '{0}' n'est pas supporté</value>
</data>
<data name="CannotInjectServiceBecauseNoInterface" xml:space="preserve">
<value>Impossible d'injecter le service automatiquement : {0} doit avoir au moins une interface</value>
<value>Impossible d'injecter le service automatiquement : {0} doit avoir au moins une interface ou une classe abstraite</value>
</data>
<data name="CannotInjectServiceBecauseNoSimilarInterface" xml:space="preserve">
<value>Impossible d'injecter le service automatiquement : {0} implémente {1} interfaces mais aucune d'elle n'a un nom similaire</value>
Expand Down
2 changes: 1 addition & 1 deletion AutoInject/Resources/Exceptions.resx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
<value>Can't inject service automatically : ServiceLifetime '{0}' is not supported</value>
</data>
<data name="CannotInjectServiceBecauseNoInterface" xml:space="preserve">
<value>Can't inject service automatically : {0} is expected to implement at least one interface</value>
<value>Can't inject service automatically : {0} is expected to implement at least one interface or abstract class</value>
</data>
<data name="CannotInjectServiceBecauseNoSimilarInterface" xml:space="preserve">
<value>Can't inject service automatically : {0} implements {1} interfaces but none of them are close to similar in name</value>
Expand Down
Loading

0 comments on commit 1003b96

Please sign in to comment.