Skip to content

Commit dfd4989

Browse files
CopilotDreamescaper
andcommitted
Rename GenerateServiceHandlerAttribute to ScanForTypesAttribute, CustomHandler to Handler
Co-authored-by: Dreamescaper <17177729+Dreamescaper@users.noreply.github.com>
1 parent f138be0 commit dfd4989

6 files changed

Lines changed: 33 additions & 32 deletions

File tree

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ public class HelloWorldEndpoint : IEndpoint
9393

9494
public static partial class ServiceCollectionExtensions
9595
{
96-
[GenerateServiceHandler(AssignableTo = typeof(IEndpoint), CustomHandler = nameof(IEndpoint.MapEndpoint))]
96+
[ScanForTypes(AssignableTo = typeof(IEndpoint), Handler = nameof(IEndpoint.MapEndpoint))]
9797
public static partial IEndpointRouteBuilder MapEndpoints(this IEndpointRouteBuilder endpoints);
9898
}
9999
```
100100

101101
### Register Options types
102-
Another example of `CustomHandler` is to register Options types. We can define custom `OptionAttribute`, which allows to specify configuration section key.
103-
And then read that value in our `CustomHandler`:
102+
Another example of `Handler` is to register Options types. We can define custom `OptionAttribute`, which allows to specify configuration section key.
103+
And then read that value in our `Handler`:
104104
```csharp
105105
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
106106
public class OptionAttribute(string? section = null) : Attribute
@@ -116,7 +116,7 @@ public record SectionOption { }
116116

