API Reference ResultStatuses - ulfbou/Zentient.Results GitHub Wiki
static class
โ ResultStatuses
๐ API Reference: Namespace:
Zentient.Results
Assembly:Zentient.Results.dll
Available since:v0.1.0
๐ Summary
ResultStatuses
is a centralized static class providing a comprehensive set of predefined, immutable, thread-safe standard IResultStatus
instances representing common HTTP-like status codes and their associated descriptions. It also exposes a cache-enabled factory method to retrieve or create custom statuses by numeric code.
This class acts as the authoritative source for status codes across the Zentient.Results ecosystem, enabling consistent and efficient status reuse, while allowing extensibility for custom codes.
๐ก Design Philosophy / Rationale
- Designed to promote reuse and immutability of common statuses like
Ok (200)
orNotFound (404)
. - Implements a thread-safe internal cache (
ConcurrentDictionary
) for fast retrieval and deduplication of both standard and user-defined statuses. - Aligns closely with HTTP status semantics to foster familiarity and ease integration with web protocols and problem details standards (RFC 7807).
- Facilitates structured result modeling where status codes carry business and transport meaning.
- Provides both static properties for common statuses and dynamic creation for custom codes, promoting extensibility without compromising type safety or performance.
๐ Type Signature
public static class ResultStatuses
๐ฆ Properties
Ok
1.1. - Type:
IResultStatus
- Summary: Represents the HTTP 200 OK status, indicating successful operation.
- Remarks: Alias
Success
points to this same instance.
Success
1.2. - Type:
IResultStatus
- Summary: Alias for
Ok
.
Created
, BadRequest
, NotFound
, InternalServerError
)
1.3. Other Status Properties (e.g., - Type:
IResultStatus
- Summary: Predefined statuses for all common HTTP success, redirection, client error, and server error codes.
- Remarks: Each status is an immutable
ResultStatus
instance carrying aCode
andDescription
fromResultStatusConstants
.
๐ง Methods
GetStatus(int code, string? description = null)
2.1. -
Signature:
public static IResultStatus GetStatus(int code, string? description = null)
-
Summary: Retrieves an existing
IResultStatus
instance by its code if available; otherwise, creates and caches a new custom status with the provided code and optional description. -
Parameters:
code
(int
): Numeric status code to retrieve or create.description
(string?
): Optional textual description for the custom status.
-
Return Value: (
IResultStatus
) The existing or newly created status instance. -
Behavior: Utilizes a thread-safe concurrent dictionary to avoid duplicate instances for the same code.
-
Example Usage:
var status = ResultStatuses.GetStatus(418, "I'm a teapot (custom)");
๐ญ Inner Types
ResultStatus
(private sealed class)
3.1. - Implements
IResultStatus
for predefined statuses. - Stores immutable
Code
andDescription
properties. - Overrides
ToString()
to return a formatted string like[200] OK
.
CustomResultStatus
(private sealed class)
3.2. - Implements
IResultStatus
for user-defined statuses created dynamically. - Stores immutable
Code
andDescription
. - Overrides
ToString()
to include(Custom)
suffix for clarity.
๐งช Usage Examples
// Accessing a standard status
IResultStatus okStatus = ResultStatuses.Ok;
Console.WriteLine(okStatus.Code); // 200
Console.WriteLine(okStatus.Description); // "OK"
// Creating or retrieving a custom status
IResultStatus custom = ResultStatuses.GetStatus(499, "Client Closed Request");
Console.WriteLine(custom); // "[499] Client Closed Request (Custom)"
โ ๏ธ Remarks
- All predefined statuses are immutable singleton instances for efficiency and safety.
GetStatus
ensures that each numeric code corresponds to exactly one instance in the cache.- While modeled on HTTP codes, the class is transport-agnostic and usable in any domain requiring numeric status semantics.
- Not thread-blocking; uses
ConcurrentDictionary
for lock-free concurrency.
๐งฉ Integration and Interoperability
- Used extensively by
Result<T>
and other result implementations as the standard source of status codes. - Complements
ResultStatusConstants
which provides raw code and description constants. - Aligns with **HTTP status codes and **RFC 9457 problem details for error interoperability.
- Extensible for custom domain-specific status codes beyond HTTP semantics.
๐ See Also
๐ท๏ธ Tags
#ResultStatus
#Immutable
#Singletons
#HTTPStatusCodes
#ThreadSafe
#ZentientCore
#StableContract
Last Updated: 2025-06-21 Version: 0.4.0