Skip to content

Commit 8ad3462

Browse files
committed
Refactor to MapMediatorEndpoints
1 parent 71f4ea3 commit 8ad3462

File tree

24 files changed

+356
-284
lines changed

24 files changed

+356
-284
lines changed

AGENTS.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ using Foundatio.Mediator;
260260
DisableAuthorization = false, // Disable inline auth checks and auth service DI
261261
HandlerDiscovery = HandlerDiscovery.All, // All (convention + explicit) or Explicit only
262262
NotificationPublishStrategy = NotificationPublishStrategy.ForeachAwait, // Sequential, TaskWhenAll, or FireAndForget
263-
ProjectName = "MyProject", // Custom suffix for generated endpoint methods
264263
EnableGenerationCounter = false, // Debug: add generation timestamp comment
265264
EndpointDiscovery = EndpointDiscovery.All, // All (default), Explicit, None
266265
EndpointRoutePrefix = "api", // Global route prefix for all endpoints (default: "api")

docs/guide/clean-architecture.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ The source generator automatically creates:
105105
**No controller classes. No endpoint registrations. No boilerplate.** Just map them in your startup:
106106

107107
```csharp
108-
app.MapOrdersEndpoints();
108+
app.MapMediatorEndpoints();
109109
```
110110

111111
The generator infers HTTP methods from message names (`Create*` → POST, `Get*` → GET), generates routes from ID properties, maps `Result<T>` to HTTP status codes, and pulls OpenAPI metadata from XML doc comments.

docs/guide/configuration.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,6 @@ public class FirstTransientMiddleware
154154
- `TaskWhenAll`: Invokes all handlers concurrently and waits for all to complete
155155
- `FireAndForget`: Fires all handlers in parallel without waiting
156156

157-
**`ProjectName`** (`string?`)
158-
159-
- **Default:** Assembly name (with dots/dashes replaced by underscores)
160-
- **Effect:** Controls the suffix used in generated endpoint extension methods and classes
161-
- **Example:** Setting `ProjectName = "Products"` generates:
162-
- `MapProductsEndpoints()` extension method
163-
- `MediatorEndpointExtensions_Products` class
164-
- `MediatorEndpointResultMapper_Products` class
165-
- **Use Case:** Meaningful endpoint method names in modular monolith architectures
166-
- **See:** [Endpoints Guide](/guide/endpoints) for full documentation
167-
168157
**`EnableGenerationCounter`** (`bool`)
169158

170159
- **Default:** `false`

docs/guide/endpoints.md

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,28 @@ public class ProductHandler
4545

4646
### 3. Map the Endpoints
4747

48-
In your startup code, call the generated extension method:
48+
In your startup code, call the generated extension method to map all handler endpoints:
4949

5050
```csharp
5151
var app = builder.Build();
5252

53-
// Map endpoints from all modules
54-
app.MapProductsEndpoints(); // From Products.Module
55-
app.MapOrdersEndpoints(); // From Orders.Module
53+
// Maps endpoints from all referenced assemblies automatically
54+
app.MapMediatorEndpoints();
5655

5756
app.Run();
5857
```
5958

59+
You can also select specific assemblies and enable endpoint logging:
60+
61+
```csharp
62+
app.MapMediatorEndpoints(c =>
63+
{
64+
c.AddAssembly<CreateProduct>(); // Products.Module
65+
c.AddAssembly<CreateOrder>(); // Orders.Module
66+
c.LogEndpoints();
67+
});
68+
```
69+
6070
## HTTP Method Inference
6171

