ワンクリックで
result-pattern
Canonical Result<T> pattern for backend tasks. Load on every backend task.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Canonical Result<T> pattern for backend tasks. Load on every backend task.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
Structure and rules for .NET projects using Clean Architecture with DDD. Use when the project has complex business domain, multiple use cases, need to test business logic in isolation, or when the system is enterprise, e-commerce, ERP, CRM, or any rich domain. Do not use for simple CRUDs without logic — prefer Vertical Slice in that case. For implementation details, load the required `backend/dotnet/orms/ef-core/*/SKILL.md` leaf skill(s) or your chosen data access skill.
Structure and rules for .NET projects using Hexagonal Architecture (Ports & Adapters). Use when the project has multiple input channels (API, CLI, message queue), need to test the domain in isolation, or when adapters change frequently. Do not use for simple CRUDs without logic — prefer Vertical Slice in that case. For implementation details, load the required `backend/dotnet/orms/ef-core/*/SKILL.md` leaf skill(s) or your chosen data access skill.
Structure and rules for .NET projects using Onion Architecture. Use when the project has a strong domain-centric focus with DDD, aggregate roots, and the Specification pattern. All dependencies point inward toward the domain core. Do not use for simple CRUDs without logic — prefer Vertical Slice in that case. For implementation details, load the required `backend/dotnet/orms/ef-core/*/SKILL.md` leaf skill(s) or your chosen data access skill.
Canonical DbContext setup and registration rules for EF Core. Load when creating or modifying DbContext classes or their DI wiring.
Pure LINQ standards for .NET 10 projects. Covers language operators, query composition, projection patterns, filtering, grouping, and performance best practices over IEnumerable<T> and IQueryable<T>. Load when writing or reviewing LINQ expressions in any layer. DO NOT load expecting EF Core-specific methods (ToListAsync, AsNoTracking, Include, ExecuteUpdateAsync) — those belong to `backend/dotnet/orms/ef-core/query-best-practices/SKILL.md`.
Canonical rules for using AppDbContext DIRECTLY from application code (use cases / handlers) when the project's data-access pattern is Direct DbContext — no repositories, no UnitOfWork. Load when the chosen pattern is Direct DbContext and a use case reads or writes data. This is the counterpart to repository-usage; never load both for the same task.
| name | result-pattern |
| description | Canonical Result<T> pattern for backend tasks. Load on every backend task. |
| requires | ["backend-dotnet-csharp"] |
| produces | ["result-pattern","action-result-mapping"] |
Result<TResponse> for backend success and failure flowsHttpStatusCode to Success() and Failure()result.ToActionResult() — never remap Result manuallypublic sealed class Result<TResponse>
{
public bool IsSuccess { get; private set; }
public HttpStatusCode HttpStatusCode { get; private set; }
public string Description { get; private set; } = string.Empty;
public List<string> Errors { get; private set; } = [];
public TResponse? Payload { get; private set; }
private Result(bool isSuccess, HttpStatusCode httpStatusCode)
{
IsSuccess = isSuccess;
HttpStatusCode = httpStatusCode;
}
public Result<TResponse> WithDescription(string description)
{
Description = description;
return this;
}
public Result<TResponse> WithErrors(List<string> errors)
{
Errors = errors;
return this;
}
public Result<TResponse> WithPayload(TResponse payload)
{
Payload = payload;
return this;
}
public static Result<TResponse> Success(HttpStatusCode httpStatusCode)
{
return new(true, httpStatusCode);
}
public static Result<TResponse> Failure(HttpStatusCode httpStatusCode)
{
return new(false, httpStatusCode);
}
}
Every Success() and Failure() call must include an HttpStatusCode.
Result<UserDto>.Success(HttpStatusCode.Created)Result<UserDto>.Failure(HttpStatusCode.NotFound)Result<UserDto>.Success()Result<UserDto>.Success(201)Result<TResponse> with the final HttpStatusCoderesult.ToActionResult()HttpStatusCode.Created for resource creationHttpStatusCode.OK for successful reads or updatesHttpStatusCode.BadRequest for validation errorsHttpStatusCode.NotFound for missing resourcesHttpStatusCode.Conflict for business rule conflictsHttpStatusCode.InternalServerError for unexpected persistence or infrastructure failurespublic static class ResultExtensions
{
public static IActionResult ToActionResult<TResponse>(this Result<TResponse> result)
{
if (result.IsSuccess)
{
return new ObjectResult(result.Payload)
{
StatusCode = (int)result.HttpStatusCode
};
}
ProblemDetails problemDetails = new()
{
Status = (int)result.HttpStatusCode,
Title = "One or more errors occurred.",
Detail = result.Description
};
problemDetails.Extensions["errors"] = result.Errors;
return new ObjectResult(problemDetails)
{
StatusCode = (int)result.HttpStatusCode
};
}
}
result.ToActionResult()result.IsSuccess in controllers to choose a status codeHttpStatusCode in controllersProblemDetails mapping in each endpoint// ✅ CORRECT — controller delegates response mapping
[HttpPost]
public async Task<IActionResult> Create(
[FromBody] CreateProductRequestDto request,
[FromServices] ICreateProduct useCase,
CancellationToken ct)
{
Result<CreateProductResponseDto> result = await useCase.ExecuteAsync(request, ct);
return result.ToActionResult();
}
// ❌ WRONG — controller rebuilds status logic manually
[HttpPost]
public async Task<IActionResult> Create(...)
{
Result<CreateProductResponseDto> result = await useCase.ExecuteAsync(request, ct);
if (result.HttpStatusCode == HttpStatusCode.NotFound)
return NotFound(result.Description);
if (!result.IsSuccess)
return BadRequest(result.Errors);
return Ok(result.Payload);
}