CleanCodeJN.GenericApis 5.0.0

dotnet add package CleanCodeJN.GenericApis --version 5.0.0
                    
NuGet\Install-Package CleanCodeJN.GenericApis -Version 5.0.0
                    
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="CleanCodeJN.GenericApis" Version="5.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CleanCodeJN.GenericApis" Version="5.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CleanCodeJN.GenericApis" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CleanCodeJN.GenericApis --version 5.0.0
                    
#r "nuget: CleanCodeJN.GenericApis, 5.0.0"
                    
#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.
#:package CleanCodeJN.GenericApis@5.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CleanCodeJN.GenericApis&version=5.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CleanCodeJN.GenericApis&version=5.0.0
                    
Install as a Cake Tool

๐Ÿš€ Generic Web APIs โ€“ Fast, Clean, Powerful

Build production-ready APIs instantly โ€“ from Minimal APIs and Controllers to fully integrated GraphQL endpoints with CRUD, filtering, sorting & paging โ€“ powered by Mediator, AutoMapper, EF Core, FluentValidation, and the IOSP architecture pattern.

โšก Quickstart

dotnet add package CleanCodeJN.GenericApis
// Program.cs
builder.Services.AddCleanCodeJN<MyDbContext>(options => {...}); // Mandatory: Configure CleanCodeJN options
app.UseCleanCodeJNWithMinimalApis();                            // For REST APIs
app.UseCleanCodeJNWithGraphQL();                                // For autogenerated GraphQL APIs
app.UseCleanCodeJNDocumentation();                              // Optional: For autogenerated Command Documentation, visit /docs
                                                                // add <GenerateDocumentationFile>true</GenerateDocumentationFile> to your .csproj file
// Entity
public class Customer : IEntity<int> 
{ 
    public int Id { get; set; } 
    public string Name { get; set; }
}

// DTO
public class CustomerGetDto : IDto 
{ 
    public int Id { get; set; } 
    public string Name { get; set; }
}

// Minimal API
public class CustomersV1Api : IApi
{
    public List<string> Tags => ["Customers Minimal API"];

    public string Route => $"api/v1/Customers";

    public List<Func<WebApplication, RouteHandlerBuilder>> HttpMethods =>
    [
         app => app.MapGet<Customer, CustomerGetDto, int>(
                Route, 
                Tags, 
                where: x => x.Name.StartsWith("a"),
                select: x => new Customer { Name = x.Name }),
    ];
}

// IOSP Workflow
public class YourIntegrationCommand(ICommandExecutionContext executionContext)
    : IntegrationCommand<YourIntegrationRequest, Customer>(executionContext)
{
    public override async Task<BaseResponse<Customer>> Handle(YourIntegrationRequest request, CancellationToken cancellationToken) =>
        await ExecutionContext
            .DoSomethingRequest1()
            .DoSomethingRequest2()
            .DoSomethingRequest3()
            .Execute<Customer>(cancellationToken);
}

Table of Contents

This package gives you:

  • โœ… blazing-fast setup
  • โœ… CRUD APIs in seconds without writing a single line of code
  • โœ… GraphQL Support with automatic schema generation from your Entities and DTOs
  • โœ… complex business logic with IOSP
  • โœ… automatic command and workflow documentation by using your xml comments - visit /docs
  • โœ… testable architecture
  • โœ… maintainable and modular code

๐Ÿงช What is IOSP?

Integration Operation Segregation Principle
Split your request handlers into: Operations โ†’ contain real logic (DB, API, etc.)
Integrations โ†’ just orchestrate other request handlers

