Skip to content

Commit

Permalink
Merge pull request #4 from patrickklaeren/ignore-types
Browse files Browse the repository at this point in the history
Selective interface registration
  • Loading branch information
patrickklaeren authored Mar 2, 2024
2 parents 0e1a0e3 + fd8821a commit 3976017
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 55 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Patrick Klaeren
Copyright (c) 2024 Patrick Klaeren

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ serviceCollection.BuildServiceProvider();

AutoRegisterInject will remove illegal characters from assembly names in order to generate legal C# method names. `,`, `.` and ` ` will be removed.

### Ignoring interfaces

By default ARI will register a type with all the interfaces it implements, however this excludes `System.IDisposable` and its `IAsyncDisposable` counterpart.

You can ignore interfaces by telling ARI to *explicitly* register with only declared interfaces in the given attributes:

```cs
public interface IFoo { }
public interface IBar { }
[RegisterScoped(typeof(IBar))]
public class Foo;
```

this will result in `Foo` ONLY being registered as `IBar` and the following output:

```cs
serviceCollection.AddTransient<IBar, Foo>();
```

`IFoo` will be ignored entirely.

Where you want to register as multiple interfaces, you can pass multiple types.

```cs
[RegisterScoped(typeof(IBar), typeof(IFoo))]
public class Foo;
```

This works for all applicable attributes.

## License

AutoRegisterInject is MIT licensed. Do with it what you please under the terms of MIT.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace AutoRegisterInject.IntegrationTest.Project1
{
Expand Down Expand Up @@ -66,8 +68,49 @@ public interface IMultiInterfaceTest
}

[RegisterScoped]
public class MultiInterfaceTest : IInterfaceTest, IMultiInterfaceTest
public class MultiInterfaceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable
{
public void Dispose()
{
// TODO release managed resources here
}

public ValueTask DisposeAsync()
{
return new ValueTask();
}
}

public interface IIgnore
{

}

[RegisterScoped(onlyRegisterAs: typeof(IIgnore))]
public class MultiInterfaceIgnoranceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable, IIgnore
{
public void Dispose()
{
// TODO release managed resources here
}

public ValueTask DisposeAsync()
{
return new ValueTask();
}
}

[RegisterScoped(typeof(IIgnore), typeof(IInterfaceTest))]
public class MultiInterfaceMultiIgnoranceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable, IIgnore
{
public void Dispose()
{
// TODO release managed resources here
}

public ValueTask DisposeAsync()
{
return new ValueTask();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using Microsoft.Extensions.DependencyInjection;

namespace AutoRegisterInject.IntegrationTest.Project2
{
Expand All @@ -25,4 +26,27 @@ public partial class PartialClassTest
{

}
}

public class FooBarAttribute : Attribute
{
public FooBarAttribute(params Type[] ignoreInterfaces)
{

}
}

public class BarAttribute : FooBarAttribute
{
public BarAttribute(params Type[] ignoreInterfaces)
{

}
}

[Bar(typeof(PartialClassTest))]
public class Destination
{

}

}
38 changes: 19 additions & 19 deletions src/AutoRegisterInject.Tests/AutoRegisterInject.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.9.2" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

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

</Project>
21 changes: 15 additions & 6 deletions src/AutoRegisterInject.Tests/GenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,28 @@ public partial class GenerationTests
private static readonly ReferenceAssemblies Reference = ReferenceAssemblies
.Net
.Net60
.AddPackages(ImmutableArray.Create(new PackageIdentity("Microsoft.Extensions.DependencyInjection", "7.0.0"),
new PackageIdentity("Microsoft.Extensions.Hosting.Abstractions", "7.0.0")));
.AddPackages(ImmutableArray.Create(new PackageIdentity("Microsoft.Extensions.DependencyInjection", "8.0.0"),
new PackageIdentity("Microsoft.Extensions.Hosting.Abstractions", "8.0.0")));

private const string ATTRIBUTE_SOURCE_OUTPUT = @"// <auto-generated>
internal const string ATTRIBUTE_SOURCE_OUTPUT = @"// <auto-generated>
// Automatically generated by AutoRegisterInject.
// Changes made to this file may be lost and may cause undesirable behaviour.
// </auto-generated>
[System.AttributeUsage(System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class RegisterScopedAttribute : System.Attribute { }
internal sealed class RegisterScopedAttribute : System.Attribute
{
internal RegisterScopedAttribute(params System.Type[] onlyRegisterAs) { }
}
[System.AttributeUsage(System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class RegisterSingletonAttribute : System.Attribute { }
internal sealed class RegisterSingletonAttribute : System.Attribute
{
internal RegisterSingletonAttribute(params System.Type[] onlyRegisterAs) { }
}
[System.AttributeUsage(System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class RegisterTransientAttribute : System.Attribute { }
internal sealed class RegisterTransientAttribute : System.Attribute
{
internal RegisterTransientAttribute(params System.Type[] onlyRegisterAs) { }
}
[System.AttributeUsage(System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class RegisterHostedServiceAttribute : System.Attribute { }";

Expand Down
2 changes: 1 addition & 1 deletion src/AutoRegisterInject/AutoRegisterInject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<RepositoryUrl>https://github.com/patrickklaeren/AutoRegisterInject</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>source generator;dependency injection;dependencies;registration;extensions;ioc</PackageTags>
<Version>1.2.1</Version>
<Version>1.3.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
40 changes: 40 additions & 0 deletions src/AutoRegisterInject/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Linq;
using Microsoft.CodeAnalysis;

namespace AutoRegisterInject;

public static class Extensions
{
public static AttributeData GetFirstAutoRegisterAttribute(this ISymbol symbol, string attributeName)
{
return symbol.GetAttributes().First(ad => ad.AttributeClass?.Name == attributeName);
}

public static string[] GetIgnoredTypeNames(this AttributeData attributeData, string parameterName)
{
if (attributeData.AttributeConstructor is null)
{
return null;
}

var parameterIndex = attributeData
.AttributeConstructor
.Parameters
.ToList()
.FindIndex(c => c.Name == parameterName);

if (parameterIndex < 0)
{
return null;
}

var values = attributeData
.ConstructorArguments[parameterIndex]
.Values
.Select(x => x.Value.ToString())
.ToArray();

return values;
}
}
Loading

0 comments on commit 3976017

Please sign in to comment.