Skip to content

Commit 39225e1

Browse files
committed
Feat: KeyedService collection
1 parent 0d96553 commit 39225e1

18 files changed

+209
-272
lines changed

src/CodeOfChaos.Extensions.DependencyInjection.Generators/CodeOfChaos.Extensions.DependencyInjection.Generators.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@
4141
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0">
4242
<PrivateAssets>all</PrivateAssets>
4343
</PackageReference>
44+
<PackageReference Include="CodeOfChaos.GeneratorTools" Version="1.7.1" Pack="true" GeneratePathProperty="true" PrivateAssets="all" />
4445
</ItemGroup>
4546

4647
<ItemGroup>
4748
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
49+
<None Include="$(PkgCodeOfChaos_GeneratorTools)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
4850
</ItemGroup>
4951

5052

src/CodeOfChaos.Extensions.DependencyInjection.Generators/Helpers/NullableAttributes.cs

Lines changed: 0 additions & 168 deletions
This file was deleted.

src/CodeOfChaos.Extensions.DependencyInjection.Generators/Helpers/StringBuilderExtensions.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/CodeOfChaos.Extensions.DependencyInjection.Generators/Helpers/StringExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ public static bool TryGetAsServiceLifetimeString(this MemberAccessExpressionSynt
1515
output = input.Name.Identifier.Text;
1616
return ValidLifeTimes.Contains(output);
1717
}
18+
19+
public static bool TryGetAsServiceLifetimeString(this string input, [NotNullWhen(true)] out string? output) {
20+
output = input;
21+
return ValidLifeTimes.Contains(output);
22+
}
1823
}

src/CodeOfChaos.Extensions.DependencyInjection.Generators/Registrations/FactoryCreatedServiceRegistration.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
44
using CodeOfChaos.Extensions.DependencyInjection.Generators.Helpers;
5+
using CodeOfChaos.GeneratorTools;
56
using Microsoft.CodeAnalysis;
67
using Microsoft.CodeAnalysis.CSharp.Syntax;
7-
using System.Text;
88

99
namespace CodeOfChaos.Extensions.DependencyInjection.Generators.Registrations;
1010
// ---------------------------------------------------------------------------------------------------------------------
@@ -22,13 +22,10 @@ string LifeTime
2222
// -----------------------------------------------------------------------------------------------------------------
2323
// Methods
2424
// -----------------------------------------------------------------------------------------------------------------
25-
public void FormatText(StringBuilder builder, string _) {
26-
builder
27-
.IndentLine(2, $"services.Add{LifeTime}<{ServiceTypeName.ToDisplayString()}>(")
28-
.IndentLine(3, $"(provider) => provider.GetRequiredService<{FactoryTypeName.ToDisplayString()}>().Create()")
29-
.IndentLine(2, ");")
30-
;
31-
}
25+
public void FormatText(GeneratorStringBuilder builder, string _) => builder
26+
.AppendLine($"services.Add{LifeTime}<{ServiceTypeName.ToDisplayString()}>(")
27+
.AppendLineIndented($"(provider) => provider.GetRequiredService<{FactoryTypeName.ToDisplayString()}>().Create()")
28+
.AppendLine(");");
3229

3330
// -----------------------------------------------------------------------------------------------------------------
3431
// Constructors

src/CodeOfChaos.Extensions.DependencyInjection.Generators/Registrations/IServiceRegistration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.GeneratorTools;
45
using Microsoft.CodeAnalysis;
5-
using System.Text;
66

77
namespace CodeOfChaos.Extensions.DependencyInjection.Generators.Registrations;
88
// ---------------------------------------------------------------------------------------------------------------------
@@ -13,5 +13,5 @@ public interface IServiceRegistration {
1313
public INamedTypeSymbol ImplementationTypeName { get; }
1414
public string LifeTime { get; }
1515

16-
public void FormatText(StringBuilder builder, string assemblyName);
16+
public void FormatText(GeneratorStringBuilder builder, string assemblyName);
1717
}