โœจ Features at a Glance

  • โšก Plug & Play CRUD APIs (Minimal API or Controller-based)
  • ๐Ÿงฌ Auto-generated GraphQL API with query/mutation/filter/sort/projection support via HotChocolate
  • ๐Ÿ“ฆ Built-in paging, filtering & projections
  • ๐Ÿง  Clean separation of logic using the Mediator pattern
  • ๐Ÿงฑ Entity Framework abstraction via DataRepositories
  • ๐Ÿ”€ Auto-mapping of Entities โ‡„ DTOs (no config needed)
  • ๐Ÿงช Fluent validation out of the box
  • ๐Ÿงผ CleanCode-first architecture using the IOSP Principle
  • ๐Ÿงช Easily mockable and testable
  • ๐Ÿš€ Runs on .NET 9

How to use

  • Add AddCleanCodeJN<IDataContext>() to your Program.cs
  • Add app.UseCleanCodeJNWithMinimalApis() to your Program.cs for minimal APIs or use AddControllers + MapControllers()
  • Add app.UseCleanCodeJNWithGraphQL() to your Program.cs for GraphQL support
  • Start writing Apis by implementing IApi
  • Extend standard CRUD operations by specific Where() and Include() clauses
  • Use IOSP for complex business logic

Step by step explanation

Add AddCleanCodeJN<IDataContext>() to your Program.cs

builder.Services.AddCleanCodeJN<MyDbContext>(options => {});
  • All Entity โ‡” DTO Mappings will be done automatically if the naming Convention will be applied: e.g.: Customer โ‡” CustomerGetDto.
  • DTO has to start with Entity-Name and must inherits from IDto
  • Entity must inherit from IEntity

These are the CleanCodeJN Options

/// <summary>
/// The options for the CleanCodeJN.GenericApis
/// </summary>
public class CleanCodeOptions
{
    /// <summary>
    /// The assemblies that contain the command types, Entity types and DTO types for automatic registration of commands, DTOs and entities.
    /// </summary>
    public List<Assembly> ApplicationAssemblies { get; set; } = [];

    /// <summary>
    /// The assembly that contains the validators types for using Fluent Validation.
    /// </summary>
    public Assembly ValidatorAssembly { get; set; }

    /// <summary>
    /// The assembly that contains the automapper mapping profiles.
    /// </summary>
    public Action<IMapperConfigurationExpression> MappingOverrides { get; set; }

    /// <summary>
    /// If true: Use distributed memory cache. If false: you can add another Distributed Cache implementation.
    /// </summary>
    public bool UseDistributedMemoryCache { get; set; } = true;

    /// <summary>
    /// If true: Add default logging behavior. If false: you can add another logging behavior.
    /// </summary>
    public bool AddDefaultLoggingBehavior { get; set; }

    /// <summary>
    /// Mediatr Types of Open Behaviors to register
    /// </summary>
    public List<Type> OpenBehaviors { get; set; } = [];

    /// <summary>
    /// Mediatr Types of Closed Behaviors to register
    /// </summary>
    public List<Type> ClosedBehaviors { get; set; } = [];
    
    /// <summary>
    /// Gets or sets a value indicating whether GraphQL auto-wiring is enabled.
    /// </summary>
    public GraphQLOptions GraphQLOptions { get; set; }
}

Add app.UseCleanCodeJNWithMinimalApis() when using Minimal APIs to your Program.cs

app.UseCleanCodeJNWithMinimalApis();

Add app.UseCleanCodeJNWithGraphQL() when using automatic GraphQL to your Program.cs

app.UseCleanCodeJNWithGraphQL();

Add app.UseCleanCodeJNDocumentation() when using automatic Command documentation (from your XML comments) to your Program.cs

app.UseCleanCodeJNDocumentation();  // add <GenerateDocumentationFile>true</GenerateDocumentationFile> to your .csproj file

When using Controllers add this to your Program.cs

builder.Services.AddControllers()
    .AddNewtonsoftJson(); // this is needed for "http patch" only. If you do not need to use patch, you can remove this line

// After Build()
app.MapControllers();

When using GraphQL add this to your Program.cs

