Files
sor/Server/src/SOR.Shared/Models/ServiceResult.cs
T
d21adbc8d0 Add networking foundation — 9-project C# solution (compiles clean)
Implements the modular networking layer for SOR Server:
- SOR.Networking.Abstractions: pure interfaces (ITransport, IClientConnection,
  IMessageSerializer, IMessageDispatcher, IMessageHandler, IEventBus, IClientPush)
- SOR.Networking.WebSocket: ITransport + IClientConnection over System.Net.WebSockets
- SOR.Networking.Nats: IEventBus over NATS.Net (INatsConnection)
- SOR.Networking.Redis: IClientPush over Redis Pub/Sub
- SOR.Shared: ServiceResult<T>, RedisKeys, NatsSubjects, ServerMessage, JSON serializer
- SOR.Shared.Proto: auth.proto with gRPC AuthService contract
- SOR.Gateway: accept loop, auth handshake, dispatcher, Redis push subscriber
- SOR.Auth: stub gRPC handler (placeholder responses)
- SOR.Host: multi-service runner via ENABLED_SERVICES env var; wires all DI

All concrete implementations are wired exclusively in SOR.Host — every transport,
broker, and push implementation can be swapped by changing only Program.cs.

dotnet build SOR.slnx → 0 errors, 0 warnings

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
2026-02-24 21:01:38 -05:00

61 lines
2.7 KiB
C#

namespace SOR.Shared.Models;
/// <summary>
/// Standardized return type for business logic operations.
///
/// Expected failures (user not found, invalid input, duplicate name, etc.) are
/// returned as ServiceResult.Fail(...) — no exception is thrown.
/// Exceptions are reserved for truly unexpected failures (database unreachable, OOM).
///
/// Usage in a service:
/// public async Task{ServiceResult{string}} LoginAsync(string email, string password)
/// {
/// var user = await _repo.FindByEmailAsync(email);
/// if (user is null) return ServiceResult{string}.Fail("USER_NOT_FOUND", "...");
/// ...
/// return ServiceResult{string}.Ok(token);
/// }
///
/// Usage in a handler:
/// var result = await _authService.LoginAsync(email, password);
/// if (!result.Success) return ServerMessage.Error(req.Seq, result.ErrorCode!, result.ErrorMessage!);
/// return ServerMessage.Reply("auth.login.ack", req.Seq, new { Token = result.Value });
///
/// 'record' generates equality, ToString(), and a compact constructor automatically.
/// The generic parameter T is the success value type.
/// </summary>
public sealed record ServiceResult<T>
{
/// <summary>True if the operation completed without an expected failure.</summary>
public required bool Success { get; init; }
/// <summary>
/// The operation's output value. Non-null when Success = true.
/// The '?' after T makes this property nullable (can hold null).
/// </summary>
public T? Value { get; init; }
/// <summary>
/// Machine-readable error code. Non-null when Success = false.
/// Convention: SCREAMING_SNAKE_CASE. Examples: "USER_NOT_FOUND", "INVALID_PASSWORD"
/// Handlers map these to IServerMessage error responses sent to the client.
/// </summary>
public string? ErrorCode { get; init; }
/// <summary>
/// Human-readable error description. Non-null when Success = false.
/// May be sent to the client in development; use generic messages in production.
/// </summary>
public string? ErrorMessage { get; init; }
// ── Factory methods ────────────────────────────────────────────────────────
/// <summary>Returns a successful result containing the given value.</summary>
public static ServiceResult<T> Ok(T value) =>
new() { Success = true, Value = value };
/// <summary>Returns a failed result with the given error code and message.</summary>
public static ServiceResult<T> Fail(string errorCode, string message) =>
new() { Success = false, ErrorCode = errorCode, ErrorMessage = message };
}