Skip to content

Commit

Permalink
Updated to .NET 8
Browse files Browse the repository at this point in the history
  • Loading branch information
Moreault committed Sep 23, 2024
1 parent a186159 commit 995bbe5
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 82 deletions.
4 changes: 2 additions & 2 deletions AutoInject.Sample/AutoInject.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

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

<ItemGroup>
<PackageReference Include="ToolBX.AssemblyInitializer.Console" Version="2.2.0" />
<PackageReference Include="ToolBX.AssemblyInitializer.Console" Version="3.0.0-beta-1" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 1 addition & 5 deletions AutoInject.Sample/Greeter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ public class Greeter : IGreeter
private readonly IFormalGreeter _formalGreeter;
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, IWeirdGreeter weirdGreeter, AbstractGreeter abstractGreeter)
public Greeter(IFamiliarGreeter familiarGreeter, IFormalGreeter formalGreeter, IComplexGreeter<ComplexGreeter> complexGreeter, IGenericGreeter<string> genericGreeter, IWeirdGreeter weirdGreeter, AbstractGreeter abstractGreeter)
{
_familiarGreeter = familiarGreeter;
_formalGreeter = formalGreeter;
_openGenericGreeter = openGenericGreeter;
_complexGreeter = complexGreeter;
_genericGreeter = genericGreeter;
_weirdGreeter = weirdGreeter;
Expand All @@ -39,8 +37,6 @@ public string Greet(GreetingKind kind)
return _complexGreeter.Greet();
case GreetingKind.Generic:
return _genericGreeter.Greet();
case GreetingKind.OpenGeneric:
return _openGenericGreeter.Greet().ToString();
case GreetingKind.Weird:
return _weirdGreeter.Greet();
case GreetingKind.Abstract:
Expand Down
1 change: 0 additions & 1 deletion AutoInject.Sample/GreetingKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public enum GreetingKind
Familiar,
Complex,
Generic,
OpenGeneric,
Weird,
Abstract,
}
12 changes: 0 additions & 12 deletions AutoInject.Sample/OpenGenericGreeter.cs

This file was deleted.

8 changes: 1 addition & 7 deletions AutoInject.Sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,12 @@ public override void Run(IServiceProvider serviceProvider)
}

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

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

if (key.Key == ConsoleKey.D7)
if (key.Key == ConsoleKey.D6)
{
Console.WriteLine(greeter.Greet(GreetingKind.Abstract));
break;
Expand Down
13 changes: 0 additions & 13 deletions AutoInject/AutoConfigAttribute.cs

This file was deleted.

6 changes: 3 additions & 3 deletions AutoInject/AutoInject.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>ToolBX.AutoInject</RootNamespace>
Expand All @@ -26,9 +26,9 @@
<ItemGroup>
<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.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="ToolBX.Reflection4Humans.TypeFetcher" Version="2.2.0" />
<PackageReference Include="ToolBX.Reflection4Humans.TypeFetcher" Version="3.0.0-beta5" />

</ItemGroup>

Expand Down
3 changes: 0 additions & 3 deletions AutoInject/AutoInjectAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
[AttributeUsage(AttributeTargets.Class)]
public sealed class AutoInjectAttribute : AutoInjectAttributeBase
{
[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) { }
}
Expand Down
37 changes: 1 addition & 36 deletions AutoInject/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ private static IServiceCollection AddAutoInjectServices(this IServiceCollection
var attribute = (AutoInjectAttributeBase)Attribute.GetCustomAttribute(type, typeof(AutoInjectAttributeBase), true)!;

Type implementation;
if (attribute is AutoInjectAttribute nonGeneric && nonGeneric.Interface != null)
implementation = nonGeneric.Interface;
else if (attribute.GetType().GetGenericArguments().Any())
if (attribute.GetType().GetGenericArguments().Any())
implementation = attribute.GetType().GetGenericArguments().Single();
else if (candidates.Length == 1)
implementation = candidates.Single()!;
Expand Down Expand Up @@ -98,39 +96,6 @@ private static IServiceCollection AddAutoInjectServices(this IServiceCollection
return serviceCollection;
}

[Obsolete("Use the AutoConfig package from nuget.org instead. Will be removed in 3.0.0")]
public static IServiceCollection AddAutoConfig(this IServiceCollection services, Assembly assembly, IConfiguration configuration)
{
if (services is null) throw new ArgumentNullException(nameof(services));
if (assembly is null) throw new ArgumentNullException(nameof(assembly));
if (configuration is null) throw new ArgumentNullException(nameof(configuration));

var types = Types.From(assembly).Where(x => !x.IsInterface && !x.IsAbstract && !x.IsGenericTypeDefinition && !x.IsGenericType && x.HasAttribute<AutoConfigAttribute>());
return services.AddAutoConfig(configuration, types);
}

[Obsolete("Use the AutoConfig package from nuget.org instead. Will be removed in 3.0.0")]
public static IServiceCollection AddAutoConfig(this IServiceCollection services, IConfiguration configuration)
{
if (services is null) throw new ArgumentNullException(nameof(services));
if (configuration is null) throw new ArgumentNullException(nameof(configuration));

var types = Types.Where(x => !x.IsInterface && !x.IsAbstract && !x.IsGenericTypeDefinition && !x.IsGenericType && x.HasAttribute<AutoConfigAttribute>());
return services.AddAutoConfig(configuration, types);
}

private static IServiceCollection AddAutoConfig(this IServiceCollection services, IConfiguration configuration, IEnumerable<Type> types)
{
foreach (var type in types)
{
var attribute = (AutoConfigAttribute)Attribute.GetCustomAttribute(type, typeof(AutoConfigAttribute), true)!;
typeof(ServiceCollectionExtensions).GetMethod(nameof(Configure), BindingFlags.Static | BindingFlags.NonPublic)!.MakeGenericMethod(type)
.Invoke(null, BindingFlags.Static | BindingFlags.NonPublic, null, new object[] { services, configuration, attribute.Name }, null);
}

return services;
}

private static IServiceCollection Configure<T>(IServiceCollection services, IConfiguration configuration, string name) where T : class => services.Configure<T>(x => configuration.GetSection(name).Bind(x));

public static IReadOnlyList<T> GetAutoInjectServices<T>(this IServiceProvider serviceProvider)
Expand Down

0 comments on commit 995bbe5

Please sign in to comment.