builder.Services.AddCleanCodeJN<MyDbContext>(options =>
{
    options.ApplicationAssemblies =
    [
        typeof(CleanCodeJN.GenericApis.Sample.Business.AssemblyRegistration).Assembly,
        typeof(CleanCodeJN.GenericApis.Sample.Core.AssemblyRegistration).Assembly,
        typeof(CleanCodeJN.GenericApis.Sample.Domain.AssemblyRegistration).Assembly
    ];
    options.ValidatorAssembly = typeof(CleanCodeJN.GenericApis.Sample.Core.AssemblyRegistration).Assembly;

    // Enable GraphQL with all CRUD operations
    options.GraphQLOptions = new GraphQLOptions
    {
        Get = true,
        Create = true,
        Update = true,
        Delete = true,
        AddAuthorizationWithPolicyName = "MyPolicy", // optional for adding authorization policy
    };
});

// Optional: Add Authentication and Authorization if needed
builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("MyPolicy", policy =>
    {
        policy.RequireAuthenticatedUser();
        policy.RequireClaim("role", "admin");
    });
});

Start writing Minimal Apis by implementing IApi

public class CustomersV1Api : IApi
{
    public List<string> Tags => ["Customers Minimal API"];

    public string Route => $"api/v1/Customers";

    public List<Func<WebApplication, RouteHandlerBuilder>> HttpMethods =>
    [
        app => app.MapGet<Customer, CustomerGetDto, int>(
            Route,
            Tags,
            where: x => x.Name.StartsWith("Customer"),
            includes: [x => x.Invoices],
            select: x => new Customer { Name = x.Name },
            ignoreQueryFilters: true),

        app => app.MapGetPaged<Customer, CustomerGetDto, int>(Route, Tags),

        app => app.MapGetFiltered<Customer, CustomerGetDto, int>(Route, Tags),

        app => app.MapGetById<Customer, CustomerGetDto, int>(Route, Tags),

        app => app.MapPut<Customer, CustomerPutDto, CustomerGetDto>(Route, Tags),

        app => app.MapPost<Customer, CustomerPostDto, CustomerGetDto>(Route, Tags),

        app => app.MapPatch<Customer, CustomerGetDto, int>(Route, Tags),

        // Or use a custom Command with MapDeleteRequest()
        app => app.MapDeleteRequest<Customer, CustomerGetDto, int>(Route, Tags, id => new DeleteCustomerIntegrationRequest { Id = id })
    ];
}

Extend standard CRUD operations by specific Where(), Include() or Select() clauses

public class CustomersV1Api : IApi
{
    public List<string> Tags => ["Customers Minimal API"];

    public string Route => $"api/v1/Customers";

    public List<Func<WebApplication, RouteHandlerBuilder>> HttpMethods =>
    [
         app => app.MapGet<Customer, CustomerGetDto, int>(Route, Tags, where: x => x.Name.StartsWith("a"), select: x => new Customer { Name = x.Name }),
    ];
}

Use ApiCrudControllerBase for CRUD operations in controllers

[Tags("Customers Controller based")]
[Route($"api/v2/[controller]")]

public class CustomersController(IMediator commandBus, IMapper mapper)
    : ApiCrudControllerBase<Customer, CustomerGetDto, CustomerPostDto, CustomerPutDto, int>(commandBus, mapper)
{
}

You can also override your Where, Include or Select clauses

/// <summary>
/// Customers Controller based
/// </summary>
/// <param name="commandBus">IMediatr instance.</param>
/// <param name="mapper">Automapper instance.</param>
[Tags("Customers Controller based")]
[Route($"api/v2/[controller]")]
public class CustomersController(IMediator commandBus, IMapper mapper)
    : ApiCrudControllerBase<Customer, CustomerGetDto, CustomerPostDto, CustomerPutDto, int>(commandBus, mapper)
{
    /// <summary>
    /// Where clause for the Get method.
    /// </summary>
    public override Expression<Func<Customer, bool>> GetWhere => x => x.Name.StartsWith("Customer");

    /// <summary>
    /// Includes for the Get method.
    /// </summary>
    public override List<Expression<Func<Customer, object>>> GetIncludes => [x => x.Invoices];

    /// <summary>
    /// Select for the Get method.
    /// </summary>
    public override Expression<Func<Customer, Customer>> GetSelect => x => new Customer { Id = x.Id, Name = x.Name };

    /// <summary>
    /// AsNoTracking for the Get method.
    /// </summary>
    public override bool AsNoTracking => true;
}

