Skip to content

Commit

Permalink
mediator updates
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Jul 5, 2024
1 parent e0d3374 commit 6021138
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
3 changes: 2 additions & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ export default defineConfig({
{
label: 'Extensions',
items:[
{ label: 'Prism', link: 'client/mediator/extensions/prism' }
{ label: 'Prism', link: 'client/mediator/extensions/prism' },
{ label: 'ASP.NET Core', link: 'client/mediator/extensions/aspnet' }
]
},
{ label: 'Advanced', link: 'client/mediator/advanced' }
Expand Down
2 changes: 1 addition & 1 deletion src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const ShinyComponents: ShinyComponent[] = [
"id": "mediator",
"nuget": "Shiny.Mediator.Maui",
"description": "Mediator",
"version" : "1.0.0"
"version" : "1.5.0"
},
{
"id": "beacons",
Expand Down
88 changes: 88 additions & 0 deletions src/content/docs/client/mediator/extensions/aspnet.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
title: ASP.NET Core (Handler to Endpoint)
---
import { Steps } from '@astrojs/starlight/components';
import NugetBadge from '../../../../../components/NugetBadge.tsx';

<NugetBadge name="Shiny.Mediator.AspNet" label="Shiny.Mediator.AspNet" />

Shiny Mediator now can map up your request handlers to ASP.NET Core endpoints using the minimal API. This
saves you the effort of implementing the following boilerplate over and over and over again

```csharp
app.MapPost("/api/MyResultHandler", async (IMediator mediator, MyRequest request, CancellationToken ct) =>
{
return await mediator.Request(request, ct);
});
```

The only thing that changes in this setup is the http method type, request type, and route. Now, all you have to do is
following;

<Steps>
1. Install Shiny.Mediator.AspNet <NugetBadge name="Shiny.Mediator.AspNet" label="Shiny.Mediator.AspNet" /> to your ASP.NET Core project.

2. On your request handler (void or result based), add `[MediatorHttpPost("MyRoute")]` or `[MediatorHttpPut("MyRoute")]` attribute to the handler.

```csharp
[MediatorHttpPost("MyRoute")]
public class MyHandler : IRequestHandler<MyRequest, MyResult>
{
public async Task<MyResult> Handle(MyRequest request, CancellationToken ct)
{
return new MyResult();
}
}
```

:::note
Currently - we only support `HttpPost` and `HttpPut` attributes. We will look into Get/Delete in the future
:::

3. In your host startup, register your handler (or use the source generator attributes).

```csharp
services.AddSingletonAsImplementedInterfaces<MyHandler>();
```

4. Again, in your host startup, add the following after your build your app. Here is a full-ish sample

```csharp
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddShinyMediator();
services.AddSingletonAsImplementedInterfaces<MyHandler>();
var app = builder.Build();

app.UseShinyMediatorEndpointHandlers(builder.Services);
app.Run();
```

</Steps>

Here is a list of all the properties supported by the Http attributes

```csharp
public class MediatorHttpAttribute(string uriTemplate, HttpMethod httpMethod) : Attribute
{
public string UriTemplate => uriTemplate;
public HttpMethod Method => httpMethod;

public bool RequiresAuthorization { get; set; }
public string[]? AuthorizationPolicies { get; set; }
public string? DisplayName { get; set; }
public string? GroupName { get; set; }
public string[]? Tags { get; set; }
public string? Description { get; set; }
public string? Summary { get; set; }
public bool UseOpenApi { get; set; } = true;
public string? CachePolicy { get; set; }
public string? CorsPolicy { get; set; }
public bool ExcludeFromDescription { get; set; }
public string? RateLimitingPolicy { get; set; }
public bool AllowAnonymous { get; set; }
}
```

:::warning
It is best to register most handlers and middleware as `Scoped` on ASP.NET Core apps
:::
3 changes: 3 additions & 0 deletions src/content/docs/release-notes/mediator/v10.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ sidebar:{ hasSidebar: false }
import Aside from '../../../../components/Alert.tsx';
import RN from '../../../../components/ReleaseNote.astro';

# 1.5.0 - July 5
<RN type="feature">Shiny.Mediator.AspNet - that allows you to map your request handlers directly to HTTP endpoints</RN>

# 1.4.5 - July 3
<RN type="feature">Strongly typed navigation now exists for Shell, just like Prism. This functionality is built into Shiny.Mediator.Maui</RN>

Expand Down

0 comments on commit 6021138

Please sign in to comment.