Skip to content

Commit

Permalink
Use PerAssembly AddHandlers/AddBehaviors methods (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin authored Nov 13, 2024
1 parent 564ca4e commit d6f20fa
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 33 deletions.
4 changes: 2 additions & 2 deletions benchmarks/Benchmark.Behaviors/Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ public void Setup()

_ = services.AddScoped<SomeService>();

_ = services.AddHandlers();
_ = services.AddBehaviors();
_ = services.AddBenchmarkBehaviorsHandlers();
_ = services.AddBenchmarkBehaviorsBehaviors();

_ = services.AddMediator(opts => opts.ServiceLifetime = ServiceLifetime.Scoped);
_ = services.AddScoped(
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/Benchmark.Large/Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void Setup()
{
var services = new ServiceCollection();

_ = services.AddHandlers();
_ = services.AddBenchmarkLargeHandlers();

_ = services.AddMediator(opts => opts.ServiceLifetime = ServiceLifetime.Scoped);
_ = services.AddMediatR(
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/Benchmark.Simple/Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void Setup()
{
var services = new ServiceCollection();

_ = services.AddHandlers();
_ = services.AddBenchmarkSimpleHandlers();

_ = services.AddMediator(opts => opts.ServiceLifetime = ServiceLifetime.Scoped);
_ = services.AddMediatR(
Expand Down
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,17 @@ Immediate.Handlers supports `Microsoft.Extensions.DependencyInjection.Abstractio

#### Registering Handlers

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

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

#### Registering Behaviors

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

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

Expand Down
4 changes: 2 additions & 2 deletions samples/Normal/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddBehaviors();
builder.Services.AddHandlers();
builder.Services.AddNormalBehaviors();
builder.Services.AddNormalHandlers();

var app = builder.Build();

Expand Down
21 changes: 17 additions & 4 deletions src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.Any(r => (r.Display ?? "")
.Contains("Microsoft.Extensions.DependencyInjection.Abstractions")));

var assemblyName = context.CompilationProvider
.Select((cp, _) => cp.AssemblyName!
.Replace(".", string.Empty)
.Replace(" ", string.Empty)
.Trim()
);

var @namespace = context
.AnalyzerConfigOptionsProvider
.Select((c, _) => c.GlobalOptions
Expand Down Expand Up @@ -59,16 +66,19 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.Collect()
.Combine(behaviors)
.Combine(@namespace
.Combine(hasMsDi));
.Combine(hasMsDi)
.Combine(assemblyName));

context.RegisterSourceOutput(
registrationNodes,
(spc, node) => RenderServiceCollectionExtension(
spc,
handlers: node.Left.Left,
behaviors: node.Left.Right,
hasDi: node.Right.Right,
@namespace: node.Right.Left)
hasDi: node.Right.Left.Right,
@namespace: node.Right.Left.Left,
assemblyName: node.Right.Right
)
);
}

Expand All @@ -77,7 +87,9 @@ private static void RenderServiceCollectionExtension(
ImmutableArray<(string? displayName, EquatableReadOnlyList<Behavior?>? behaviors)> handlers,
ImmutableArray<Behavior?> behaviors,
bool hasDi,
string? @namespace)
string? @namespace,
string assemblyName
)
{
var cancellationToken = context.CancellationToken;
cancellationToken.ThrowIfCancellationRequested();
Expand All @@ -101,6 +113,7 @@ private static void RenderServiceCollectionExtension(
var source = template.Render(new
{
Namespace = @namespace,
AssemblyName = assemblyName,
Handlers = handlers.Select(x => x.displayName),
Behaviors = behaviors
.Concat(handlers.SelectMany(h => h.behaviors ?? Enumerable.Empty<Behavior?>()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace {{ namespace }};
{{~ end ~}}
public static class HandlerServiceCollectionExtensions
{
public static IServiceCollection AddBehaviors(
public static IServiceCollection Add{{ assembly_name }}Behaviors(
this IServiceCollection services)
{
{{~ for b in behaviors ~}}
Expand All @@ -19,7 +19,7 @@ public static class HandlerServiceCollectionExtensions
return services;
}

public static IServiceCollection AddHandlers(
public static IServiceCollection Add{{ assembly_name }}Handlers(
this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
Expand Down
6 changes: 3 additions & 3 deletions tests/Immediate.Handlers.FunctionalTests/HandlerResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

namespace Immediate.Handlers.FunctionalTests;

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

serviceCollectionConfigurator?.Invoke(serviceCollection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

public static class HandlerServiceCollectionExtensions
{
public static IServiceCollection AddBehaviors(
public static IServiceCollection AddTestsBehaviors(
this IServiceCollection services)
{
services.TryAddTransient(typeof(global::ConstraintBehavior<,>));

return services;
}

public static IServiceCollection AddHandlers(
public static IServiceCollection AddTestsHandlers(
this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public static class HandlerServiceCollectionExtensions
{
public static IServiceCollection AddBehaviors(
public static IServiceCollection AddTestsBehaviors(
this IServiceCollection services)
{
services.TryAddTransient(typeof(global::Dummy.LoggingBehavior<,>));
Expand All @@ -18,7 +18,7 @@ public static IServiceCollection AddBehaviors(
return services;
}

public static IServiceCollection AddHandlers(
public static IServiceCollection AddTestsHandlers(
this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

public static class HandlerServiceCollectionExtensions
{
public static IServiceCollection AddBehaviors(
public static IServiceCollection AddTestsBehaviors(
this IServiceCollection services)
{
services.TryAddTransient(typeof(global::Dummy.LoggingBehavior<,>));

return services;
}

public static IServiceCollection AddHandlers(
public static IServiceCollection AddTestsHandlers(
this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

public static class HandlerServiceCollectionExtensions
{
public static IServiceCollection AddBehaviors(
public static IServiceCollection AddTestsBehaviors(
this IServiceCollection services)
{

return services;
}

public static IServiceCollection AddHandlers(
public static IServiceCollection AddTestsHandlers(
this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

public static class HandlerServiceCollectionExtensions
{
public static IServiceCollection AddBehaviors(
public static IServiceCollection AddTestsBehaviors(
this IServiceCollection services)
{

return services;
}

public static IServiceCollection AddHandlers(
public static IServiceCollection AddTestsHandlers(
this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

public static class HandlerServiceCollectionExtensions
{
public static IServiceCollection AddBehaviors(
public static IServiceCollection AddTestsBehaviors(
this IServiceCollection services)
{

return services;
}

public static IServiceCollection AddHandlers(
public static IServiceCollection AddTestsHandlers(
this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Scoped
)
Expand Down

0 comments on commit d6f20fa

Please sign in to comment.