For using the /filtered api with a filter, just provide a serialized json as filter parameter, like this:

{
    "Condition" : 0, // 0 = AND; 1 = OR
    "Filters": [
        {
            "Field": "Name",
            "Value": "aac",
            "Type": 0
        },
        {
            "Field": "Id",
            "Value": "3",
            "Type": 1
        }
    ]
}

Which means: Give me all Names which CONTAINS "aac" AND have Id EQUALS 3. So string Types use always CONTAINS and integer types use EQUALS. All filters are combined with ANDs.

The Type can be specified with these values

public enum FilterTypeEnum
{
    STRING = 0,
    INTEGER = 1,
    DOUBLE = 2,
    INTEGER_NULLABLE = 3,
    DOUBLE_NULLABLE = 4,
    DATETIME = 5,
    DATETIME_NULLABLE = 6,
    GUID = 7,
    GUID_NULLABLE = 8,
}

Advanced Topics

Built-in Support for Fluent Validation:

Just write your AbstractValidators<T>. They will be automatically executed on generic POST and generic PUT actions:

public class CustomerPostDtoValidator : AbstractValidator<CustomerPostDto>
{
    public CustomerPostDtoValidator()
    {
        RuleFor(x => x.Name)
            .NotEmpty()
            .MaximumLength(10);
    }
public class CustomerPutDtoValidator : AbstractValidator<CustomerPutDto>
{
    public CustomerPutDtoValidator()
    {
        RuleFor(x => x.Id)
            .GreaterThan(0);

        RuleFor(x => x.Name)
            .NotEmpty()
            .MaximumLength(10)
            .CreditCard();
    }
}

Implement your own specific Request:

public class SpecificDeleteRequest : IRequest<BaseResponse<Customer>>
{
    public required int Id { get; init; }
}

Requests can also be marked as ICachableRequest, which uses IDistributedCache to cache the Response:

public class SpecificDeleteRequest : IRequest<BaseResponse<Customer>>, ICachableRequest
{
    public required int Id { get; init; }

    public bool BypassCache { get; }

    public string CacheKey => "Your Key";

