Skip to content

Commit d6f20fa

Browse files
Use PerAssembly AddHandlers/AddBehaviors methods (#152)
1 parent 564ca4e commit d6f20fa

14 files changed

+46
-33
lines changed

benchmarks/Benchmark.Behaviors/Benchmark.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ public void Setup()
149149

150150
_ = services.AddScoped<SomeService>();
151151

152-
_ = services.AddHandlers();
153-
_ = services.AddBehaviors();
152+
_ = services.AddBenchmarkBehaviorsHandlers();
153+
_ = services.AddBenchmarkBehaviorsBehaviors();
154154

155155
_ = services.AddMediator(opts => opts.ServiceLifetime = ServiceLifetime.Scoped);
156156
_ = services.AddScoped(

benchmarks/Benchmark.Large/Benchmark.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void Setup()
7171
{
7272
var services = new ServiceCollection();
7373

74-
_ = services.AddHandlers();
74+
_ = services.AddBenchmarkLargeHandlers();
7575

7676
_ = services.AddMediator(opts => opts.ServiceLifetime = ServiceLifetime.Scoped);
7777
_ = services.AddMediatR(

benchmarks/Benchmark.Simple/Benchmark.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void Setup()
7171
{
7272
var services = new ServiceCollection();
7373

74-
_ = services.AddHandlers();
74+
_ = services.AddBenchmarkSimpleHandlers();
7575

7676
_ = services.AddMediator(opts => opts.ServiceLifetime = ServiceLifetime.Scoped);
7777
_ = services.AddMediatR(

readme.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,17 @@ Immediate.Handlers supports `Microsoft.Extensions.DependencyInjection.Abstractio
158158

159159
#### Registering Handlers
160160

161-
```cs
162-
services.AddHandlers();
163-
```
161+
In your `Program.cs`, add a call to `app.MapXxxHandlers()`, where `Xxx` is the shortened form of the project name.
162+
* For a project named `Web`, it will be `app.MapWebHandlers()`
163+
* For a project named `Application.Web`, it will be `app.MapApplicationWebHandlers()`
164164

165165
This registers all classes in the assembly marked with `[Handler]`.
166166

167167
#### Registering Behaviors
168168

169-
```cs
170-
services.AddBehaviors();
171-
```
169+
In your `Program.cs`, add a call to `app.MapXxxBehaviors()`, where `Xxx` is the shortened form of the project name.
170+
* For a project named `Web`, it will be `app.MapWebBehaviors()`
171+
* For a project named `Application.Web`, it will be `app.MapApplicationWebBehaviors()`
172172

173173
This registers all behaviors referenced in any `[Behaviors]` attribute.
174174

samples/Normal/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
builder.Services.AddEndpointsApiExplorer();
66
builder.Services.AddSwaggerGen();
7-
builder.Services.AddBehaviors();
8-
builder.Services.AddHandlers();
7+
builder.Services.AddNormalBehaviors();
8+
builder.Services.AddNormalHandlers();
99

1010
var app = builder.Build();
1111

src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.cs

+17-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1818
.Any(r => (r.Display ?? "")
1919
.Contains("Microsoft.Extensions.DependencyInjection.Abstractions")));
2020

21+
var assemblyName = context.CompilationProvider
22+
.Select((cp, _) => cp.AssemblyName!
23+
.Replace(".", string.Empty)
24+
.Replace(" ", string.Empty)
25+
.Trim()
26+
);
27+
2128
var @namespace = context
2229
.AnalyzerConfigOptionsProvider
2330
.Select((c, _) => c.GlobalOptions
@@ -59,16 +66,19 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
5966
.Collect()
6067
.Combine(behaviors)
6168
.Combine(@namespace
62-
.Combine(hasMsDi));
69+
.Combine(hasMsDi)
70+
.Combine(assemblyName));
6371

6472
context.RegisterSourceOutput(
6573
registrationNodes,
6674
(spc, node) => RenderServiceCollectionExtension(
6775
spc,
6876
handlers: node.Left.Left,
6977
behaviors: node.Left.Right,
70-
hasDi: node.Right.Right,
71-
@namespace: node.Right.Left)
78+
hasDi: node.Right.Left.Right,
79+
@namespace: node.Right.Left.Left,
80+
assemblyName: node.Right.Right
81+
)
7282
);
7383
}
7484

@@ -77,7 +87,9 @@ private static void RenderServiceCollectionExtension(
7787
ImmutableArray<(string? displayName, EquatableReadOnlyList<Behavior?>? behaviors)> handlers,
7888
ImmutableArray<Behavior?> behaviors,
7989
bool hasDi,
80-
string? @namespace)
90+
string? @namespace,
91+
string assemblyName
92+
)
8193
{
8294
var cancellationToken = context.CancellationToken;
8395
cancellationToken.ThrowIfCancellationRequested();
@@ -101,6 +113,7 @@ private static void RenderServiceCollectionExtension(
101113
var source = template.Render(new
102114
{
103115
Namespace = @namespace,
116+
AssemblyName = assemblyName,
104117
Handlers = handlers.Select(x => x.displayName),
105118
Behaviors = behaviors
106119
.Concat(handlers.SelectMany(h => h.behaviors ?? Enumerable.Empty<Behavior?>()))

src/Immediate.Handlers.Generators/Templates/ServiceCollectionExtensions.sbntxt

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace {{ namespace }};
99
{{~ end ~}}
1010
public static class HandlerServiceCollectionExtensions
1111
{
12-
public static IServiceCollection AddBehaviors(
12+
public static IServiceCollection Add{{ assembly_name }}Behaviors(
1313
this IServiceCollection services)
1414
{
1515
{{~ for b in behaviors ~}}
@@ -19,7 +19,7 @@ public static class HandlerServiceCollectionExtensions
1919
return services;
2020
}
2121

22-
public static IServiceCollection AddHandlers(
22+
public static IServiceCollection Add{{ assembly_name }}Handlers(
2323
this IServiceCollection services,
2424
ServiceLifetime lifetime = ServiceLifetime.Scoped
2525
)

tests/Immediate.Handlers.FunctionalTests/HandlerResolver.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.Extensions.DependencyInjection;
1+
using Microsoft.Extensions.DependencyInjection;
22

33
namespace Immediate.Handlers.FunctionalTests;
44

@@ -8,8 +8,8 @@ public static T Resolve<T>(Action<IServiceCollection>? serviceCollectionConfigur
88
where T : notnull
99
{
1010
var serviceCollection = new ServiceCollection()
11-
.AddHandlers()
12-
.AddBehaviors();
11+
.AddImmediateHandlersFunctionalTestsHandlers()
12+
.AddImmediateHandlersFunctionalTestsBehaviors();
1313

1414
serviceCollectionConfigurator?.Invoke(serviceCollection);
1515

tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.CrtpBehavior_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
public static class HandlerServiceCollectionExtensions
88
{
9-
public static IServiceCollection AddBehaviors(
9+
public static IServiceCollection AddTestsBehaviors(
1010
this IServiceCollection services)
1111
{
1212
services.TryAddTransient(typeof(global::ConstraintBehavior<,>));
1313

1414
return services;
1515
}
1616

17-
public static IServiceCollection AddHandlers(
17+
public static IServiceCollection AddTestsHandlers(
1818
this IServiceCollection services,
1919
ServiceLifetime lifetime = ServiceLifetime.Scoped
2020
)

tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.MultipleBehaviors_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public static class HandlerServiceCollectionExtensions
88
{
9-
public static IServiceCollection AddBehaviors(
9+
public static IServiceCollection AddTestsBehaviors(
1010
this IServiceCollection services)
1111
{
1212
services.TryAddTransient(typeof(global::Dummy.LoggingBehavior<,>));
@@ -18,7 +18,7 @@ public static IServiceCollection AddBehaviors(
1818
return services;
1919
}
2020

21-
public static IServiceCollection AddHandlers(
21+
public static IServiceCollection AddTestsHandlers(
2222
this IServiceCollection services,
2323
ServiceLifetime lifetime = ServiceLifetime.Scoped
2424
)

tests/Immediate.Handlers.Tests/GeneratorTests/BehaviorTests.SingleBehavior_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
public static class HandlerServiceCollectionExtensions
88
{
9-
public static IServiceCollection AddBehaviors(
9+
public static IServiceCollection AddTestsBehaviors(
1010
this IServiceCollection services)
1111
{
1212
services.TryAddTransient(typeof(global::Dummy.LoggingBehavior<,>));
1313

1414
return services;
1515
}
1616

17-
public static IServiceCollection AddHandlers(
17+
public static IServiceCollection AddTestsHandlers(
1818
this IServiceCollection services,
1919
ServiceLifetime lifetime = ServiceLifetime.Scoped
2020
)

tests/Immediate.Handlers.Tests/GeneratorTests/HandlerTests.ComplexParameterAttribute_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
public static class HandlerServiceCollectionExtensions
88
{
9-
public static IServiceCollection AddBehaviors(
9+
public static IServiceCollection AddTestsBehaviors(
1010
this IServiceCollection services)
1111
{
1212

1313
return services;
1414
}
1515

16-
public static IServiceCollection AddHandlers(
16+
public static IServiceCollection AddTestsHandlers(
1717
this IServiceCollection services,
1818
ServiceLifetime lifetime = ServiceLifetime.Scoped
1919
)

tests/Immediate.Handlers.Tests/GeneratorTests/HandlerTests.MultipleParameterAttributes_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
public static class HandlerServiceCollectionExtensions
88
{
9-
public static IServiceCollection AddBehaviors(
9+
public static IServiceCollection AddTestsBehaviors(
1010
this IServiceCollection services)
1111
{
1212

1313
return services;
1414
}
1515

16-
public static IServiceCollection AddHandlers(
16+
public static IServiceCollection AddTestsHandlers(
1717
this IServiceCollection services,
1818
ServiceLifetime lifetime = ServiceLifetime.Scoped
1919
)

tests/Immediate.Handlers.Tests/GeneratorTests/HandlerTests.SimpleParameterAttribute_assemblies=Msdi#IH.ServiceCollectionExtensions.g.verified.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
public static class HandlerServiceCollectionExtensions
88
{
9-
public static IServiceCollection AddBehaviors(
9+
public static IServiceCollection AddTestsBehaviors(
1010
this IServiceCollection services)
1111
{
1212

1313
return services;
1414
}
1515

16-
public static IServiceCollection AddHandlers(
16+
public static IServiceCollection AddTestsHandlers(
1717
this IServiceCollection services,
1818
ServiceLifetime lifetime = ServiceLifetime.Scoped
1919
)

0 commit comments

Comments
 (0)