ApiFirstMediatR.Generator 1.0.6-alpha-4

This is a prerelease version of ApiFirstMediatR.Generator.
There is a newer version of this package available.
See the version list below for details.
dotnet add package ApiFirstMediatR.Generator --version 1.0.6-alpha-4                
NuGet\Install-Package ApiFirstMediatR.Generator -Version 1.0.6-alpha-4                
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.6-alpha-4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ApiFirstMediatR.Generator --version 1.0.6-alpha-4                
#r "nuget: ApiFirstMediatR.Generator, 1.0.6-alpha-4"                
#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.6-alpha-4&prerelease

// Install ApiFirstMediatR.Generator as a Cake Tool
#tool nuget:?package=ApiFirstMediatR.Generator&version=1.0.6-alpha-4&prerelease                

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 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
{
    [Route("[controller]")]
    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.

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.

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