src/CodeOfChaos.Extensions.DependencyInjection.Generators/Registrations/InjectablePoolableServiceRegistration.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
44
using CodeOfChaos.Extensions.DependencyInjection.Generators.Helpers;
5+
using CodeOfChaos.GeneratorTools;
56
using Microsoft.CodeAnalysis;
67
using Microsoft.CodeAnalysis.CSharp.Syntax;
7-
using System.Text;
88

99
namespace CodeOfChaos.Extensions.DependencyInjection.Generators.Registrations;
1010
// ---------------------------------------------------------------------------------------------------------------------
@@ -20,14 +20,15 @@ INamedTypeSymbol ImplementationTypeName
2020
// -----------------------------------------------------------------------------------------------------------------
2121
// Methods
2222
// -----------------------------------------------------------------------------------------------------------------
23-
public void FormatText(StringBuilder builder, string assemblyName) {
24-
builder
25-
.IndentLine(2, $"services.Add{LifeTime}<{ServiceTypeName.ToDisplayString()}>(")
26-
.IndentLine(3, $"(provider) => provider.GetRequiredService<{assemblyName}.AutoPooledServices>().{ImplementationTypeName.Name}Pool.Get()")
27-
.IndentLine(2, ");")
28-
;
29-
}
23+
public void FormatText(GeneratorStringBuilder builder, string assemblyName) => builder
24+
.AppendLine($"services.Add{LifeTime}<{ServiceTypeName.ToDisplayString()}>(")
25+
.AppendLineIndented($"(provider) => provider.GetRequiredService<{assemblyName}.AutoPooledServices>().{ImplementationTypeName.Name}Pool.Get()")
26+
.AppendLine(");");
3027

28+
public void FormatPoolText(GeneratorStringBuilder builder) => builder
29+
.AppendLine($"public ObjectPool<{ImplementationTypeName.ToDisplayString()}> {ImplementationTypeName.Name}Pool {{ get; }} = _objectPoolProvider")
30+
.AppendLineIndented($".Create(new CodeOfChaos.Extensions.DependencyInjection.PooledInjectableServiceObjectPolicy<{ImplementationTypeName.ToDisplayString()}>());");
31+
3132
// -----------------------------------------------------------------------------------------------------------------
3233
// Constructors
3334
// -----------------------------------------------------------------------------------------------------------------
@@ -61,10 +62,4 @@ out InjectablePoolableServiceRegistration registration
6162

6263
return true;
6364
}
64-
65-
66-
public void FormatPoolText(StringBuilder builder) {
67-
builder.IndentLine(1, $"public ObjectPool<{ImplementationTypeName.ToDisplayString()}> {ImplementationTypeName.Name}Pool {{ get; }} = _objectPoolProvider")
68-
.IndentLine(2, $".Create(new CodeOfChaos.Extensions.DependencyInjection.PooledInjectableServiceObjectPolicy<{ImplementationTypeName.ToDisplayString()}>());");
69-
}
7065
}

src/CodeOfChaos.Extensions.DependencyInjection.Generators/Registrations/InjectableServiceRegistration.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
44
using CodeOfChaos.Extensions.DependencyInjection.Generators.Helpers;
5+
using CodeOfChaos.GeneratorTools;
56
using Microsoft.CodeAnalysis;
67
using Microsoft.CodeAnalysis.CSharp.Syntax;
7-
using System.Text;
88

99
namespace CodeOfChaos.Extensions.DependencyInjection.Generators.Registrations;
1010
// ---------------------------------------------------------------------------------------------------------------------
@@ -20,9 +20,9 @@ string LifeTime
2020
// -----------------------------------------------------------------------------------------------------------------
2121
// Methods
2222
// -----------------------------------------------------------------------------------------------------------------
23-
public void FormatText(StringBuilder builder, string _) {
24-
builder.IndentLine(2, $"services.Add{LifeTime}<{ServiceTypeName.ToDisplayString()}, {ImplementationTypeName.ToDisplayString()}>();");
25-
}
23+
public void FormatText(GeneratorStringBuilder builder, string _) => builder
24+
.AppendLine($"services.Add{LifeTime}<{ServiceTypeName.ToDisplayString()}, {ImplementationTypeName.ToDisplayString()}>();");
25+
2626
// -----------------------------------------------------------------------------------------------------------------
2727
// Constructors
2828
// -----------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)