CleanCodeJN.GenericApis
5.2.2
See the version list below for details.
dotnet add package CleanCodeJN.GenericApis --version 5.2.2
NuGet\Install-Package CleanCodeJN.GenericApis -Version 5.2.2
<PackageReference Include="CleanCodeJN.GenericApis" Version="5.2.2" />
<PackageVersion Include="CleanCodeJN.GenericApis" Version="5.2.2" />
<PackageReference Include="CleanCodeJN.GenericApis" />
paket add CleanCodeJN.GenericApis --version 5.2.2
#r "nuget: CleanCodeJN.GenericApis, 5.2.2"
#:package CleanCodeJN.GenericApis@5.2.2
#addin nuget:?package=CleanCodeJN.GenericApis&version=5.2.2
#tool nuget:?package=CleanCodeJN.GenericApis&version=5.2.2
๐ Generic Web APIs โ Fast, Clean, Powerful
Build production-ready APIs instantly โ from Minimal APIs and Controllers to fully integrated GraphQL endpoints and an AI-ready MCP Server with CRUD, filtering, sorting & paging โ powered by Mediator, AutoMapper, EF Core, FluentValidation, and the IOSP architecture pattern.
โก Getting Started
dotnet add package CleanCodeJN.GenericApis
// Program.cs
builder.Services.AddCleanCodeJN<MyDbContext>(options =>
{
options.ApplicationAssemblies = // assemblies with your Commands, DTOs, Entities
[
typeof(YourBusiness.AssemblyRegistration).Assembly,
typeof(YourCore.AssemblyRegistration).Assembly,
];
options.ValidatorAssembly = typeof(YourCore.AssemblyRegistration).Assembly;
options.AddDefaultLoggingBehavior = true; // optional
});
var app = builder.Build();
app.UseCleanCodeJNWithMinimalApis(); // REST: registers all IApi endpoints โ /api/...
app.UseCleanCodeJNWithGraphQL(); // GraphQL: auto-schema from entities/DTOs โ /graphql
app.UseCleanCodeJNWithMcp(); // MCP Server: every endpoint = AI tool โ /mcp
app.UseCleanCodeJNWithDocumentation(); // IOSP command docs from XML comments โ /docs
app.MapControllers();
app.Run();
// Entity + DTO (naming convention: <EntityName>GetDto / PostDto / PutDto)
public class Customer : IEntity<int> { public int Id { get; set; } public string Name { get; set; } }
public class CustomerGetDto : IDto { public int Id { get; set; } public string Name { get; set; } }
// Minimal API โ full CRUD in ~10 lines
public class CustomersApi : IApi
{
public List<string> Tags => ["Customers"];
public string Route => "api/v1/Customers";
public List<Func<WebApplication, RouteHandlerBuilder>> HttpMethods =>
[
app => app.MapGet<Customer, CustomerGetDto, int>(Route, Tags),
app => app.MapGetById<Customer, CustomerGetDto, int>(Route, Tags),
app => app.MapPost<Customer, CustomerPostDto, CustomerGetDto>(Route, Tags),
app => app.MapPut<Customer, CustomerPutDto, CustomerGetDto>(Route, Tags),
app => app.MapPatch<Customer, CustomerGetDto, int>(Route, Tags),
app => app.MapDeleteRequest<Customer, CustomerGetDto, int>(Route, Tags, id => new DeleteCustomerRequest { Id = id }),
];
}
// IOSP: complex business logic as clean orchestration
public class DeleteCustomerCommand(ICommandExecutionContext ctx)
: IntegrationCommand<DeleteCustomerRequest, Customer>(ctx)
{
public override async Task<BaseResponse<Customer>> Handle(DeleteCustomerRequest request, CancellationToken ct) =>
await ExecutionContext
.GetCustomerByIdRequest(request.Id)
.ValidateInvoicesRequest()
.DeleteCustomerRequest()
.Execute<Customer>(ct);
}
Use* call |
What it adds | Endpoint |
|---|---|---|
UseCleanCodeJNWithMinimalApis() |
All IApi REST endpoints |
/api/... + Swagger |
UseCleanCodeJNWithGraphQL() |
Auto-generated GraphQL | /graphql |
UseCleanCodeJNWithMcp() |
AI-callable MCP tools | /mcp |
UseCleanCodeJNWithDocumentation() |
IOSP command docs | /docs |
AddCleanCodeJNWithAiChat() (Blazor WASM) |
AI Chat UI connected to your MCP backend | /ai |
3. Program.cs โ AI Proxy config (only if using /ai)
options.AiProxyOptions = new AiProxyOptions
{
AnthropicApiKey = configuration["Anthropic:ApiKey"],
SelfBaseUrl = configuration["SelfBaseUrl"],
Model = "claude-sonnet-4-6",
MaxTokens = 4096,
};
4. The three building blocks
| Block | What you write | What you get |
|---|---|---|
| Entity | class Customer : IEntity<int> |
EF Core + Repository + GraphQL type |
| DTO | class CustomerGetDto : IDto |
Auto-mapped, Swagger schema, MCP output schema |
| IApi | class CustomersApi : IApi |
REST endpoints + MCP tools + GraphQL queries |
// Entity
public class Customer : IEntity<int>
{
public int Id { get; set; }
public string Name { get; set; }
}
// DTO (naming convention: <EntityName>GetDto / PostDto / PutDto)
public class CustomerGetDto : IDto
{
public int Id { get; set; }
public string Name { get; set; }
}
// Minimal API โ all CRUD in ~10 lines
public class CustomersApi : IApi
{
public List<string> Tags => ["Customers"];
public string Route => "api/v1/Customers";
public List<Func<WebApplication, RouteHandlerBuilder>> HttpMethods =>
[
app => app.MapGet<Customer, CustomerGetDto, int>(Route, Tags),
app => app.MapGetById<Customer, CustomerGetDto, int>(Route, Tags),
app => app.MapPost<Customer, CustomerPostDto, CustomerGetDto>(Route, Tags),
app => app.MapPut<Customer, CustomerPutDto, CustomerGetDto>(Route, Tags),
app => app.MapPatch<Customer, CustomerGetDto, int>(Route, Tags),
app => app.MapDeleteRequest<Customer, CustomerGetDto, int>(Route, Tags, id => new DeleteCustomerRequest { Id = id }),
];
}
5. Feature flags โ what each Use* call activates
| Call | Activates | Visit |
|---|---|---|
UseCleanCodeJNWithMinimalApis() |
All IApi REST endpoints |
/api/docs (Swagger) |
UseCleanCodeJNWithGraphQL() |
Auto-generated GraphQL schema | /graphql |
UseCleanCodeJNWithMcp() |
MCP Server (AI-callable tools) | /mcp |
UseCleanCodeJNWithDocumentation() |
IOSP command docs from XML | /docs |
Table of Contents
- โก Getting Started
- ๐ Generic Web APIs โ Fast, Clean, Powerful
- Step by step explanation
- Add AddCleanCodeJN<IDataContext>() to your Program.cs
- These are the CleanCodeJN Options
- Add app.UseCleanCodeJNWithMinimalApis() when using Minimal APIs to your Program.cs
- Add app.UseCleanCodeJNWithGraphQL() when using automatic GraphQL to your Program.cs
- Add app.UseCleanCodeJNDocumentation() when using automatic Command documentation (from your XML comments) to your Program.cs
- ๐ค Add app.UseCleanCodeJNWithMcp() to expose your API as an AI-callable MCP Server
- When using Controllers add this to your Program.cs
- Start writing Minimal Apis by implementing IApi
- Extend standard CRUD operations by specific Where(), Include() or Select() clauses
- Use ApiCrudControllerBase for CRUD operations in controllers
- You can also override your Where, Include or Select clauses
- For using the /filtered api with a filter, just provide a serialized json as filter parameter
- The Type can be specified with these values
- Advanced Topics
- Built-in Support for Fluent Validation
- Implement your own specific Request
- Requests can also be marked as ICachableRequest, which uses IDistributedCache to cache the Response
- With your own specific Command using CleanCodeJN.Repository
- Custom Middlewares
- Use IOSP for complex business logic
- Derive from BaseIntegrationCommand
- Write Extensions on ICommandExecutionContext with Built in Requests or with your own
- Use WithParallelWhenAllRequests() to execute multiple requests in parallel and execute when all tasks are finished
- Use GetListParallelWhenAll() to get all results of WithParallelWhenAllRequests
- Use GetParallelWhenAllByIndex<T> to get the result of the WithParallelWhenAllRequests with a typed object by index
- Use IfRequest() to execute an optional request - continue when conditions are not satisfied
- Use IfBreakRequest() to execute an optional request - break whole process when conditions are not satisfied
- See the how clean your code will look like in the end
- ๐ฌ AI Chat UI โ /ai Page
- Sample Code
โจ This package gives you
- โก CRUD APIs in seconds โ Minimal API or Controller-based, zero boilerplate
- ๐งฌ Auto-generated GraphQL โ query/mutation/filter/sort/projection via HotChocolate
- ๐ค MCP Server โ one line exposes your entire API as AI-callable tools (Claude, Cursor, โฆ)
- ๐ฌ AI Chat UI โ ready-made
/aiBlazor page: chat with your API in natural language - ๐ฆ Paging, filtering & projections โ built-in, no extra code
- ๐ Auto-mapping โ Entities โ DTOs by naming convention, no AutoMapper config needed
- ๐งช FluentValidation โ validators auto-discovered and executed on POST/PUT
- ๐งผ IOSP architecture โ clean orchestration of complex business logic
- ๐ Command docs โ auto-generated workflow documentation from XML comments at
/docs - ๐ .NET 10, EF Core 10, fully testable & mockable
๐งช What is IOSP?
Integration Operation Segregation Principle โ split your handlers into: Operations โ real logic (DB, external APIs, โฆ) Integrations โ pure orchestration of other handlers, no logic of their own
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
๐ค Add app.UseCleanCodeJNWithMcp() to expose your API as an AI-callable MCP Server
One line of code turns your entire API into a Model Context Protocol (MCP) Server โ discoverable and executable by any AI assistant that supports the standard MCP Streamable HTTP transport (Claude, Cursor, Continue, and more).
app.UseCleanCodeJNWithMcp();
This registers a POST /mcp endpoint implementing the MCP Streamable HTTP transport (protocol version 2024-11-05). No custom protocol, no vendor lock-in โ any standard MCP client works out of the box.
What gets auto-generated?
CRUD tools are generated automatically from your Controllers and Minimal APIs โ including full JSON schemas derived from your DTOs:
| Tool | Description |
|---|---|
list_customer |
Retrieve all Customer records |
get_customer_by_id |
Retrieve a single Customer by ID |
create_customer |
Create a new Customer (schema from CustomerPostDto) |
update_customer |
Update an existing Customer (schema from CustomerPutDto) |
delete_customer |
Delete a Customer by ID |
Custom endpoint tools are generated from any Minimal API endpoint annotated with .WithSummary() / .WithDescription():
public List<Func<WebApplication, RouteHandlerBuilder>> HttpMethods =>
[
// Standard CRUD โ auto-discovered, no annotation needed
app => app.MapGet<Customer, CustomerGetDto, int>(Route, Tags),
app => app.MapPost<Customer, CustomerPostDto, CustomerGetDto>(Route, Tags),
// Custom endpoints โ annotate to expose as MCP tools
app => app.MapGetRequest(Route + "/cached", Tags, async ([FromServices] ApiBase api) =>
await api.Handle<Customer, List<CustomerGetDto>>(new CachedCustomerRequest()))
.WithSummary("Get cached customers")
.WithDescription("Returns a cached list of customers. Served from cache if available."),
app => app.MapDeleteRequest<Customer, CustomerGetDto, int>(Route, Tags, id => new DeleteCustomerIntegrationRequest { Id = id })
.WithSummary("Delete customer via integration flow")
.WithDescription("Deletes a customer using the full integration delete workflow."),
];
Enrich tool schemas with DTO XML comments
Add /// <summary> comments to your DTO properties โ the MCP server reads them automatically and includes them as description fields in the JSON schema. This tells the AI assistant exactly what each field means, including constraints and examples.
Step 1: Enable XML doc generation in your DTO project's .csproj:
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
Step 2: Add XML comments to your DTO properties:
public class CustomerPostDto : IDto
{
/// <summary>Full name of the customer, e.g. 'Acme Corp'. Maximum 100 characters.</summary>
public string Name { get; set; }
}
public class CustomerPutDto : IDto
{
/// <summary>Unique identifier of the customer to update.</summary>
public int Id { get; set; }
/// <summary>New full name of the customer, e.g. 'Acme Corp'. Maximum 100 characters.</summary>
public string Name { get; set; }
}
The MCP tool schema for create_customer will then look like:
{
"name": "create_customer",
"inputSchema": {
"properties": {
"name": {
"type": "string",
"description": "Full name of the customer, e.g. 'Acme Corp'. Maximum 100 characters."
}
}
}
}
This enables AI assistants to ask the right questions when creating or updating records โ e.g. "What is the customer's full name?" โ before sending the request.
IOSP Command tools are generated from your XML documentation โ giving AI assistants insight into your business workflows:
/// <summary>Handles the deletion of a customer including all related data.</summary>
/// <remarks>Executes in parallel: retrieves customer, validates invoices, then deletes.</remarks>
public class DeleteCustomerIntegrationCommand(...) : IntegrationCommand<...> { }
All tool calls go through the standard HTTP pipeline โ your authentication and authorization middleware applies automatically.
Connect any MCP client
Claude Desktop (claude_desktop_config.json):
{
"mcpServers": {
"my-api": {
"url": "https://your-api.com/mcp"
}
}
}
Cursor (.cursor/mcp.json):
{
"mcpServers": {
"my-api": {
"url": "https://your-api.com/mcp"
}
}
}
Once connected, your AI assistant knows your full API surface and can answer "what can you do with customers?" or execute "create a customer named Acme Corp" directly.
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.UseCleanCodeJNWithMcp();
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);
}
๐ฌ AI Chat UI โ /ai Page
The CleanCodeJN.GenericApis.Chat package provides a ready-made Blazor WebAssembly chat page at /ai. It connects directly to your backend's MCP server and lets users interact with your API through natural language โ powered by Claude.
Requires
UseCleanCodeJNWithMcp()on the backend.
Install
dotnet add package CleanCodeJN.GenericApis.Chat # Blazor WASM UI component
Backend โ Program.cs additions
// Register the AI proxy service (streams Claude responses + executes MCP tool calls)
builder.Services.AddHttpClient("AiProxy");
builder.Services.Configure<AiProxyOptions>(builder.Configuration.GetSection("AiProxy"));
builder.Services.AddScoped<AiProxyService>();
// appsettings.json
{
"AiProxy": {
"AnthropicApiKey": "sk-ant-...",
"Model": "claude-opus-4-5",
"MaxTokens": 8096,
"SelfBaseUrl": "https://localhost:7132"
}
}
The backend must also expose the /api/ai/stream SSE endpoint โ this is included automatically when AiProxyService is registered and the endpoint is mapped:
app.MapPost("/api/ai/stream", async (
ChatRequest request,
AiProxyService aiProxy,
HttpContext context) =>
{
var bearer = context.Request.Headers.Authorization.ToString().Replace("Bearer ", "");
context.Response.ContentType = "text/event-stream";
await foreach (var ev in aiProxy.StreamAsync(request, bearer, context.RequestAborted))
await context.Response.WriteAsync($"data: {JsonSerializer.Serialize(ev)}\n\n");
});
Blazor WASM โ Program.cs
using CleanCodeJN.GenericApis.Chat.Extensions;
builder.Services.AddMudServices();
builder.Services.AddCleanCodeJNWithAiChat(options =>
{
options.BackendUrl = "https://localhost:7132"; // URL of your CleanCodeJN backend
options.Title = "My AI Assistant"; // Title shown in the app bar
options.ShowToolCalls = true; // Show tool call chips in the chat
// options.BearerToken = "your-token"; // Optional: bearer token for auth
});
App.razor โ register the /ai route from the library
<Router AppAssembly="@typeof(App).Assembly"
AdditionalAssemblies="@(new[] { typeof(CleanCodeJN.GenericApis.Chat.Pages.AiChatPage).Assembly })">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)" />
</Found>
</Router>
Layout/MainLayout.razor โ MudBlazor dark theme
@inherits LayoutComponentBase
<MudThemeProvider IsDarkMode="true" Theme="_theme" />
<MudPopoverProvider />
<MudDialogProvider />
<MudSnackbarProvider />
@Body
@code {
private readonly MudTheme _theme = new()
{
PaletteDark = new PaletteDark
{
Primary = "#4CAF50",
AppbarBackground = "#0d1b2a",
Background = "#0a1520",
Surface = "#112233",
}
};
}
What you get
| Feature | Description |
|---|---|
| Streaming chat | Claude responses stream token by token via SSE |
| Tool sidebar | All MCP tools listed โ click to insert into input |
| Tool call chips | Visual indicator when Claude calls a tool and receives a result |
| Markdown rendering | Tables, code blocks, lists rendered with syntax highlighting |
| Dark theme | CleanCodeJN-branded dark UI out of the box |
| Auto-redirect | App opens directly at /ai on start |
Sample Code
| Product | Versions 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. |
-
net10.0
- Anthropic.SDK (>= 5.10.0)
- AutoMapper (>= 14.0.0)
- BananaCakePop.Middleware (>= 17.0.0)
- CleanCodeJN.GenericApis.Abstractions (>= 4.0.0)
- CleanCodeJN.Repository.EntityFramework (>= 2.0.1)
- FluentValidation (>= 12.1.1)
- FluentValidation.AspNetCore (>= 11.3.1)
- HotChocolate.AspNetCore (>= 15.1.12)
- HotChocolate.AspNetCore.Authorization (>= 15.1.12)
- HotChocolate.Data (>= 15.1.12)
- MediatR (>= 12.5.0)
- Microsoft.AspNetCore.JsonPatch (>= 10.0.3)
- Microsoft.Extensions.Caching.Memory (>= 10.0.3)
- Newtonsoft.Json (>= 13.0.4)
- System.Formats.Asn1 (>= 10.0.3)
- System.Text.Json (>= 10.0.3)
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.2.4 | 28 | 3/10/2026 |
| 5.2.3 | 33 | 3/10/2026 |
| 5.2.2 | 35 | 3/9/2026 |
| 5.2.1 | 30 | 3/9/2026 |
| 5.2.0 | 37 | 3/9/2026 |
| 5.1.1 | 35 | 3/8/2026 |
| 5.1.0 | 35 | 3/8/2026 |
| 5.0.1 | 40 | 3/8/2026 |
| 5.0.0 | 377 | 11/12/2025 |
| 4.2.10 | 179 | 10/24/2025 |
| 4.2.9 | 207 | 10/22/2025 |
| 4.2.8 | 193 | 10/21/2025 |
| 4.2.7 | 198 | 10/20/2025 |
| 4.2.6 | 191 | 10/20/2025 |
| 4.2.5 | 159 | 10/18/2025 |
| 4.2.4 | 158 | 10/17/2025 |
| 4.2.3 | 140 | 10/17/2025 |
| 4.2.2 | 153 | 10/17/2025 |
| 4.2.1 | 157 | 10/17/2025 |
| 4.2.0 | 164 | 10/17/2025 |
BugFixes: Readme.md page.