6272
The HTTP method is inferred from the message type name:
@@ -571,23 +581,6 @@ No endpoints are generated:
571581
[assembly: MediatorConfiguration(EndpointDiscovery = EndpointDiscovery.None)]
572582
```
573583

574-
## Project Name Configuration
575-
576-
Control the generated extension method name with the `ProjectName` property on `MediatorConfiguration`:
577-
578-
```csharp
579-
[assembly: MediatorConfiguration(
580-
ProjectName = "Products",
581-
EndpointDiscovery = EndpointDiscovery.All
582-
)]
583-
```
584-
585-
This generates:
586-
- `MapProductsEndpoints()` extension method
587-
- `MediatorEndpointExtensions_Products` class
588-
- `MediatorEndpointResultMapper_Products` class
589-
590-
Without this setting, the assembly name is used (with dots/dashes converted to underscores).
591584

592585
## Generated Code Example
593586

@@ -623,9 +616,14 @@ public class ProductHandler
623616
The generator produces:
624617

625618
```csharp
626-
public static class MediatorEndpointExtensions_Products
619+
public static partial class Products_Module_MediatorEndpoints
627620
{
628-
public static IEndpointRouteBuilder MapProductsEndpoints(this IEndpointRouteBuilder endpoints)
621+
public static void MapEndpoints(IEndpointRouteBuilder endpoints, bool logEndpoints = false)
622+
{
623+
MapEndpointsCore(endpoints, logEndpoints);
624+
}
625+
626+
static partial void MapEndpointsCore(IEndpointRouteBuilder endpoints, bool logEndpoints)
629627
{
630628
var rootGroup = endpoints.MapGroup("api");
631629
var productsGroup = rootGroup.MapGroup("products").WithTags("Products");
@@ -635,7 +633,7 @@ public static class MediatorEndpointExtensions_Products
635633
IMediator mediator, CancellationToken ct) =>
636634
{
637635
var result = await ProductHandler_CreateProduct_Handler.HandleAsync(mediator, message, ct);
638-
return MediatorEndpointResultMapper_Products.ToHttpResult(result);
636+
return MediatorEndpointResultMapper_Products_Module.ToHttpResult(result);
639637
})
640638
.WithName("CreateProduct")
641639
.WithSummary("Creates a new product")
@@ -647,13 +645,11 @@ public static class MediatorEndpointExtensions_Products
647645
{
648646
var message = new GetProduct(productId);
649647
var result = ProductHandler_GetProduct_Handler.Handle(mediator, message, ct);
650-
return MediatorEndpointResultMapper_Products.ToHttpResult(result);
648+
return MediatorEndpointResultMapper_Products_Module.ToHttpResult(result);
651649
})
652650
.WithName("GetProduct")
653651
.WithSummary("Gets a product by ID")
654652
.Produces<Product>(200);
655-
656-
return endpoints;
657653
}
658654
}
659655
```

docs/guide/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public class TodoHandler
9595

9696
```csharp
9797
// Program.cs
98-
app.MapMyAppEndpoints(); // generated extension method based on project name
98+
app.MapMediatorEndpoints();
9999
```
100100

101101
This generates:

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,6 @@ var reply = await mediator.InvokeAsync<string>(new Ping("Hello"));
9292
Turn your message handlers into API endpoints automatically:
9393

9494
```csharp
95-
app.MapMyAppEndpoints();
95+
app.MapMediatorEndpoints();
9696
// That's it — routes, methods, and parameter binding are all generated for you.
9797
```

samples/CleanArchitectureSample/src/Api/AssemblyAttributes.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
[assembly: MediatorConfiguration(
44
EnableGenerationCounter = true,
5-
EndpointDiscovery = EndpointDiscovery.All,
65
AuthorizationRequired = true,
7-
MiddlewareLifetime = MediatorLifetime.Singleton,
8-
ProjectName = "Api"
6+
MiddlewareLifetime = MediatorLifetime.Singleton
97
)]

samples/CleanArchitectureSample/src/Api/Program.cs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
using System.Security.Claims;
22
using Common.Module;
3-
using Common.Module.Events;
43
using Foundatio.Mediator;
54
using Microsoft.AspNetCore.Authentication;
65
using Microsoft.AspNetCore.Authentication.Cookies;
76
using Orders.Module;
8-
using Orders.Module.Messages;
97
using Products.Module;
10-
using Products.Module.Messages;
118
using Reports.Module;
12-
using Reports.Module.Messages;
139
using Scalar.AspNetCore;
14-
using Api.Handlers;
1510

1611
var builder = WebApplication.CreateBuilder(args);
1712

@@ -41,16 +36,8 @@
4136
});
4237
builder.Services.AddAuthorization();
4338

44-
// Add Foundatio.Mediator with assemblies from all modules
45-
builder.Services.AddMediator(c =>
46-
{
47-
c.SetMediatorLifetime(ServiceLifetime.Scoped);
48-
c.AddAssembly<OrderCreated>(); // Common.Module
49-
c.AddAssembly<CreateOrder>(); // Orders.Module
50-
c.AddAssembly<CreateProduct>(); // Products.Module
51-
c.AddAssembly<GetDashboardReport>(); // Reports.Module
52-
c.AddAssembly<ClientEventStreamHandler>(); // Api (for SSE streaming handler)
53-
});
39+
// Add Foundatio.Mediator — all referenced module assemblies are auto-discovered
40+
builder.Services.AddMediator();
5441

5542
// Add module services
5643
// Order matters: Common.Module provides cross-cutting services that other modules may depend on
@@ -123,14 +110,10 @@
123110
return Results.Ok(new UserInfo(displayName, username, role));
124111
}).AllowAnonymous();
125112

126-
// Map module endpoints - each module exposes its own API endpoints
113+
// Map module endpoints - discovers and maps all endpoint modules from referenced assemblies
127114
// All generated endpoints now require authentication via [assembly: MediatorConfiguration(AuthorizationRequired = true)]
128115
// Handlers marked with [AllowAnonymous] (e.g., HealthHandler) opt out of auth
129-
app.MapCommonEndpoints();
130-
app.MapOrdersEndpoints();
131-
app.MapProductsEndpoints();
132-
app.MapReportsEndpoints();
133-
app.MapApiEndpoints();
116+
app.MapMediatorEndpoints();
134117

135118
// SPA fallback - serves index.html for client-side routing
136119
app.MapFallbackToFile("/index.html");
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using Foundatio.Mediator;
22

33
[assembly: MediatorConfiguration(
4-
EndpointDiscovery = EndpointDiscovery.All,
54
AuthorizationRequired = true,
65
EnableGenerationCounter = true,
7-
MiddlewareLifetime = MediatorLifetime.Singleton,
8-
ProjectName = "Common"
6+
MiddlewareLifetime = MediatorLifetime.Singleton
97
)]
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using Foundatio.Mediator;
22

33
[assembly: MediatorConfiguration(
4-
EndpointDiscovery = EndpointDiscovery.All,
54
AuthorizationRequired = true,
6-
EnableGenerationCounter = true,
7-
ProjectName = "Orders"
5+
EnableGenerationCounter = true
86
)]

0 commit comments

Comments
 (0)