Path: redb/redb.Templates/templates/redb-bff/RedbBff.Backend/Controllers/ApiController.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Templates/templates/redb-bff/RedbBff.Backend/Controllers/ApiController.cs
----
using System.ComponentModel.DataAnnotations;
using redb.Core;
using redb.Route.Controllers;
using redb.Route.Http;
using redb.Route.RedbCore.Extensions;
using RedbBff.Models;
namespace RedbBff.Backend.Controllers;
///
/// What the controllers of this backend share. A controller is created for each request, without DI:
/// what it needs comes from Context (the route context) and Exchange (the request).
///
public abstract class ApiController : RedbController
{
///
/// The redb service of this request: scoped to the exchange and disposed with it, so parallel requests
/// never share a connection. The empty name is the host's default redb.
///
protected IRedbService Redb => Context.GetRedbService("", Exchange);
/// Sets the status code of the answer; the returned object is still the body.
protected T Status(int code, T body)
{
Exchange.Out ??= Exchange.In.Clone();
Exchange.Out.Headers[HttpHeaders.ResponseCode] = code;
return body;
}
protected ApiError NotFound(string message) => Status(404, new ApiError { Message = message });
/// The web server validates the form, but the backend cannot trust that: the same attributes are checked here.
protected ApiError? Invalid(object dto)
{
var errors = new List();
return Validator.TryValidateObject(dto, new ValidationContext(dto), errors, validateAllProperties: true)
? null
: Status(400, new ApiError { Message = string.Join(" ", errors.Select(r => r.ErrorMessage)) });
}
}