    public TimeSpan? CacheDuration => TimeSpan.FromHours(168);
}

With your own specific Command using CleanCodeJN.Repository

public class SpecificDeleteCommand(IRepository<Customer, int> repository) : IRequestHandler<SpecificDeleteRequest, BaseResponse<Customer>>
{
    public async Task<BaseResponse<Customer>> Handle(SpecificDeleteRequest request, CancellationToken cancellationToken)
    {
        var deletedCustomer = await repository.Delete(request.Id, cancellationToken);

        return await BaseResponse<Customer>.Create(deletedCustomer is not null, deletedCustomer);
    }
}

Custom Middlewares

CleanCodeJN.GenericApis is fully compatible with the standard ASP.NET Core middleware pipeline.
You can easily add custom middlewares for authentication, logging, exception handling, or any other cross-cutting concern โ€” before or after the CleanCodeJN setup.

Where to Add Middlewares

Custom middlewares should be registered in your Program.cs after the AddCleanCodeJN() call, but before the CleanCodeJN middlewares such as UseCleanCodeJNWith. Global mediator behaviours for logging or caching can directly be added in the AddCleanCodeJN() options.

There already is a default logging behaviour included, which can be enabled in the options. This behaviour logs the execution and exection time of each command.

Example Structure
var builder = WebApplication.CreateBuilder(args);

// Add CleanCodeJN
builder.Services.AddCleanCodeJN<MyDbContext>(options =>
{
    options.AddDefaultLoggingBehavior = true; // Enables default logging behaviour
    options.OpenBehaviors = [typeof(CustomBehavior<,>)]; // Adds custom behaviour with 2 generic parameters for TRequest, TResponse

    options.ApplicationAssemblies = [typeof(Program).Assembly];
    options.ValidatorAssembly = typeof(Program).Assembly;
});

// Add custom services
builder.Services.AddLogging();
builder.Services.AddAuthentication("Bearer")
    .AddJwtBearer("Bearer", options =>
    {
        options.Authority = "https://your-keycloak-domain/auth/realms/yourrealm";
        options.Audience = "your-api";
    });
builder.Services.AddAuthorization();

var app = builder.Build();

// Add your middlewares in the right order

// Authentication & Authorization
app.UseAuthentication();
app.UseAuthorization();

// Custom Logging Middleware
app.Use(async (context, next) =>
{
    var logger = context.RequestServices.GetRequiredService<ILogger<Program>>();
    logger.LogInformation("โžก๏ธ Request: {Method} {Path}", context.Request.Method, context.Request.Path);
    await next();
    logger.LogInformation("โฌ…๏ธ Response: {StatusCode}", context.Response.StatusCode);
});

// Global Exception Handling
app.Use(async (context, next) =>
{
    try
    {
        await next();
    }
    catch (Exception ex)
    {
        var logger = context.RequestServices.GetRequiredService<ILogger<Program>>();
        logger.LogError(ex, "Unhandled exception occurred");

        context.Response.StatusCode = 500;
        await context.Response.WriteAsJsonAsync(new
        {
            Title = "Unexpected Error",
            Detail = ex.Message
        });
    }
});

// CleanCodeJN Middlewares
app.UseCleanCodeJNWithMinimalApis();
app.UseCleanCodeJNWithGraphQL();
app.UseCleanCodeJNDocumentation();

// Run
app.Run();

Use IOSP for complex business logic

Derive from BaseIntegrationCommand:

public class YourIntegrationCommand(ICommandExecutionContext executionContext)
    : IntegrationCommand<YourIntegrationRequest, YourDomainObject>(executionContext)

Write Extensions on ICommandExecutionContext with Built in Requests or with your own

public static ICommandExecutionContext CustomerGetByIdRequest(
    this ICommandExecutionContext executionContext, int customerId) 
    => executionContext.WithRequest(
            () => new GetByIdRequest<Customer>
            {
                Id = customerId,
                Includes = [x => x.Invoices, x => x.OtherDependentTable],
            },
            CommandConstants.CustomerGetById);

Use WithParallelWhenAllRequests() to execute multiple requests in parallel and execute when all tasks are finished:

   executionContext.WithParallelWhenAllRequests(
                [
                    () => new GetByIdRequest<Customer, int>
                          {
                              Id = request.Id,
                          },
                    () => new GetByIdRequest<Customer, int>
                          {
                              Id = request.Id,
                          },
                ])

Use GetListParallelWhenAll() to get all results of WithParallelWhenAllRequests():

   .WithRequest(
                () => new YourSpecificRequest
                {
                    Results = executionContext.GetListParallelWhenAll("Parallel Block"),
                })

Use GetParallelWhenAllByIndex<T>() to get the result of the WithParallelWhenAllRequests() with a typed object by index:

   .WithRequest(
                () => new GetByIdRequest<Invoice, Guid>
                {
                    Id = executionContext.GetParallelWhenAllByIndex<Invoice>("Parallel Block", 1).Id,
                })

Use IfRequest() to execute an optional request - continue when conditions are not satisfied:

    executionContext.IfRequest(() => new GetByIdRequest<Customer, int> { Id = request.Id },
                               ifBeforePredicate: () => true,
                               ifAfterPredicate: response => response.Succeeded)

Use IfBreakRequest() to execute an optional request - break whole process when conditions are not satisfied:

