API Reference ResultStatuses - ulfbou/Zentient.Results GitHub Wiki
๐ API Reference: static class โ ResultStatuses
Namespace:
Zentient.ResultsAssembly:Zentient.Results.dllAvailable 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
1.1. Ok
- Type:
IResultStatus - Summary: Represents the HTTP 200 OK status, indicating successful operation.
- Remarks: Alias
Successpoints to this same instance.
1.2. Success
- Type:
IResultStatus - Summary: Alias for
Ok.
1.3. Other Status Properties (e.g., Created, BadRequest, NotFound, InternalServerError)
- Type:
IResultStatus - Summary: Predefined statuses for all common HTTP success, redirection, client error, and server error codes.
- Remarks: Each status is an immutable
ResultStatusinstance carrying aCodeandDescriptionfromResultStatusConstants.
๐ง Methods
2.1. GetStatus(int code, string? description = null)
-
Signature:
public static IResultStatus GetStatus(int code, string? description = null) -
Summary: Retrieves an existing
IResultStatusinstance 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
3.1. ResultStatus (private sealed class)
- Implements
IResultStatusfor predefined statuses. - Stores immutable
CodeandDescriptionproperties. - Overrides
ToString()to return a formatted string like[200] OK.
3.2. CustomResultStatus (private sealed class)
- Implements
IResultStatusfor user-defined statuses created dynamically. - Stores immutable
CodeandDescription. - 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.
GetStatusensures 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
ConcurrentDictionaryfor lock-free concurrency.
๐งฉ Integration and Interoperability
- Used extensively by
Result<T>and other result implementations as the standard source of status codes. - Complements
ResultStatusConstantswhich 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