117117
public static partial class ServiceCollectionExtensions
118118
{
119-
[GenerateServiceHandler(AttributeFilter = typeof(OptionAttribute), CustomHandler = nameof(AddOption))]
119+
[ScanForTypes(AttributeFilter = typeof(OptionAttribute), Handler = nameof(AddOption))]
120120
public static partial IServiceCollection AddOptions(this IServiceCollection services, IConfiguration configuration);
121121

122122
private static void AddOption<T>(IServiceCollection services, IConfiguration configuration) where T : class
@@ -133,7 +133,7 @@ public static partial class ServiceCollectionExtensions
133133
```csharp
134134
public static partial class ModelBuilderExtensions
135135
{
136-
[GenerateServiceHandler(AssignableTo = typeof(IEntityTypeConfiguration<>), CustomHandler = nameof(ApplyConfiguration))]
136+
[ScanForTypes(AssignableTo = typeof(IEntityTypeConfiguration<>), Handler = nameof(ApplyConfiguration))]
137137
public static partial ModelBuilder ApplyEntityConfigurations(this ModelBuilder modelBuilder);
138138

139139
private static void ApplyConfiguration<T, TEntity>(ModelBuilder modelBuilder)
@@ -164,12 +164,12 @@ public static partial class ModelBuilderExtensions
164164
| **ExcludeByTypeName** | Sets this value to exclude types from being registered by their full name. You can use '*' wildcards. You can also use ',' to separate multiple filters. |
165165
| **ExcludeByAttribute** | Excludes matching types by the specified attribute type being present. |
166166
| **KeySelector** | Sets this property to add types as keyed services. This property should point to one of the following: <br>- The name of a static method in the current type with a string return type. The method should be either generic or have a single parameter of type `Type`. <br>- A constant field or static property in the implementation type. |
167-
| **CustomHandler** | *(Obsolete — use `GenerateServiceHandler` instead.)* Sets this property to invoke a custom method for each type found instead of regular registration logic. |
167+
| **CustomHandler** | *(Obsolete — use `ScanForTypes` instead.)* Sets this property to invoke a custom method for each type found instead of regular registration logic. |
168168

169-
`GenerateServiceHandler` attribute is used to invoke a custom method for each matched type. It has the same filtering properties as `GenerateServiceRegistrations`, but without the registration-specific ones (`Lifetime`, `AsImplementedInterfaces`, `AsSelf`, `KeySelector`):
169+
`ScanForTypes` attribute is used to invoke a custom method for each matched type. It has the same filtering properties as `GenerateServiceRegistrations`, but without the registration-specific ones (`Lifetime`, `AsImplementedInterfaces`, `AsSelf`, `KeySelector`):
170170
| Property | Description |
171171
| --- | --- |
172-
| **CustomHandler** | Sets this property to invoke a custom method for each type found. This property should point to one of the following: <br>- Name of a generic method in the current type. <br>- Static method name in found types. <br>**Note:** Types are automatically filtered by the generic constraints defined on the method's type parameters (e.g., `class`, `struct`, `new()`, interface constraints). |
172+
| **Handler** | Sets this property to invoke a custom method for each type found. This property should point to one of the following: <br>- Name of a generic method in the current type. <br>- Static method name in found types. <br>**Note:** Types are automatically filtered by the generic constraints defined on the method's type parameters (e.g., `class`, `struct`, `new()`, interface constraints). |
173173
| **FromAssemblyOf** | Sets the assembly containing the given type as the source of types to scan. If not specified, the assembly containing the method with this attribute will be used. |
174174
| **AssemblyNameFilter** | Sets this value to filter scanned assemblies by assembly name. This option is incompatible with `FromAssemblyOf`. You can use '*' wildcards. You can also use ',' to separate multiple filters. |
175175
| **AssignableTo** | Sets the type that the scanned types must be assignable to. |

ServiceScan.SourceGenerator.Tests/CustomHandlerTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ private static Compilation CreateCompilation(params string[] source)
11011101
}
11021102

11031103
[Fact]
1104-
public void GenerateServiceHandlerAttribute_WithNoParameters()
1104+
public void ScanForTypesAttribute_WithNoParameters()
11051105
{
11061106
var source = $$"""
11071107
using ServiceScan.SourceGenerator;
@@ -1110,7 +1110,7 @@ namespace GeneratorTests;
11101110
11111111
public static partial class ServicesExtensions
11121112
{
1113-
[GenerateServiceHandler(AssignableTo = typeof(IService), CustomHandler = nameof(HandleType))]
1113+
[ScanForTypes(AssignableTo = typeof(IService), Handler = nameof(HandleType))]
11141114
public static partial void ProcessServices();
11151115
11161116
private static void HandleType<T>() => System.Console.WriteLine(typeof(T).Name);
@@ -1149,7 +1149,7 @@ public static partial void ProcessServices()
11491149
}
11501150

11511151
[Fact]
1152-
public void GenerateServiceHandlerAttribute_WithParameters()
1152+
public void ScanForTypesAttribute_WithParameters()
11531153
{
11541154
var source = $$"""
11551155
using ServiceScan.SourceGenerator;
@@ -1158,7 +1158,7 @@ namespace GeneratorTests;
11581158
11591159
public static partial class ServicesExtensions
11601160
{
1161-
[GenerateServiceHandler(TypeNameFilter = "*Service", CustomHandler = nameof(HandleType))]
1161+
[ScanForTypes(TypeNameFilter = "*Service", Handler = nameof(HandleType))]
11621162
public static partial void ProcessServices(string value);
11631163
11641164
private static void HandleType<T>(string value) => System.Console.WriteLine(value + typeof(T).Name);
@@ -1197,7 +1197,7 @@ public static partial void ProcessServices( string value)
11971197
}
11981198

11991199
[Fact]
1200-
public void GenerateServiceHandlerAttribute_MultipleAttributes()
1200+
public void ScanForTypesAttribute_MultipleAttributes()
12011201
{
12021202
var source = $$"""
12031203
using ServiceScan.SourceGenerator;
@@ -1206,8 +1206,8 @@ namespace GeneratorTests;
12061206
12071207
public static partial class ServicesExtensions
12081208
{
1209-
[GenerateServiceHandler(AssignableTo = typeof(IFirstService), CustomHandler = nameof(HandleFirstType))]
1210-
[GenerateServiceHandler(AssignableTo = typeof(ISecondService), CustomHandler = nameof(HandleSecondType))]
1209+
[ScanForTypes(AssignableTo = typeof(IFirstService), Handler = nameof(HandleFirstType))]
1210+
[ScanForTypes(AssignableTo = typeof(ISecondService), Handler = nameof(HandleSecondType))]
12111211
public static partial void ProcessServices();
12121212
12131213
private static void HandleFirstType<T>() => System.Console.WriteLine("First:" + typeof(T).Name);
@@ -1248,7 +1248,7 @@ public static partial void ProcessServices()
12481248
}
12491249

12501250
[Fact]
1251-
public void GenerateServiceHandlerAttribute_MissingCustomHandler_ReportsDiagnostic()
1251+
public void ScanForTypesAttribute_MissingHandler_ReportsDiagnostic()
12521252
{
12531253
var source = $$"""
12541254
using ServiceScan.SourceGenerator;
@@ -1257,7 +1257,7 @@ namespace GeneratorTests;
12571257
12581258
public static partial class ServicesExtensions
12591259
{
1260-
[GenerateServiceHandler(AssignableTo = typeof(IService))]
1260+
[ScanForTypes(AssignableTo = typeof(IService))]
12611261
public static partial void ProcessServices();
12621262
}
12631263
""";
@@ -1281,7 +1281,7 @@ public class MyService : IService { }
12811281
}
12821282

12831283
[Fact]
1284-
public void GenerateServiceHandlerAttribute_MissingSearchCriteria_ReportsDiagnostic()
1284+
public void ScanForTypesAttribute_MissingSearchCriteria_ReportsDiagnostic()
12851285
{
12861286
var source = $$"""
12871287
using ServiceScan.SourceGenerator;
@@ -1290,7 +1290,7 @@ namespace GeneratorTests;
12901290
12911291
public static partial class ServicesExtensions
12921292
{
1293-
[GenerateServiceHandler(CustomHandler = nameof(HandleType))]
1293+
[ScanForTypes(Handler = nameof(HandleType))]
12941294
public static partial void ProcessServices();
12951295
12961296
private static void HandleType<T>() { }
@@ -1308,7 +1308,7 @@ private static void HandleType<T>() { }
13081308
}
13091309

13101310
[Fact]
1311-
public void MixingGenerateServiceRegistrationsAndGenerateServiceHandler_ReportsDiagnostic()
1311+
public void MixingGenerateServiceRegistrationsAndScanForTypes_ReportsDiagnostic()
13121312
{
13131313
var source = $$"""
13141314
using ServiceScan.SourceGenerator;
@@ -1319,7 +1319,7 @@ namespace GeneratorTests;
13191319
public static partial class ServicesExtensions
13201320
{
13211321
[GenerateServiceRegistrations(AssignableTo = typeof(IService))]
1322-
[GenerateServiceHandler(AssignableTo = typeof(IService), CustomHandler = nameof(HandleType))]
1322+
[ScanForTypes(AssignableTo = typeof(IService), Handler = nameof(HandleType))]
13231323
public static partial IServiceCollection ProcessServices(this IServiceCollection services);
13241324
13251325
private static void HandleType<T>() { }

ServiceScan.SourceGenerator/DependencyInjectionGenerator.ParseMethodModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public partial class DependencyInjectionGenerator
1717
if (!method.IsPartialDefinition)
1818
return Diagnostic.Create(NotPartialDefinition, method.Locations[0]);
1919

20-
// Check if GenerateServiceHandlerAttribute is also on this method - that's not allowed
20+
// Check if ScanForTypesAttribute is also on this method - that's not allowed
2121
var hasServiceHandlerAttribute = method.GetAttributes()
2222
.Any(a => a.AttributeClass?.ToDisplayString() == GenerateAttributeInfo.HandlerMetadataName);
2323

ServiceScan.SourceGenerator/DiagnosticDescriptors.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ public static class DiagnosticDescriptors
7575
true);
7676

7777
public static readonly DiagnosticDescriptor MissingCustomHandlerOnGenerateServiceHandler = new("DI0013",
78-
"Missing CustomHandler",
79-
"GenerateServiceHandlerAttribute must have CustomHandler specified",
78+
"Missing Handler",
79+
"ScanForTypesAttribute must have Handler specified",
8080
"Usage",
8181
DiagnosticSeverity.Error,
8282
true);
8383

8484
public static readonly DiagnosticDescriptor CantMixServiceRegistrationsAndServiceHandler = new("DI0014",
85-
"Cannot mix GenerateServiceRegistrationsAttribute and GenerateServiceHandlerAttribute",
86-
"It is not allowed to use both GenerateServiceRegistrationsAttribute and GenerateServiceHandlerAttribute on the same method",
85+
"Cannot mix GenerateServiceRegistrationsAttribute and ScanForTypesAttribute",
86+
"It is not allowed to use both GenerateServiceRegistrationsAttribute and ScanForTypesAttribute on the same method",
8787
"Usage",
8888
DiagnosticSeverity.Error,
8989
true);

ServiceScan.SourceGenerator/GenerateAttributeInfo.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
internal static class GenerateAttributeInfo
44
{
55
public const string MetadataName = "ServiceScan.SourceGenerator.GenerateServiceRegistrationsAttribute";
6-
public const string HandlerMetadataName = "ServiceScan.SourceGenerator.GenerateServiceHandlerAttribute";
6+
public const string HandlerMetadataName = "ServiceScan.SourceGenerator.ScanForTypesAttribute";
77

88
public const string Source = """
99
#nullable enable
@@ -111,22 +111,22 @@ internal class GenerateServiceRegistrationsAttribute : Attribute
111111
/// This property is incompatible with <see cref="Lifetime"/>, <see cref="AsImplementedInterfaces"/>, <see cref="AsSelf"/>,
112112
/// and <see cref="KeySelector"/> properties.
113113
/// </summary>
114-
/// <remarks>This property is obsolete. Use <see cref="GenerateServiceHandlerAttribute"/> instead.</remarks>
115-
[Obsolete("Use GenerateServiceHandlerAttribute instead.", error: false)]
114+
/// <remarks>This property is obsolete. Use <see cref="ScanForTypesAttribute"/> instead.</remarks>
115+
[Obsolete("Use ScanForTypesAttribute instead.", error: false)]
116116
public string? CustomHandler { get; set; }
117117
}
118118
119119
[Conditional("CODE_ANALYSIS")]
120120
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
121-
internal class GenerateServiceHandlerAttribute : Attribute
121+
internal class ScanForTypesAttribute : Attribute
122122
{
123123
/// <summary>
124124
/// Sets this property to invoke a custom method for each type found.
125125
/// This property should point to one of the following:
126126
/// - Name of a generic method in the current type.
127127
/// - Static method name in found types.
128128
/// </summary>
129-
public string? CustomHandler { get; set; }
129+
public string? Handler { get; set; }
130130
131131
/// <summary>
132132
/// Sets the assembly containing the given type as the source of types to scan.

ServiceScan.SourceGenerator/Model/AttributeModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public static AttributeModel Create(AttributeData attribute, IMethodSymbol metho
4747
var excludeByTypeName = attribute.NamedArguments.FirstOrDefault(a => a.Key == "ExcludeByTypeName").Value.Value as string;
4848
var excludeAssignableTo = attribute.NamedArguments.FirstOrDefault(a => a.Key == "ExcludeAssignableTo").Value.Value as INamedTypeSymbol;
4949
var keySelector = attribute.NamedArguments.FirstOrDefault(a => a.Key == "KeySelector").Value.Value as string;
50-
var customHandler = attribute.NamedArguments.FirstOrDefault(a => a.Key == "CustomHandler").Value.Value as string;
50+
var customHandler = (attribute.NamedArguments.FirstOrDefault(a => a.Key == "Handler").Value.Value
51+
?? attribute.NamedArguments.FirstOrDefault(a => a.Key == "CustomHandler").Value.Value) as string;
5152

5253
var assignableToTypeParametersCount = assignableTo?.TypeParameters.Length ?? 0;
5354

0 commit comments

Comments
 (0)