Skip to content

Commit d769d60

Browse files
committed
Fix: Correct typos and improve service registration logic
Renamed `SpecifcInjectableServiceRegistration` to `SpecificInjectableServiceRegistration` to fix spelling errors. Adjusted string checks and formatting in service registration methods to enhance clarity and accuracy, including updating argument handling for key-based registration.
1 parent f198d26 commit d769d60

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public record struct InjectableServiceRegistration(
2424
// Methods
2525
// -----------------------------------------------------------------------------------------------------------------
2626
public void FormatText(GeneratorStringBuilder builder, string _) {
27-
if (Key is not null) builder.AppendLine($"services.AddKeyed{LifeTime}<{ServiceTypeName.ToDisplayString()}, {ImplementationTypeName.ToDisplayString()}>({Key.ToQuotedString()});");
27+
if (Key is not null) builder.AppendLine($"services.AddKeyed{LifeTime}<{ServiceTypeName.ToDisplayString()}, {ImplementationTypeName.ToDisplayString()}>(serviceKey:{Key.ToQuotedString()});");
2828
builder.AppendLine($"services.Add{LifeTime}<{ServiceTypeName.ToDisplayString()}, {ImplementationTypeName.ToDisplayString()}>();");
2929
}
3030

@@ -50,7 +50,7 @@ out InjectableServiceRegistration registration
5050
if (genericNameSyntax?.TypeArgumentList.Arguments.FirstOrDefault() is not {} serviceTypeSyntax) return false;
5151
if (resolver.ResolveSymbol(serviceTypeSyntax) is not INamedTypeSymbol serviceNamedTypeSymbol) return false;
5252

53-
AttributeData? keyedServiceAttribute = attributes.FirstOrDefault(attr => attr.AttributeClass?.ToDisplayString().Contains("KeyedInjectableServiceAttribute") ?? false);
53+
AttributeData? keyedServiceAttribute = attributes.FirstOrDefault(attr => attr.AttributeClass?.ToDisplayString().Contains("InjectableServiceAttribute") ?? false);
5454
string? key = (string?)keyedServiceAttribute?.ConstructorArguments.ElementAtOrDefault(1).Value;
5555
int lifeTime = (int)(keyedServiceAttribute?.ConstructorArguments.ElementAtOrDefault(0).Value ?? -1);
5656

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace CodeOfChaos.Extensions.DependencyInjection.Generators.Registrations;
1313
// Code
1414
// ---------------------------------------------------------------------------------------------------------------------
1515
// ReSharper disable once StructCanBeMadeReadOnly
16-
public record struct SpecifcInjectableServiceRegistration(
16+
public record struct SpecificInjectableServiceRegistration(
1717
INamedTypeSymbol ServiceTypeName,
1818
INamedTypeSymbol ImplementationTypeName,
1919
string LifeTime,
@@ -24,7 +24,10 @@ public record struct SpecifcInjectableServiceRegistration(
2424
// Methods
2525
// -----------------------------------------------------------------------------------------------------------------
2626
public void FormatText(GeneratorStringBuilder builder, string _) {
27-
if (!string.IsNullOrWhiteSpace(Key)) builder.AppendLine($"services.AddKeyed{LifeTime}<{ServiceTypeName.ToDisplayString()}, {ImplementationTypeName.ToDisplayString()}>({Key!.ToQuotedString()});");
27+
if (!string.IsNullOrWhiteSpace(Key)) {
28+
builder.AppendLine($"services.AddKeyed{LifeTime}<{ServiceTypeName.ToDisplayString()}, {ImplementationTypeName.ToDisplayString()}>({Key!.ToQuotedString()});");
29+
return;
30+
}
2831
builder.AppendLine($"services.Add{LifeTime}<{ServiceTypeName.ToDisplayString()}, {ImplementationTypeName.ToDisplayString()}>();");
2932
}
3033

@@ -35,7 +38,7 @@ public static bool TryCreateFromModel(
3538
INamedTypeSymbol implementationTypeSymbol,
3639
AttributeSyntax attribute,
3740
ISymbolResolver resolver,
38-
out SpecifcInjectableServiceRegistration registration
41+
out SpecificInjectableServiceRegistration registration
3942
) {
4043
registration = default;
4144

@@ -50,7 +53,7 @@ out SpecifcInjectableServiceRegistration registration
5053
if (genericNameSyntax?.TypeArgumentList.Arguments.FirstOrDefault() is not {} serviceTypeSyntax) return false;
5154
if (resolver.ResolveSymbol(serviceTypeSyntax) is not INamedTypeSymbol serviceNamedTypeSymbol) return false;
5255

53-
AttributeData? keyedServiceAttribute = attributes.FirstOrDefault(attr => attr.AttributeClass?.ToDisplayString().Contains("KeyedInjectableServiceAttribute") ?? false);
56+
AttributeData? keyedServiceAttribute = attributes.FirstOrDefault(attr => attr.AttributeClass?.ToDisplayString().Contains("Injectable") ?? false);
5457
string? key = (string?)keyedServiceAttribute?.ConstructorArguments.ElementAtOrDefault(0).Value;
5558
string lifeTimeName = attribute.Name.ToFullString();
5659

@@ -59,7 +62,7 @@ out SpecifcInjectableServiceRegistration registration
5962
if (lifeTimeName.Contains("Scoped")) lifeTime = "Scoped";
6063
if (lifeTimeName.Contains("Transient")) lifeTime = "Transient";
6164

62-
registration = new SpecifcInjectableServiceRegistration(
65+
registration = new SpecificInjectableServiceRegistration(
6366
serviceNamedTypeSymbol,
6467
implementationTypeSymbol,
6568
lifeTime,

src/CodeOfChaos.Extensions.DependencyInjection.Generators/ServiceRegistrationGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private static List<IServiceRegistration> GetRegistrations(SourceProductionConte
128128
if ((SymbolEqualityComparer.Default.Equals(constructedFrom, injectableSingletonAttributeType)
129129
|| SymbolEqualityComparer.Default.Equals(constructedFrom, injectableScopedAttributeType)
130130
|| SymbolEqualityComparer.Default.Equals(constructedFrom, injectableTransientAttributeType))
131-
&& SpecifcInjectableServiceRegistration.TryCreateFromModel(implementationTypeSymbol, attribute, new SymbolResolver(model), out SpecifcInjectableServiceRegistration specificInjectable)) {
131+
&& SpecificInjectableServiceRegistration.TryCreateFromModel(implementationTypeSymbol, attribute, new SymbolResolver(model), out SpecificInjectableServiceRegistration specificInjectable)) {
132132
registrations.Add(specificInjectable);
133133
continue;
134134
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
// ---------------------------------------------------------------------------------------------------------------------
2-
// Imports
3-
// ---------------------------------------------------------------------------------------------------------------------
4-
5-
using Microsoft.Extensions.DependencyInjection;
6-
7-
namespace CodeOfChaos.Extensions.DependencyInjection;
8-
// ---------------------------------------------------------------------------------------------------------------------
9-
// Code
10-
// ---------------------------------------------------------------------------------------------------------------------
11-
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
12-
public class InjectableServiceAttribute<TService>(ServiceLifetime lifetime, string? key = null) : Attribute {
13-
public ServiceLifetime Lifetime { get; } = lifetime;
14-
public Type ServiceType { get; } = typeof(TService);
15-
public string? Key { get; } = key;
16-
}
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
namespace CodeOfChaos.Extensions.DependencyInjection;
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
// Code
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
12+
public class InjectableServiceAttribute<TService>(ServiceLifetime lifetime, string? key = null) : Attribute {
13+
public ServiceLifetime Lifetime { get; } = lifetime;
14+
public Type ServiceType { get; } = typeof(TService);
15+
public string? Key { get; } = key;
16+
}

0 commit comments

Comments
 (0)