Reference

Parameter Binding

Enable automatic parameter hydration for generated procedures with minimal setup.

Overview

Xtraq can auto-bind input parameters (including UDTTs) before a procedure executes. Add a binding once; let the generator hydrate parameters when they are missing.

Configuration (.xtraqconfig)

{
  "Api": {
    "Requests": {
      "AutoBind": [
        "@UserId INT",
        "@Entries shared.AuditLogEntryTableType READONLY"
      ],
      "AutoBindProcedures": [
        "sample.UserCompositeJsonSnapshot",
        "sample.WriteAuditLogEntries"
      ]
    }
  }
}
  • AutoBindProcedures filters binding to specific procedures (schema-qualified).
  • AutoBind is informational for you; resolvers live in code.

Register bindings

builder.Services.AddHttpContextAccessor();
builder.Services.AddXtraqDbContext(options =>
{
    options.ConnectionString = "...";
    options.ParameterBindings
        .BindScalar<int>("@UserId", async (sp, _) =>
        {
            var accessor = sp?.GetService<IHttpContextAccessor>();
            var sub = accessor?.HttpContext?.User?.FindFirstValue("sub");
            return int.TryParse(sub, out var id) ? id : 0;
        }, "sample.UserCompositeJsonSnapshot");
});
  • BindScalar/BindTable(..., params string[] procedures) filter by procedure when needed.
  • Bindings fire only when the request leaves the parameter null; existing values are respected.
  • Use Bind*WithInput when you need both ambient data and request data (for example, context TVPs).

Notes

  • Request DTO mappers (ConfigureProcedure<TRequest, TInput>) apply bindings before constructing inputs.
  • Table-valued parameters hydrate as IReadOnlyList<T>; bindings skip when the request already has a value.
  • Need more examples (streaming, envelopes)? Pair this with API Integration.

Request DTOs & Auto-Binding

  • Add an Api section when you want the generator to emit <Proc>Request records, table-type request rows, and their mappers. These DTOs feed straight into ConfigureProcedure<TRequest, TInput> and the selector-based ExecuteAsync overloads.
  • The mapper auto-binds missing values via ParameterBindingOptions (ResolveValueAsync) before constructing <Proc>Input. If a required parameter stays null, it throws an InvalidOperationException instead of defaulting to zero/empty.
  • Table-valued parameters hydrate as IReadOnlyList<T>; bindings fire only when the request leaves the property null. When request projections are enabled, companion TableTypeRequest rows expose DataAnnotations for HTTP validation.
  • Streaming helpers also accept request DTO overloads (e.g. StreamResult...Async(request, onRow, ct)); the request is mapped and hydrated before streaming starts, so row handlers see the fully prepared payload.