ApiFirstMediatR.Generator 1.0.11

dotnet add package ApiFirstMediatR.Generator --version 1.0.11
NuGet\Install-Package ApiFirstMediatR.Generator -Version 1.0.11
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ApiFirstMediatR.Generator" Version="1.0.11" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ApiFirstMediatR.Generator --version 1.0.11
#r "nuget: ApiFirstMediatR.Generator, 1.0.11"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ApiFirstMediatR.Generator as a Cake Addin
#addin nuget:?package=ApiFirstMediatR.Generator&version=1.0.11

// Install ApiFirstMediatR.Generator as a Cake Tool
#tool nuget:?package=ApiFirstMediatR.Generator&version=1.0.11

ApiFirstMediatR

CI NuGet

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 7.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
      description: Gets a HelloWorld Message
      parameters: []
      responses:
        200:
          description: Hello world!
          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/>
using System.Text.Json.Serialization;

namespace compilation.Dtos
{
    public class HelloWorldDto 
    {
        [JsonPropertyName("message")]
        public string? Message { get; set; }
    }
}
// <auto-generated/>
#nullable enable
using MediatR;
using compilation.Dtos;

namespace compilation.Requests
{
    /// <summary>
    /// Gets a HelloWorld Message
    /// </summary>
    /// <returns>Hello world!</returns>
    public sealed class GetHelloWorldQuery : IRequest<HelloWorldDto>
    {
        public GetHelloWorldQuery()
        {
        }
    }
}
// <auto-generated/>
#nullable enable
using MediatR;
using Microsoft.AspNetCore.Mvc;
using compilation.Dtos;
using compilation.Requests;

namespace compilation.Controllers
{
    public sealed class ApiController : Controller
    {
        private readonly IMediator _mediator;

        public ApiController(IMediator mediator)
        {
            _mediator = mediator;
        }
        
        /// <summary>
        /// Gets a HelloWorld Message
        /// </summary>
        /// <returns>Hello world!</returns>
        [HttpGet("/api/HelloWorld")]
        public async Task<ActionResult> GetHelloWorld(CancellationToken cancellationToken)
        {
            var request = new GetHelloWorldQuery();
            var response = await _mediator.Send(request, 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.

Configuration

MSBuild Options

Available MSBuild properties:

Property Default Available Options Description
ApiFirstMediatR_SerializationLibrary System.Text.Json System.Text.Json, Newtonsoft.Json Serialization Library that the generated code should use.
ApiFirstMediatR_RequestBodyName Body string The name that's used for request bodies in mediatr requests.

To set an MSBuild option you need to add the property and value to your csproj file.

Example:

    <PropertyGroup>
        <ApiFirstMediatR_SerializationLibrary>Newtonsoft.Json</ApiFirstMediatR_SerializationLibrary>
    </PropertyGroup>
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

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.0.11 4,368 3/12/2023
1.0.10 1,726 3/4/2023
1.0.9 224 2/28/2023
1.0.8 509 12/15/2022
1.0.8-alpha-1 123 12/15/2022
1.0.7 267 12/15/2022
1.0.7-alpha-2 106 12/15/2022
1.0.7-alpha-1 119 12/15/2022
1.0.6 311 12/1/2022
1.0.6-alpha-4 120 12/1/2022
1.0.6-alpha-3 112 12/1/2022
1.0.6-alpha-2 124 12/1/2022
1.0.6-alpha-1 108 11/30/2022
1.0.5 307 11/29/2022
1.0.4 335 11/11/2022
1.0.3 320 11/7/2022
1.0.2 353 11/5/2022
1.0.1 341 11/4/2022
1.0.1-alpha-1 127 11/4/2022
1.0.0 344 11/4/2022