ApiFirstMediatR.Generator
1.0.1
See the version list below for details.
dotnet add package ApiFirstMediatR.Generator --version 1.0.1
NuGet\Install-Package ApiFirstMediatR.Generator -Version 1.0.1
<PackageReference Include="ApiFirstMediatR.Generator" Version="1.0.1"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add ApiFirstMediatR.Generator --version 1.0.1
#r "nuget: ApiFirstMediatR.Generator, 1.0.1"
// Install ApiFirstMediatR.Generator as a Cake Addin #addin nuget:?package=ApiFirstMediatR.Generator&version=1.0.1 // Install ApiFirstMediatR.Generator as a Cake Tool #tool nuget:?package=ApiFirstMediatR.Generator&version=1.0.1
ApiFirstMediatR
=======
Generates Controllers, DTOs and MediatR Requests from a given OpenAPI Spec file to support API First development. Business logic implementation is handled by MediatR handlers that implement the generated MediatR Requests.
Code is generated using a Roslyn based Source Generator. To find out more about Roslyn Source Generators go here: https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview
Currently supports ASP.NET Core 6.0 and OpenAPI Spec version 3 and 2 in both yaml and json formats.
Installation
dotnet add package ApiFirstMediatR.Generator
Register your OpenAPI spec file by adding the following to your .csproj
:
<ItemGroup>
<AdditionalFiles Include="api_spec.json" />
</ItemGroup>
Hello World Example
Hello World OpenAPI spec:
openapi: 3.0.1
info:
title: HelloWorld API
version: v1
paths:
/api/HelloWorld:
get:
tags:
- HelloWorld
operationId: GetHelloWorld
parameters: []
responses:
200:
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/HelloWorldDto'
components:
schemas:
HelloWorldDto:
type: object
properties:
message:
type: string
nullable: true
security: []
The source generators will then generate the following based on he given OpenAPI spec file:
// <auto-generated/>
namespace HelloWorld.Dtos
{
public class HelloWorldDto
{
public string? Message { get; set; } // TODO: Set the JsonProperty Attributes
}
}
// <auto-generated/>
#nullable enable
using MediatR;
using HelloWorld.Dtos;
namespace HelloWorld.Requests
{
public sealed class GetHelloWorldQuery : IRequest<HelloWorldDto>
{
}
}
// <auto-generated/>
using MediatR;
using Microsoft.AspNetCore.Mvc;
using HelloWorld.Dtos;
using HelloWorld.Requests;
namespace HelloWorld.Controllers
{
[Route(""[controller]"")]
public sealed class ApiController : Controller
{
private readonly IMediator _mediator;
public ApiController(IMediator mediator)
{
_mediator = mediator;
}
[HttpGet(""/api/HelloWorld"")]
public async Task<ActionResult> GetHelloWorld()
{
var request = new GetHelloWorldQuery
{
};
var response = await _mediator.Send(request); // TODO: optionally use a CancellationToken
return Ok(response);
}
}
}
To implement the Hello World endpoint create the following handler:
using HelloWorld.Dtos;
using HelloWorld.Requests;
using MediatR;
namespace HelloWorld.Handlers
{
public sealed class GetHelloWorldQueryHandler : IRequestHandler<GetHelloWorldQuery, HelloWorldDto>
{
public Task<HelloWorldDto> Handle(GetHelloWorldQuery query, CancellationToken cancellationToken)
{
// Endpoint implementation goes here
}
}
}
Viewing the generated files
Visual Studio
In the solution explorer expand Project → Dependencies → Analyzers → ApiFirstMediatR.Generator → ApiFirstMediatR.Generator.ApiSourceGenerator.
Rider
In the explorer expand Project → Dependencies → .NET 6.0 → Source Generators → ApiFirstMediatR.Generator.ApiSourceGenerator
VSCode
Add the following to your .csproj
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
</ItemGroup>
<Target Name="CleanSourceGeneratedFiles" BeforeTargets="BeforeRebuild" DependsOnTargets="$(BeforeBuildDependsOn)">
<RemoveDir Directories="$(CompilerGeneratedFilesOutputPath)" />
</Target>
This will force the generators to output the generated files to the Generated
directory, but notify the compiler to ignore the generated files and continue to use the normal Roslyn Source Generator compile process. Adding the Generated directory to your .gitignore
is recommended.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.3.1)
- Microsoft.CSharp (>= 4.5.0)
- System.Threading.Tasks.Extensions (>= 4.5.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.1.0 | 83 | 10/22/2024 |
1.0.11 | 6,054 | 3/12/2023 |
1.0.10 | 2,434 | 3/4/2023 |
1.0.9 | 255 | 2/28/2023 |
1.0.8 | 543 | 12/15/2022 |
1.0.8-alpha-1 | 150 | 12/15/2022 |
1.0.7 | 291 | 12/15/2022 |
1.0.7-alpha-2 | 135 | 12/15/2022 |
1.0.7-alpha-1 | 146 | 12/15/2022 |
1.0.6 | 345 | 12/1/2022 |
1.0.6-alpha-4 | 145 | 12/1/2022 |
1.0.6-alpha-3 | 143 | 12/1/2022 |
1.0.6-alpha-2 | 151 | 12/1/2022 |
1.0.6-alpha-1 | 139 | 11/30/2022 |
1.0.5 | 339 | 11/29/2022 |
1.0.4 | 369 | 11/11/2022 |
1.0.3 | 343 | 11/7/2022 |
1.0.2 | 387 | 11/5/2022 |
1.0.1 | 371 | 11/4/2022 |
1.0.1-alpha-1 | 150 | 11/4/2022 |
1.0.0 | 372 | 11/4/2022 |