    executionContext.IfBreakRequest(() => new GetByIdRequest<Customer, int> { Id = request.Id },
                                    ifBeforePredicate: () => true,
                                    ifAfterPredicate: response => response.Succeeded)

This is how clean your code will look like in the end

public class YourIntegrationCommand(ICommandExecutionContext executionContext)
    : IntegrationCommand<YourIntegrationRequest, Customer>(executionContext)
{
    public override async Task<BaseResponse<Customer>> Handle(YourIntegrationRequest request, CancellationToken cancellationToken) =>
        await ExecutionContext
            .CandidateGetByIdRequest(request.Dto.CandidateId)
            .CustomerGetByIdRequest(request.Dto.CustomerIds)
            .GetOtherStuffRequest(request.Dto.XYZType)
            .PostSomethingRequest(request.Dto)
            .SendMailRequest()
            .Execute<Customer>(cancellationToken);
}

Sample Code

GitHub Full Sample

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CleanCodeJN.GenericApis:

Package Downloads
CleanCodeJN.GenericApis.ServiceBusConsumer

This CleanCodeJN package for Service Bus simplifies the development of asynchronous microservices by providing a framework that leverages the power of MediatR and IOSP to consume service bus events from topics and execute commands to process these events.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.0.0 274 11/12/2025
4.2.10 144 10/24/2025
4.2.9 178 10/22/2025
4.2.8 164 10/21/2025
4.2.7 166 10/20/2025
4.2.6 163 10/20/2025
4.2.5 138 10/18/2025
4.2.4 129 10/17/2025
4.2.3 111 10/17/2025
4.2.2 120 10/17/2025
4.2.1 132 10/17/2025
4.2.0 131 10/17/2025
4.1.10 330 9/18/2025
4.1.9 228 5/28/2025
4.1.8 223 5/21/2025
4.1.7 186 5/20/2025
4.1.6 219 5/19/2025
4.1.5 204 5/19/2025
4.1.4 229 4/3/2025
4.1.3 235 4/2/2025
4.1.2 215 4/2/2025
4.1.1 175 3/28/2025
4.1.0 188 3/28/2025
4.0.3 194 3/27/2025
4.0.2 184 3/27/2025
4.0.1 556 3/25/2025
4.0.0 147 2/28/2025
3.6.1 159 2/7/2025
3.6.0 243 12/12/2024
3.5.1 169 12/7/2024
3.5.0 161 11/19/2024
3.4.1 203 11/10/2024
3.4.0 198 11/10/2024
3.3.0 185 10/21/2024
3.2.2 224 10/20/2024
3.2.1 210 10/20/2024
3.2.0 181 10/20/2024
3.1.9 204 10/18/2024
3.1.8 175 10/9/2024
3.1.7 154 10/8/2024
3.1.6 175 10/8/2024
3.1.5 202 9/25/2024
3.1.4 195 9/12/2024
3.1.3 183 9/12/2024
3.1.2 177 9/12/2024
3.1.1 195 9/11/2024
3.1.0 144 9/9/2024
3.0.4 172 9/6/2024
3.0.3 156 9/6/2024
3.0.2 195 9/3/2024
3.0.1 136 7/30/2024
3.0.0 216 5/24/2024
2.2.0 165 5/23/2024
2.1.1 189 5/22/2024
2.1.0 200 5/15/2024
2.0.8 159 5/14/2024
2.0.7 176 5/14/2024
2.0.6 169 5/14/2024
2.0.5 181 5/13/2024
2.0.4 144 5/13/2024
2.0.3 149 5/12/2024
2.0.2 181 5/11/2024
2.0.1 156 5/8/2024
2.0.0 176 5/7/2024
1.1.1 178 5/7/2024
1.1.0 191 5/7/2024
1.0.6 188 5/7/2024
1.0.5 188 5/7/2024
1.0.4 191 5/7/2024
1.0.3 189 5/7/2024
1.0.2 204 5/6/2024
1.0.1 186 5/6/2024
1.0.0 171 5/6/2024

Update to .NET 10 and Documentation improvements.